• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Math class question

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if we use
System.out.print(Math.max(99.9f,99.9));
then it prints 99.9000015258789

and if we use
System.out.print(Math.max(99.9,99.9))
or
System.out.println(Math.max(99.9f,99.9f))
then it prints 99.9

what's the reason pl's explain me
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it is impossible to represent .9 in a floating point number, what you are seeing are rounded results printed by the print method. The print method does not print the exact value of floating point numbers.
As it turns out, 99.9f is a bigger number than 99.9d.
Try this:
System.out.print(new java.math.BigDecimal(Math.max(99.9,99.9)));
reply
    Bookmark Topic Watch Topic
  • New Topic