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
Ashley Yakeley wrote:
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'.
Even though Hugs has this bug, you get the same behaviour even in proper Haskell. The reason is that the type of 0.9 gets defaulted to Double (unless you've changed the default default). -- Lennart
On Tue, Apr 17, 2001 at 08:53:08AM +0200, Lennart Augustsson wrote:
Ashley Yakeley wrote:
At 2001-04-16 18:35, pablocardenal@laplatavive.com wrote:
Why (toRational 0.9) == (9 % 10) is False? ... 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'.
Even though Hugs has this bug, you get the same behaviour even in proper Haskell. The reason is that the type of 0.9 gets defaulted to Double (unless you've changed the default default).
Good point. From ghci: Prelude> 0.9 :: Rational 9 % 10 Prelude> toRational 0.9 8106479329266893 % 9007199254740992 (But hugs does have this bug: 0.9 :: Rational is not 9 % 10.) --Dylan Thurston
Mon, 16 Apr 2001 19:03:21 -0700, Ashley Yakeley <ashley@semantic.org> pisze:
Why (toRational 0.9) == (9 % 10) is False?
Because the type of 0.9 is ambiguous and is being defaulted to Double. You can just write 0.9: it will have type Rational if needed. No need to convert it. You can also add default (Integer, Rational) in the module to let ambiguous numeric types default to Rational. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
participants (4)
-
Ashley Yakeley -
Dylan Thurston -
Lennart Augustsson -
qrczak@knm.org.pl