• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Casting

 
Greenhorn
Posts: 17
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

OUTPUT is....
1.6666666269302368
1.6666666666666667

why is the value typecast by float and double different like this......
 
lowercase baba
Posts: 13069
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how much do you know about how floating point numbers are stored?
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that casting has a higher precedence than division. A float has about 7 figures' precision and a double about 15-16 figures.
 
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A double gives a slightly more accurate representation of rational and irrational numbers because it has more bits dedicated to its precision than a float does, a double has 52 of its 64 to give precision whereas a float only has 23. The rest of the bits are the exponent and the sign bit. when you do (float) 5/3 the result is cast into a float before being assigned to your double so you lose precision this way. You will always have some degree of error when representing irrational numbers with floats or doubles.
 
fred rosenberger
lowercase baba
Posts: 13069
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernie Mcracken wrote:You will always have some degree of error when representing irrational numbers with floats or doubles.

The original number can be rational, too, and you can still loose precision.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic