question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

float128 should have __floor__, __ceil__ and __trunc__

See original GitHub issue

math.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:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Oct 31, 2019

The latter one surprised me.

Your version of numpy is (very) old, that’s fixed in master

0reactions
rossbarcommented, Jul 22, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found