float128 should have __floor__, __ceil__ and __trunc__
See original GitHub issuemath.floor()
and math.ceil()
can produce incorrect result for float128
due to double rounding. math.trunc()
doe not work with float128
(but int()
works).
Reproducing code example:
>>> import numpy as np
>>> import math
>>> x = np.float128('0.99999999999999999995')
>>> x < 1
True
>>> np.floor(x)
0.0
>>> math.floor(x)
1
>>> np.trunc(x)
0.0
>>> y = np.float128('1.0000000000000000001')
>>> y > 1
True
>>> np.ceil(y)
2.0
>>> math.ceil(y)
1
>>> z = np.float128()
>>> np.trunc(z)
0.0
>>> math.trunc(z)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type numpy.float128 doesn't define __trunc__ method
>>> int(z)
0
This can be solved by implementing __floor__
, __ceil__
and __trunc__
in float128
.
Numpy/Python version information:
1.13.3 3.6.8 (default, Oct 7 2019, 12:59:55) [GCC 8.3.0]
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Does OCaml have C-like round() and trunc() functions?
However it can be expressed more succinctly (and efficiently) with floor ... If you want a function truncate having the type float ->...
Read more >Math.ceil, floor, and trunc Examples - LaunchCode Education
This method rounds a decimal value UP to the next integer (hence the ceiling reference in the name). Integer values remain the same....
Read more >Math::Float128 - metacpan.org
Returns a Math::Float128 object to which the numeric value of $arg. has been assigned. If no arg is supplied then $f will be...
Read more >Issue 38639: Optimize floor(), ceil() and trunc() for floats
Currently math.floor(), math.ceil() and math.trunc() look up special ... We should also check how this optimization affects other types: $ .
Read more >Precision of floating point numbers in C++ (floor(), ceil(), trunc ...
For float values in C++ this precision is set to 6-7 digit after that if the decimal recurs it will discard the value....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Your version of numpy is (very) old, that’s fixed in master
Closing as duplicate to keep the discussion centralized in #13375. Please add any additional comments (including a potentially a summary of the discussion here) to that thread.