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