• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Round for negative nr

 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic