[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Float numbers equality.
- From: Matthias Kluwe <mkluwe@...>
- Date: Fri, 9 Aug 2013 21:37:39 +0200
Hi!
2013/8/9 Karol Dro <karoldro@gmail.com>:
> I'm writing some unit tests, where I compare numbers and I have problem with
> something like this:
> 300 * 0.07 == 21 => false.
> print (300 * 0.07, 21, 300 * 0.07 == 21)
>
> What is the best way, for test float equality?
>
> I consider to use tostring function:
> print (300 * 0.07, 21, tostring (300 * 0.07) == tostring (21)),
> But i wonder, maybe there is a better way, to compare something like this?
In short: Use a reasonable "epsilon".
But excuse me for mentioning the usual hint: Do a search for "What
Every Computer Scientist Should Know About Floating-Point Arithmetic".
The document may be a bit lengthy and answer a few more questions than
you have, but it tells the basic characteristics of floating point
arithmetics including important points as cancellation and the IEEE
standard.
Regards,
Matthias