java - Visual Studio Wrong output for float/single operation -
i'm trying understand how floating point operations give different output. tests performed below result 1; vb.net , c# give different output, whereas java gives different output. has compiler , read what every computer scientist should know floating-point arithmetic confusing, can explain in simple language?
vb.net
dim x single = 1.000001 dim y single = 0.000001 dim result = x - y
output: 0.9999999 click here see output same goes c#
also, while watching variable in visual studio, value of result different outputs, trimmed while printing , 7 9's printed(which understood), dont understand how actual result them 0.99999994
update: alright, i'm more interested in how calculation done(removed java stuff)
numbers in visual basic single (or c# float) stored ieee 754-2008. number stored in 32 bits. first bit sign next 8 bits store exponent , next 23 bits store fraction.
first integer part of number converted base 2. fraction converted base 2 , number shifted right exponent matches format:
1.x1..x23 x 2^e
where x1 x23 bits in fraction part , e exponent.
for example 0.25 converted to: 1.0 x 2^-2
note significant digits limited 23 bits.
in example 1.000001 converted 1.<20 zeors>101001... can take first 23 digits (1.<20 zeors>101). 0.000001 can start 23 digits first 1 (which bit 20th) , use exponent -20 , store number in higher precision.
Comments
Post a Comment