LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 03-08-2012, 11:38 AM   #1
hydraMax
Member
 
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, 11:56 AM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,059

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
Hi there,

Quote:
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, 02:59 PM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,455

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

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
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
 
Old 03-08-2012, 06:02 PM   #5
tuxdev
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, 11:11 AM   #6
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
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.
 
  


Reply


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 08:10 AM
Rounding in pure bash anonguy9 LinuxQuestions.org Member Success Stories 4 03-29-2009 01:20 PM
HEELLP ~~ Converting an integer minute into time, Rounding off a double ~~ HELLP Mistro116@yahoo.com Programming 5 10-04-2005 12:51 PM
C++ Rounding and Truncation Opeth Programming 4 09-17-2005 08:16 PM
Java Precission rounding. Tru_Messiah Programming 4 05-15-2004 12:23 AM


All times are GMT -5. The time now is 02:02 AM.

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