floating-accuracy

Floating-point errors in cube root of exact cubic input

Floating-point errors in cube root of exact cubic input Question: I found myself needing to compute the "integer cube root", meaning the cube root of an integer, rounded down to the nearest integer. In Python, we could use the NumPy floating-point cbrt() function: import numpy as np def icbrt(x): return int(np.cbrt(x)) Though this works most …

Total answers: 1

tuple to numpy, data accuracy

tuple to numpy, data accuracy Question: When I convert a tuple to numpy, there is a problem with data accuracy. My code is like this: import numpy as np a=(0.547693688614422, -0.7854270889025808, 0.6267478456110592) print(a) print(type(a)) tmp=np.array(a) print(tmp) The result is like this: (0.547693688614422, -0.7854270889025808, 0.6267478456110592) <class ‘tuple’> [ 0.54769369 -0.78542709 0.62674785] Why? Asked By: June-Solstice || …

Total answers: 3

Why does the floating-point value of 4*0.1 look nice in Python 3 but 3*0.1 doesn't?

Why does the floating-point value of 4*0.1 look nice in Python 3 but 3*0.1 doesn't? Question: I know that most decimals don’t have an exact floating point representation (Is floating point math broken?). But I don’t see why 4*0.1 is printed nicely as 0.4, but 3*0.1 isn’t, when both values actually have ugly decimal representations: …

Total answers: 4

Why `0.4/2` equals to `0.2` meanwhile `0.6/3` equals to `0.19999999999999998` in python?

Why `0.4/2` equals to `0.2` meanwhile `0.6/3` equals to `0.19999999999999998` in python? Question: I know these are float point division. But why did these two formula behave differently? And I did some more investigation, the result confusing me even more: >>>0.9/3 0.3 >>>1.2/3 0.39999999999999997 >>>1.5/3 0.5 What’s the logic here to decide whether the result …

Total answers: 3

Is floating point arbitrary precision available?

Is floating point arbitrary precision available? Question: Just for fun and because it was really easy, I’ve written a short program to generate Grafting numbers, but because of floating point precision issues it’s not finding some of the larger examples. def isGrafting(a): for i in xrange(1, int(ceil(log10(a))) + 2): if a == floor((sqrt(a) * 10**(i-1)) …

Total answers: 5

negative zero in python

negative zero in python Question: I encountered negative zero in output from python; it’s created for example as follows: k = 0.0 print(-k) The output will be -0.0. However, when I compare the -k to 0.0 for equality, it yields True. Is there any difference between 0.0 and -0.0 (I don’t care that they presumably …

Total answers: 7