• 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

double, float multiplication

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I came across some unexpected behaviour today, regarding double,float and double,double multiplications. I get the values 240000.0 and 239999.99463558197 respectively when the program below is run.



I expected both results to be the same since the float b2, was being cast to a double which would mean that the same underlying calculation would be performed. But I just found out (right now!) that the widening conversion to double of b2 produces 0.07999999821186066 and not 0.08 as I expected. I do not understand why this should be as no calulation is being performed which would introduce the usual float/double error, just a simple widening conversion.

Oh well, I guess I'll stick to doubles for now...
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello... floating point... Ok... the value you have in your 32 bits floating float 0.08 is not actually 0.08, it's a value close to 0.08. So when you cast it in a double it become less close to 0.08 because a double doesn't have the same repersentation of deciaml than a float. I was asking me the same question couple years ago for my programmer certification and I found this answer here: http://docs.sun.com/source/806-3568/ncg_goldberg.html

Alain Boucher
reply
    Bookmark Topic Watch Topic
  • New Topic