LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 10: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, 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: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
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, 01:59 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
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
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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, 05:02 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

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
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
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



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 LinuxQuestions.org Member Success Stories 4 03-29-2009 12:20 PM
HEELLP ~~ Converting an integer minute into time, Rounding off a double ~~ HELLP Mistro116@yahoo.com 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:46 PM.

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
Open Source Consulting | Domain Registration