简体   繁体   中英

Why numpy 1.14 rounds float16 65504 to 65500

I run the following code with numpy 1.14.2 on Windows

print(np.array(65504, dtype=np.float16))

I get the result 65500.0 .

But I run the finfo(np.float16) I get the following output:

Machine parameters for float16
---------------------------------------------------------------
precision =   3   resolution = 1.00040e-03
machep =    -10   eps =        9.76562e-04
negep =     -11   epsneg =     4.88281e-04
minexp =    -14   tiny =       6.10352e-05
maxexp =     16   max =        6.55040e+04
nexp =        5   min =        -max
---------------------------------------------------------------

It shows the max is 65504. And I run the same code with numpy 1.13, it prints 65504.0.

What am I missing with numpy 1.14?

In NumPy 1.14, the floating-point stringification algorithm has been changed to print the shortest representation that still rounds to the original value, like regular Python floats.

"Shortest" here is in terms of significant figures, so 65500.0 is considered shorter than 65504.0. Since np.float16('65500.0') produces the same value, 65500.0 is the representation that gets printed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM