Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org int -- floor or ceiling? (C Programming)
 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

 04-16-2006, 07:48 PM #1 smoothdogg00 Member   Registered: Feb 2006 Location: Maine Distribution: Ubuntu Posts: 44 Rep: int -- floor or ceiling? (C Programming) When an int is divided by another int and stored, (int x = y/z: int x = 5/3) does it floor or ceiling, or does it depend on the outcome? Would x be 1 or 2?
 04-16-2006, 08:14 PM #2 95se Member   Registered: Apr 2002 Location: Windsor, ON, CA Distribution: Ubuntu Posts: 740 Rep: Think of it as always chopping off the decimal point, so floor
 04-16-2006, 08:15 PM #3 haymaker2000 Member   Registered: Jan 2006 Location: New Zealand Distribution: Ubuntu 5.10 (Breezy), Debian 3.1 (Sarge) Posts: 37 Rep: I could be wrong, but I think it floors. I'm actually quite sure about this, because if you do modulo arithmetic, you always get the remainder. So, 6/4 would give you 1 remainder 2. The remainder can be calculated using 6%4 (=2). So, in general a%b = r (a,b integers).
04-17-2006, 02:34 AM   #4
primo
Member

Registered: Jun 2005
Posts: 542

Rep:
Quoting from: http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html
Quote:
 The integer division and modulus operators are defined to perform truncation towards zero. (In C89, it was implementation-defined whether truncation was done towards zero or -infinity. This is (obviously) important only if one or both operands are negative. Consider: -22 / 7 = -3 -22 % 7 = -1 truncation towards zero -22 / 7 = -4 -22 % 7 = 6 truncation towards -infinity Both satisfy the required equation (a/b)*b + a%b == a. The second has the advantage that the modulus is always positive -- but they decided on the other (more Fortran-like, less Pascal-like) variant...)

 Tags division, int, integer

 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 indian Programming 4 09-30-2005 08:00 AM lez Linux - Networking 2 09-05-2005 04:00 AM paranoid times Programming 15 03-17-2005 11:06 PM scuzzman Programming 2 11-16-2004 09:34 PM

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

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