• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Math.round() question for negative numbers

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In Chapter 6 of K&B book, it is mentioned that "If the number after the decimal point is less than 0.5, Math.round() is equal to Math.floor(). If the number after the decimal point is greater than or equal to 0.5, Math.round()is equal to Math.ceil()".
Please look at the following code:

But I surprised to see the value of -10.8 is rounding to -11 which is a lower value than -10.8. The above rule seems to be okay with positive numbers, but does anyone explain what needs to be interpreted in case of negative numbers?
Thanks in advance.
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The following approach is useful for Math.round()
if a is double
(long)Math.floor(a + 0.5d)
if a is float
(int)Math.floor(a + 0.5f)

Math.round (-10.8)
= (long) Math.floor (-10.8 + 0.5)
= (long) Math.floor (-10.3)
= (long) (-11.0)
= -11
Hope this helps!!
[ November 09, 2003: Message edited by: Cathy Song ]
[ November 09, 2003: Message edited by: Cathy Song ]
 
Durgaprasad Guduguntla
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cathy for quick reply. Your approach holds good for both negative and positive numbers. But rounding of a negative number to a lower value rather than to a next higher value looks strange behavior.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don�t know the reason why they chose to make the half way point round the way it does, but this is the way I see it: I draw a number line.

-3.5 and 3.5 both round to the RIGHT.
Otherwise the values between two whole numbers round to the nearest whole number.
[ November 09, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Durgaprasad Guduguntla:
But rounding of a negative number to a lower value rather than to a next higher value looks strange behavior.



Durga
Rounding gives the "closest" integer (this is by definition, strange or not). In case there's a "tie", the higher integer is chosen, eg.
rounding -3.5 gives -3.0 (as Marlene explained above), and mathematically, -3.0 > -3.5.
Cheers
Harwinder
[ November 09, 2003: Message edited by: Harwinder Bhatia ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, so that is how you handle these extraordinarilylongnames. You know how to truncate them. Durga.
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic