Quantcast
Channel: Checking if float is equivalent to an integer value in python - Stack Overflow
Browsing latest articles
Browse All 13 View Live
↧

Answer by Mad Physicist for Checking if float is equivalent to an integer...

Python has unlimited integer precision, but only 53 bits of float precision. When you square a number, you double the number of bits it requires. This means that the ULP of the original number is...

View Article


Answer by NoName for Checking if float is equivalent to an integer value in...

Consider using NumPy, they take care of everything under the hood.import numpy as npresult_bool = np.isclose(float1, float2)

View Article

Answer by ndpu for Checking if float is equivalent to an integer value in python

There is is_integer function in python float type:>>> float(1.0).is_integer()True>>> float(1.001).is_integer()False>>>

View Article

Answer by lvc for Checking if float is equivalent to an integer value in python

floats can exactly represent all integers in their range - floating-point equality is only tricky if you care about the bit after the point. So, as long as all of the calculations in your formula...

View Article

Answer by user648852 for Checking if float is equivalent to an integer value...

It is hard to argue with standards. In C99 and POSIX, the standard for rounding a float to an int is defined by nearbyint() The important concept is the direction of rounding and the locale specific...

View Article


Answer by Daniel Stutzbach for Checking if float is equivalent to an integer...

Under the hood, Python's float type is a C double.The most robust way would be to get the nearest integer to num, then test if that integers satisfies the property you're after:import mathdef...

View Article

Answer by Sven Marnach for Checking if float is equivalent to an integer...

Both your implementations have problems. It actually can happen that you end up with something like 4.999999999999997, so using int() is not an option.I'd go for a completely different approach: First...

View Article

Answer by JBernardo for Checking if float is equivalent to an integer value...

You can round your number to e.g. 14 decimal places or less:>>> round(4.999999999999997, 14) 5.0PS: double precision is about 15 decimal places

View Article


Answer by eyquem for Checking if float is equivalent to an integer value in...

I think the module decimal is what you need

View Article


Answer by Paul D. Waite for Checking if float is equivalent to an integer...

Python does have a Decimal class (in the decimal module), which you could use to avoid the imprecision of floats.

View Article

Answer by Dan Roberts for Checking if float is equivalent to an integer value...

You'll want to do the latter. In Programming in Python 3 the following example is given as the most accurate way to comparedef equal_float(a, b): #return abs(a - b) <= sys.float_info.epsilon return...

View Article

Answer by Aurojit Panda for Checking if float is equivalent to an integer...

Python still uses the same floating point representation and operations C does, so the second one is the correct way.

View Article

Checking if float is equivalent to an integer value in python

In Python 3, I am checking whether a given value is triangular, that is, it can be represented as n * (n + 1) / 2 for some positive integer n.Can I just write:import mathdef is_triangular1(x): num = (1...

View Article

Browsing latest articles
Browse All 13 View Live