toRational problem

Ashley Yakeley ashley@semantic.org
Mon, 16 Apr 2001 19:03:21 -0700


At 2001-04-16 18:35, pablocardenal@laplatavive.com wrote:

>> Why (toRational 0.9) == (9 % 10) is False?

I tried this in Hugs. It gives (toRational 0.9) as 7549747 % 8388608 
(i.e. 37748735/41943040 instead of 37748736/41943040 = 9/10).

This is probably because Hugs attempts a floating-point approximation of 
the string "0.9" before passing it to a 'from' function (fromSingle, 
fromDouble) to cast it to 'Fractional a => a'.

This is a bug in my opinion. The string "0.9" unambiguously represents 
9/10, and Hugs has types for representing such values. Instead, it uses 
floating point.

On a more general note, floating point representations are overused in 
software. It seems like about 90% of the time the programmer should have 
used integer arithmetic over a small 'quantum', and 5% of the time should 
have used rational arithmetic. Floating point is only really appropriate 
when proportional accuracy is needed over a wide range of scales.

-- 
Ashley Yakeley, Seattle WA