Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


Search this Thread
Old 03-08-2012, 10:38 AM   #1
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
C: Integer Division Rounding

Sanity check: In C, integer division does always round down, correct? 15/4 produces 3 on any architecture, yes?
Old 03-08-2012, 10:56 AM   #2
Senior Member
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 343Reputation: 343Reputation: 343Reputation: 343
Hi there,

Originally Posted by hydraMax View Post
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
Old 03-08-2012, 01:59 PM   #3
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,655

Rep: Reputation: 1274Reputation: 1274Reputation: 1274Reputation: 1274Reputation: 1274Reputation: 1274Reputation: 1274Reputation: 1274Reputation: 1274
Math operations never round anything ... unless you do it yourself by calling an appropriate rounding function. Integer division always truncates.
Old 03-08-2012, 02:42 PM   #4
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,480

Rep: Reputation: 623Reputation: 623Reputation: 623Reputation: 623Reputation: 623Reputation: 623
Wow this has been a long time since I programmed C... forgot even what is allowed.

int i;
i = 15/4;
apparently assigns 3 to i.

What will this do:
int i;
i = 15.0/4.0;
Will the compiler call the correct conversion?

Old 03-08-2012, 05:02 PM   #5
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
It'll truncate when the implicit cast happens, 'i' will still be 3
Old 03-09-2012, 10:11 AM   #6
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Originally Posted by tuxdev View Post
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer division operation in Linux kernel MahendraL Linux - Newbie 1 09-09-2009 07:10 AM
Rounding in pure bash anonguy9 Member Success Stories 4 03-29-2009 12:20 PM
HEELLP ~~ Converting an integer minute into time, Rounding off a double ~~ HELLP Programming 5 10-04-2005 11:51 AM
C++ Rounding and Truncation Opeth Programming 4 09-17-2005 07:16 PM
Java Precission rounding. Tru_Messiah Programming 4 05-14-2004 11:23 PM

All times are GMT -5. The time now is 09:47 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration