java - what is the reason behind below condition -


this question has answer here:

class magicwithoperators{     public static void main(string[] args) {         float  f = 10.2f;          double d = 10.2;          system.out.println(f==d);     } } 

output: false.

why 10.2f==10.2 false 10.0f==10.0 true?

the reason value 10.2 cannot represented exactly, using either float or double representations. both different approximations 10.2, mathematically not equal.

the reason it's true 10.0f , 10.0 10 can represented in both float , double representations.

here hexadecimal representations of numbers above.

41233333          // 10.2f (float, inexact) 4024666666666666  // 10.2  (double, inexact) 4024666660000000  // 10.2f (widened double, inexact)  41200000          // 10.0f (float, exact) 4024000000000000  // 10.0  (double, exact) 4024000000000000  // 10.0f (widened double, exact) 

the conversions done integer.tohexstring(float.floattointbits()) , long.tohexstring(double.doubletolongbits).


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -