From: Pascal B. <pj...@in...> - 2005-07-22 21:39:38
|
michal kabata writes: > I haven't encountered such thing before in clisp, but this migh be old > (but I can't find it in archives). I've tested current cvs build right now. > > [1]> (- 1.1 0.9) > 0.20000005 > > is not something I was expecting to see, it's almost javascript style ;) > It's the same with (- (+ 1 0.1) (- 1 0.1)) and so on. Error gets bigger > for bigger numbers, as one would suspect, but on the other hand some > calculations come out ok. > I've played around with code in ffloat.c - FF_FF_minus_FF(). As far as I > can see, problem seems to be with float_to_FF() "macro". And there's my > problem - my german is very very poor, so it's hard for me to debug code > written and commented in german. Has anyone encountered same thing and > maybe solved it? Or maybe someone here wrote that code? You may want to use rationals instead of floating-point numbers: [232]> (- 11/10 9/10) 1/5 Common Lisp is a _practical_ language. Therefore in some aspects it's very high level (it has rational numbers), but in other aspects it's designed to be efficient on available hardware therefore it is specified to use floating-point numbers. And for good or for evil, the reader syntax 1.1 and 0.9 correspond to floating-point numbers. When you write 1.1 or 0.9, you DON'T get 11/10 or 9/10. You get: 3F8CCCCD and 3F666666 (hex) and when you subtract these two floating point numbers, you get: S Exponent Mantissa 1.1 --> 3F8CCCCD = 0 01111111 (1).00011001100110011001101 = (1).00011001100110011001101 * 2^0 0.9 --> 3F666666 = 0 01111110 (1).11001100110011001100110 = (1).11001100110011001100110 * 2^(-1) = (0).111001100110011001100110 * 2^0 and: (1).00011001100110011001101 - (0).111001100110011001100110 ------------------------------ = 0 .00110011001100110011010 = (1).10011001100110011010000 * 2^(-3) 3E4CCCD0 = 0 01111100 (1).10011001100110011010000 --> 0.20000005 There are various packages that implement reader macros to read decimal numbers as rational instead of floating-point if you need to do financial or other exact computations. -- __Pascal Bourguignon__ http://www.informatimago.com/ Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we. -- Georges W. Bush |