3 ms·
red>> .1 + .2 + .3 = .6 == true red>> .1 + .2 + .3 - .6 == 1.110223024625157e-16 This doesn't feel right to me. I usually would expect equality to
by FreeFull 12y ago
red>> .1 + .2 + .3 = .6
== true
red>> .1 + .2 + .3 - .6
== 1.110223024625157e-16
This doesn't feel right to me. I usually would expect equality to be a strictly exact thing. This is bound to cause some problems in code that assumes IEEE-754 has been implemented exactly.
- geographomics 12y agoThe language distinguishes between equal and "strictly equal": red>> .1 + .2 + .3 == .6 == false red>> .1 + .2 + .3 = .6 == true I'm not sure if that syntax is the wisest choice, but in the absence of arbitrary precision arithmetic it seems a very good concept, to avoid common IEEE-754 surprises. They appear to be implementing it along these lines: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ http://randomascii.wordpress.com/2012/02/25/comparing-floati...
- draegtun 12y agoIt should be noted that Rebol comes with an arbitrary precision (bignum) arithmetic option via it's money! datatype - http://www.rebol.net/wiki/Money http://www.rebol.net/wiki/Money >> .1 + .2 + .3 == 0.6000000000000001 >> type? 0.1 == decimal! >> $.1 + $.2 + $.3 == $0.6 >> type? $0.1 == money! Red will most likely get this soon but perhaps with a different nomenclature (Red as already renamed decimal! to float!).
- qtxie 12y agoIt also doesn't feel right to me when I learned it for the first time. But if you know that "=" means "almost equal" when doing float comparision, it is very convenient in practical use. For example, if we want to compare two float variables. In Red: if float-1 = float-2 [ ; do something ] In other languages: if (float-1 - float-2) < EPS { // do something } Obviously, Red's way is more natural and readable. :)
- fulafel 12y agoChoosing the epsilon requires numerical analyis expertise or lucky cargo-cuting: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ https://randomascii.wordpress.com/2012/02/25/comparing-float...
- dockimbel 12y agoThank you for reminding us about those great articles! We were using a Ulp-based method only, but Qtxie proceeded with some improvements [1] for close to zero values, as suggested in the article. Thanks again. :-) [1] https://github.com/red/red/pull/986 https://github.com/red/red/pull/986