Comparing Floating-Point Numbers Is Tricky
bitbashing.io
Comparing Floating-Point Numbers Is Tricky
1–10 of 45 posts
Re: Comparing Floating-Point Numbers Is Tricky
#2Re: Comparing Floating-Point Numbers Is Tricky
#3pg1:joel=#* select 1/7::numeric7; ?column? ------------------------ 0.99999999999999999998 (1 row)
pg1:joel=# select 1/7::float*7; ?column? ---------- 1 (1 row)
Re: Comparing Floating-Point Numbers Is Tricky
#4Specifically: when you have a less-than relationship "<", then !(a<b) && !(b<a) implies that a and b are equal (a==b). And if a==b and b==c then it must be the case that a==c, or the requirements of the ordering predicate are not met. Unfortunately, under most of these FP comparison schemes, for numbers a and b that are "close but not too close", it's the case that a<b, but for x=(a+b)/2, a==x and x==b!
Re: Comparing Floating-Point Numbers Is Tricky
#5Can 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)
Numeric types are meant to be exact, so they will be represented as an exact value.
Re: Comparing Floating-Point Numbers Is Tricky
#6Formatting note for the author: on Safari Mac, something is causing ff and fl ligatures to be applied even to the monospaced code, which makes it look kind of weird.
Re: Comparing Floating-Point Numbers Is Tricky
#7If you're ever doing mathematical calculations of any sort, it is a good practice to have a handle on the scale your numbers will lie within. If not just to be a better professional, it helps you choose an ϵ that matches.
Re: Comparing Floating-Point Numbers Is Tricky
#8Can 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)
Re: Comparing Floating-Point Numbers Is Tricky
#9Formatting note for the author: on Safari Mac, something is causing ff and fl ligatures to be applied even to the monospaced code, which makes it look kind of weird.
Is it better now?
Re: Comparing Floating-Point Numbers Is Tricky
#10http://blogs.mathworks.com/cleve/2014/07/07/floating-point-n...