Path: utzoo!mnetor!uunet!husc6!necntc!ima!think!barmar From: barmar@think.COM (Barry Margolin) Newsgroups: comp.lang.lisp Subject: Re: Float/rational comparison Message-ID: <20062@think.UUCP> Date: 21 Apr 88 05:19:32 GMT References: <888@cresswell.quintus.UUCP> Sender: usenet@think.UUCP Reply-To: barmar@fafnir.think.com.UUCP (Barry Margolin) Organization: Thinking Machines Corporation, Cambridge, MA Lines: 43 The reason for floating-point contagion is that floating point numbers are not considered exact, while rationals are. It is therefore not proper to automatically convert floats to rationals, because you would be fooling yourself if you believe that the float actually represented that exact rational. A computation is only as precise as its least exact constituent. This is why the rule is called "contagion": floating point numbers infect rationals with their inexactness, and it spreads like a disease through the entire calculation. Let's consider your example, (> (/ big-integer (1+ (* big-integer 2))) 0.5) Where did that 0.5 come from in the first place? It would be silly for it to have been a literal in the program, because the programmer could have said what he meant by using 1/2 instead, without injecting imprecision into the program. Floating point literals should only be used when a computation is being done all in floating point, or when the value is irrational (e.g. pi, e). Therefore, it must have come from some floating point computation. For instance, it could have been the result of (/ big-float (1+ (* big-float 2))) Consider the case where big-float is (float big-integer). By the "usual arithmetic" rules one would expect the above comparison to be false, because they should be =. In order for this to happen, the rational must be converted to a float. The sentence that said "the usual arithmetic tests are performed" does not appear to be defining the behavior, but simply describing the way the comparison functions are used. In general, you cannot expect floating point numbers to always obey the usual arithmetic rules. For example, (> (/ 9.001 10) .9) is not guaranteed to return T, so why would you expect any more of (> 9001/10000 10)? Barry Margolin Thinking Machines Corp. barmar@think.com uunet!think!barmar