Re: Newbie question about numbers

"Scott G. Miller" <[email protected]> Wed, 24 Oct 2007 00:17:28 -0700
Newsgroups gmane.comp.java.sisc.user
Message-ID <[email protected]>
On 10/23/07, Scott Hickey <[email protected]> wrote:
> I'm new to Scheme.
>
> In Java, for business math, I declare all my numbers that participate
>  in calculations as BigDecimal.  In Scheme, I have seen that there is the
>  notion of exactness.
>
> I understand that this should evaluate to false because the underlying
>  representation is a double.
>
> #;> (eqv? 1.2 (+ 1.1 0.1))
> #f
>
> It was my impression that putting #e in front of a litteral would for
>  an exact representation, but the statement still fails.
> #;> (eqv? #e1.2 (+ #e1.1 #e0.1))
> #f

The problem is that the conversion from an inexact number to an exact
number, well, isn't exact.  Since the representation of for example
1.2 isn't a precise one, and the result of adding 1.1 to 0.1 may not
also yield precisely 1.2, its likely that that test still fails.  The
only sure way to ensure equivalence in the above expressions is to
start with exact numbers.  This is why for example financial packages
use fractions to represent money.

#;> (eqv? 1200/1000 (+ 1100/1000 100/1000))
#t

   Scott

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/