Floating point bug when writing to file in Matlab? -


im not sure if im missing simple following code fails (a , b meant same):

a=single(2147483584) f=fopen('test','wb'); fwrite(f,a,'int32') fclose(f);  f=fopen('test','rb'); b=fread(f,inf,'int32'); fclose(f) b 

with output:

a =     2.1475e+009 b =    -2.1475e+009 

and following code succeeds:

a=single(2147483583) f=fopen('test','wb'); fwrite(f,a,'int32') fclose(f);  f=fopen('test','rb'); b=fread(f,inf,'int32'); fclose(f) b 

with output:

a =     2.1475e+009 b =    2.1475e+009 

does know why?

i don't know matlab well, seems clear what's happening here. you're converting a float , storing result of conversion 32-bit signed integer. nearest single-precision ieee 754 float integer 2147483584 2147483648.0, or 2**31. 32-bit integer can represent values in range [-2**31, 2**31-1], looks though when write value integer, gets wrapped modulo 2**32 give -2**31 instead of 2**31.

in contrast, nearest single-precision float 2147483583 2147483520.0, does fit in 32-bit integer.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -