Comparing Floating-Point Numbers Is Tricky
21–30 of 45 posts
Re: Comparing Floating-Point Numbers Is Tricky
#22Comparing floating point numbers for equality us tricky. In fact it is a classic fool's errand. Comparing for lesser or greater is not tricky.
Re: Comparing Floating-Point Numbers Is Tricky
#23Comparing floating point numbers for equality us tricky. In fact it is a classic fool's errand. Comparing for lesser or greater is not tricky.
Why is that so ? If you can do a b reliably, you can get equality as !(a<b || b<a). Inequality has the same issues for very close values.
Re: Comparing Floating-Point Numbers Is Tricky
#24Comparing floating point numbers for equality us tricky. In fact it is a classic fool's errand. Comparing for lesser or greater is not tricky.
Why is that so ? If you can do a b reliably, you can get equality as !(a<b || b<a). Inequality has the same issues for very close values.
Re: Comparing Floating-Point Numbers Is Tricky
#25Earlier quoted context omitted.
Yes, though I'm specifically talking about when you decide to define a 'bool less_than(double, double)' that uses some kind of fuzzy comparison approach internally. This can affect any platform, not just one with the "bug #323" behavior in it.
You don't need (and shouldn't do) any kind of fuzzy comparison for less than for floats.
Re: Comparing Floating-Point Numbers Is Tricky
#26Can someone explain why float is "better" than numeric in this example? pg1:joel=#* select 1/7::numeric 7; ?column? ------------------------ 0.99999999999999999998 (1 row) pg1:joel=# select 1/7::float*7; ?column? ---------- 1 (1 row)
Floating point math is a bit fuzzy, so many languages will round to the nearest integer if the float is within a certain margin. Numeric types are meant to be exact, so they will be represented as an exact value.
What language does this? It is more likely that it is printing out a truncated form but the number still contains that small difference, such as it will print "1.1" even though 1.1 isn't exactly representable in binary. This comes from the often used Grisu set of algorithms for printing which (in Grisu3) prints the smallest string that can represent the floating point bit pattern.
Re: Comparing Floating-Point Numbers Is Tricky
#27Comparing floating point numbers for equality us tricky. In fact it is a classic fool's errand. Comparing for lesser or greater is not tricky.
(The above bug is a restatement of bubblethink's sibling comment; it's an inequality where the two values are extremely (exactly!) close.)
The Go bug on the lack of a round function had several broken implementations, none of which had a direct equality[1].
Re: Comparing Floating-Point Numbers Is Tricky
#28What I would say is that when you're considering comparison of floating point numbers, it's important to understand what the operation means in terms of the data which you're representing using the floating point numbers, in other words what does it mean in terms of the data for two values to be equal or not equal. Usually there is a precision inherent in the data itself which will guide you to how to formulate equality, if necessary.
Re: Comparing Floating-Point Numbers Is Tricky
#29Earlier quoted context omitted.
You don't need (and shouldn't do) any kind of fuzzy comparison for less than for floats.
You sure might imagine you need to. For instance, suppose you want to average pieces of data that are timestamped "almost the same", but could arrive to be processed with varying delays, including out of order arrival (so you can't just say "is this datum at 'about the same time as' the datum received just prior"). There are better approaches, but the one I inherited involved using a std::map which used a fuzzy less…
Re: Comparing Floating-Point Numbers Is Tricky
#30Earlier quoted context omitted.
Floating point math is a bit fuzzy, so many languages will round to the nearest integer if the float is within a certain margin. Numeric types are meant to be exact, so they will be represented as an exact value.
> so many languages will round to the nearest integer if the float is within a certain margin What language does this? It is more likely that it is printing out a truncated form but the number still contains that small difference, such as it will print "1.1" even though 1.1 isn't exactly representable in binary. This comes from the often used Grisu set of algorithms for printing which (in Grisu3) prints the smallest…