473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Precision for equality of two floats?

Hi!

When I do simple calculation with float values, they are rarely exactly
equal even if they should be. What is the threshold and how can I change
it?

e.g. "if f1==f2:" will always mean "if abs(f1-f2)<1e-6:"

Anton
Nov 28 '05 #1
7 6298
Anton81 <be*******@gmx.net> wrote:
Hi!

When I do simple calculation with float values, they are rarely exactly
equal even if they should be. What is the threshold and how can I change
it?


Python's builtin floats compare for exact bit-by-bit equality -- no
"threshold". You may want to look at the allclose function in the
Numeric extension package, at least for its specs (it consider both
absolute and relative difference). Extension module gmpy might also
help, since its mpf floating numbers implement a fast reldiff (relative
difference) method.
Alex
Nov 28 '05 #2
"Anton81" wrote:
When I do simple calculation with float values, they are rarely exactly
equal even if they should be. What is the threshold
http://www.lahey.com/float.htm
http://docs.python.org/tut/node16.html
and how can I change it?
you cannot.
e.g. "if f1==f2:" will always mean "if abs(f1-f2)<1e-6:"


use your own equal function, e.g.

def equal(a, b):
return abs(a - b) < 1e-6

if equal(f1, f2):
...

(see the "safe comparision" section of the lahey.com paper
for better ways to test for "approximate equality")

</F>

Nov 28 '05 #3
Anton81 <be*******@gmx.net> writes:
When I do simple calculation with float values, they are rarely exactly
equal even if they should be. What is the threshold and how can I change
it?
Implementation dependent, because floats use an underlying C type, and
there's no portable way to do that.
e.g. "if f1==f2:" will always mean "if abs(f1-f2)<1e-6:"


This is a *bad* idea. What happens if f1=1e-12 and f2=1e-112? Do you
really want to values that 100 orders of magnitude different to
compare as equal? You should use something like "if abs(f1 - f2) <
abs(f1) * 1e-6" (been a long time since I worried about this; there's
probably a better version).

DSepending on why you need it, you might consider using decimals
(introduced in 2.4) instead of floats.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

Nov 28 '05 #4
On Mon, 28 Nov 2005 12:35:16 -0500, Mike Meyer wrote:
e.g. "if f1==f2:" will always mean "if abs(f1-f2)<1e-6:"
This is a *bad* idea. What happens if f1=1e-12 and f2=1e-112? Do you
really want to values that 100 orders of magnitude different to
compare as equal?


Sure, if f1 and f2 represent quantities like "average number of people,
in millions", both should be considered as more or less zero. It is a
little hard to justify treating one millionth of a person as a meaningful
figure, no matter how accurate your model for population growth might be.

For the avoidance of confusion, I am *not* suggesting that == for floats
should have any other behaviour other than the one it has now, merely that
the developer rarely wants to use == for comparing floats.

Floating point equality is usually application-specific. Not only do
the floats themselves have only finite resolution, but often you don't
even care about that full resolution since it simply introduces spurious
accuracy not justified by either your model or your data.
You should use something like "if abs(f1 - f2) <
abs(f1) * 1e-6" (been a long time since I worried about this; there's
probably a better version).


Sometimes you want relative differences, sometimes you care about absolute
differences, and sometimes -- very rarely -- you actually want full-blown
bit-for-bit equality.
--
Steven.

Nov 28 '05 #5
On Mon, 28 Nov 2005 07:58:37 -0800, al***@mail.comcast.net (Alex Martelli) wrote:
Anton81 <be*******@gmx.net> wrote:
Hi!

When I do simple calculation with float values, they are rarely exactly
equal even if they should be. What is the threshold and how can I change
it?


Python's builtin floats compare for exact bit-by-bit equality -- no
"threshold". You may want to look at the allclose function in the

Does "exact bit-by-bit" mean that

a = <floatexpr>
a == <floatexpr>

is 100% guaranteed? I.e., are there implementations where an expression
value might remain in 80-bit representation in the fpu and be rounded
to 64 bits for assignment to a and then not compare equal because the second
a is a rounded 64-bit value compared to the regenerated 80-bit value?

Since cpython bytecodes store expression values on the cpu stack, not
the fpu stack, I assume it's ok there, but is it a potential problem
for optimizers generating machine code? Or is it spec-ed for mandatory
as-if-storing-both-arguments-as-double-before-comparing behaviour?

Just wondering ;-)

Regards,
Bengt Richter
Nov 29 '05 #6

Anton81 wrote:
Hi!

When I do simple calculation with float values, they are rarely exactly
equal even if they should be. What is the threshold and how can I change
it?

e.g. "if f1==f2:" will always mean "if abs(f1-f2)<1e-6:"

Anton


googled for "floating point" "comparison tolerance"
http://www.boost.org/libs/test/doc/c...omparison.html

Nov 29 '05 #7
On Tue, 29 Nov 2005 14:31:46 GMT,
bo**@oz.net (Bengt Richter) wrote:
On Mon, 28 Nov 2005 07:58:37 -0800, al***@mail.comcast.net (Alex Martelli) wrote:
Python's builtin floats compare for exact bit-by-bit equality -- no
"threshold". You may want to look at the allclose function in the

Does "exact bit-by-bit" mean that

a = <floatexpr>
a == <floatexpr> is 100% guaranteed? I.e., are there implementations where an
expression value might remain in 80-bit representation in the fpu and
be rounded to 64 bits for assignment to a and then not compare equal
because the second a is a rounded 64-bit value compared to the
regenerated 80-bit value?


IIRC, if you consider NaNs, then a == a isn't even guaranteed if a might
be a NaN, but that's probably Too Much Information. ;-)

There have been (and may well still be) seemingly endless debates about
this on comp.std.c (even without the possibility of NaNs, for similar
reasons to the one you cite). I don't know if the Lisp and/or IEEE-754
crowds have worked all of this out.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Nov 30 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Thinkit | last post by:
Are there any good libraries of arbitrary precision binary floats? I'd like to specify the mantissa length and exponent range. Hexadecimal output would be best (decimal is disgusting with binary...
15
by: Ladvánszky Károly | last post by:
Entering 3.4 in Python yields 3.3999999999999999. I know it is due to the fact that 3.4 can not be precisely expressed by the powers of 2. Can the float handling rules of the underlying layers be...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
26
by: Alexander Block | last post by:
Hello newsgroup, let's say I have a function like template<class Type> inline bool areEqual(const Type &a, const Type &b) { return ( a == b ); }
7
by: ma740988 | last post by:
I've got an unpacker that unpacks a 32 bit word into 3-10 bits samples. Bits 0 and 1 are dont cares. For the purposes of perfoming an FFT and an inverse FFT, I cast the 10 bit values into doubles....
5
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
23
by: Arnaud Delobelle | last post by:
Hi all, I want to know the precision (number of significant digits) of a float in a platform-independent manner. I have scoured through the docs but I can't find anything about it! At the...
8
by: Grant Edwards | last post by:
I'm pretty sure the answer is "no", but before I give up on the idea, I thought I'd ask... Is there any way to do single-precision floating point calculations in Python? I know the various...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.