Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org C: Integer Division Rounding
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 03-08-2012, 10:38 AM #1 hydraMax Member   Registered: Jul 2010 Location: Skynet Distribution: Debian + Emacs Posts: 467 Blog Entries: 60 Rep: C: Integer Division Rounding Sanity check: In C, integer division does always round down, correct? 15/4 produces 3 on any architecture, yes?
03-08-2012, 10:56 AM   #2
Doc CPU
Senior Member

Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep:
Hi there,

Quote:
 Originally Posted by hydraMax Sanity check: In C, integer division does always round down, correct? 15/4 produces 3 on any architecture, yes?
if you want to call it "rounding", yes.
That's how integer division is defined: 15 / 4 is 3, with a remainder of 3.
Or: "How often does 4 fit entirely into 15?"

[X] Doc CPU

 03-08-2012, 01:59 PM #3 sundialsvcs LQ Guru   Registered: Feb 2004 Location: SE Tennessee, USA Distribution: Gentoo, LFS Posts: 8,503 Blog Entries: 4 Rep: Math operations never round anything ... unless you do it yourself by calling an appropriate rounding function. Integer division always truncates.
 03-08-2012, 02:42 PM #4 jlinkels Senior Member   Registered: Oct 2003 Location: Bonaire Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE Posts: 4,631 Rep: Wow this has been a long time since I programmed C... forgot even what is allowed. Code: ```int i; i = 15/4;``` apparently assigns 3 to i. What will this do: Code: ```int i; i = 15.0/4.0;``` Will the compiler call the correct conversion? jlinkels
 03-08-2012, 05:02 PM #5 tuxdev Senior Member   Registered: Jul 2005 Distribution: Slackware Posts: 2,014 Rep: It'll truncate when the implicit cast happens, 'i' will still be 3
03-09-2012, 10:11 AM   #6
orgcandman
Member

Registered: May 2002
Location: dracut MA
Distribution: Fedora, RHEL
Posts: 600

Rep:
Quote:
 Originally Posted by tuxdev It'll truncate when the implicit cast happens, 'i' will still be 3
Indeed, that's actually specified in the standard, so you can rely on this behavior*.

*= there are a few caveats, so be sure to read the c99 standard very thoroughly w.r.t. arithematic and conversion.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post MahendraL Linux - Newbie 1 09-09-2009 07:10 AM anonguy9 LinuxQuestions.org Member Success Stories 4 03-29-2009 12:20 PM Mistro116@yahoo.com Programming 5 10-04-2005 11:51 AM Opeth Programming 4 09-17-2005 07:16 PM Tru_Messiah Programming 4 05-14-2004 11:23 PM

LinuxQuestions.org

All times are GMT -5. The time now is 01:57 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -