wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Round for negative nr Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Round for negative nr" Watch "Round for negative nr" New topic
Author

Round for negative nr

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Round algorithm is to ad 0.5 and truncate to the nearest integer(equivalent).
This algorithm works strange with negative nr.
By ex:
x=-2.4 => -2.4+0.5 = -1.9 => trunc(-1.9)=-1.But this wrong Math.round(-2.4)=-2;
but
x=-2.5=>-2.5 + 0.5=-2 => trunc(-2)=-2 this is ok!
That means(for negative values) if the number after decimal point is greater or equal with 5 the algorthm is
value+.5 and truncate else
-(value +.5 ) and truncate
I try this with code & it seams to work-> that means at least from the practically part I am right.Can be tat the round has (for negative values) two algorithms?


SCJP, SCJD, SCWCD, OCPJBCD
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

My problem is with the statement "truncate to the nearest integer".
the word "truncate" means to "cut off", i.e. drop the stuff to the right of the decimal. if i truncate 1.999999, i get 1.
"to the nearest integer" mean "look at the decimal part, and use that to decide where to go". 1.99999 to the nearest integer is 2.
Your alorithm doesn't make sense to me. I'm not saying it's wrong, just that I don't understand it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

Now that i've a) had some coffee, and b) done a little research, here's what i've found..
the algorithm should be "floor(x + 0.5"). "floor" is the operation that says "take the largest integer LESS THAN or equal to the value in question. there is a similar function called "ceiling", which is the smallest number greated than or equal to the number in question. there are good examples here:
Floor/Ceiling examples
so..
x=-2.4 => -2.4+0.5 = -1.9 => floor(-1.9)=-2. (-1 is not less than -1.9, -2 is!!!).
x=-2.5=>-2.5 + 0.5=-2 => floor(-2)=-2. this again works.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Math.round() adds .5, then takes the Math.floor() of the result, which will return the integer that is closest to positive infinty and not greater than the number.
ie:
-2.4 should round to -2
-2.4 + .5 = -1.9
integer that is closest to +inf and not greater than -1.9 = -2
-2.5 should round to -2
-2.5 + .5 = -2.0
-2.0 is already == -2 and falls under the special case where the argument is == to an integer, the number is returned as an int: -2
-2.6 should round to -3
-2.6 + .5 = -2.1
integer that is closest to +inf and not greater than -2.1 = -3
Except for the special cases (there are others detailed in the javadoc for Math.round() and Math.floor()) there is only one procedure for any argument.
Hope that helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Round for negative nr