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 7 6339
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
"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 "approximat e equality")
</F>
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.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
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.
On Mon, 28 Nov 2005 07:58:37 -0800, al***@mail.comc ast.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
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
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.comc ast.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.tombstoneze ro.net/dan/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
floats).
|
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 set
from Python so that 3.4 yield 3.4?
Thanks,
Károly
|
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 script to
convert numbers to arbitrary bases. It aims to take any of whole
numbers (python ints, longs, or Decimals), rational numbers (n / m n,
m whole) and floating points (the best I can do for reals), and
convert them to any base between 2 and...
|
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 );
}
|
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. I'm told:
"floats and doubles have bits for mantissa and exponent. (I knew that).
If you are subtract bits that partly belong to mantissa and partly
belong to exponent, that hardly makes sense. Anyway, looking on bits
only, a float has 32...
| |
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 purpose, that
isn't enough.
I was reading a book about some of the financial scandals of the
1990s called "Inventing Money: The Story of Long-Term Capital Management
and the Legends Behind it" by Nicholas Dunbar. On page 95, he mentions
that...
|
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 behaviour, which is
returning a value which is, really, id({}) < id().
He also said that default equality comparison will continue to be
identity-based. This means that x == y will never raise an exception,
as is the situation is now. Here's his reason:
|
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 moment I use this terrible substitute:
FLOAT_PREC = repr(1.0/3).count('3')
|
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 array modules generally support arrays of
single-precision floats. I suppose I could turn all my
variables into single-element arrays, but that would be way
ugly...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |