Re: Newbie question about numbers

"Scott G. Miller" <[email protected]> Wed, 24 Oct 2007 09:09:35 -0700
Newsgroups gmane.comp.java.sisc.user
Message-ID <[email protected]>
On 10/24/07, Scott Hickey <[email protected]> wrote:
> Obviously, 1.2 can be represented exactly using the BigDecimal class. So you're saying that #e doesn't force an different underlying data type than floating point double? In sisc or in Scheme in general?
>

SISC isn't using BigDecimal, and mathematically, 1.2 isn't exact even
in the BigDecimal class, rather, it allows you to constraint the
amount of precision.

#e converts from the inexact number you specified, which is
represented as a double to start, into an exact number.  This yields
an exact number, but not the one you may expect.  This is true of most
programming languages, Scheme included.

> And I should alway represent decimal amounts as fractions to retain exactness in all Schemes?

And in mathematics in general.  The financial industry insists on this
for accuracy.

>
> Finally, if I'm pulling data out of a database using SISCWeb, do I lose exactness for decimal amounts?

Decimal amounts never have exactness on computers, but for most
purposes they tend to keep the precision you expect.  Errors usually
accumulate when you start operating on the numbers.

Scott


> ----- Original Message ----
> From: Scott G. Miller <[email protected]>
> To: Scott Hickey <[email protected]>
> Cc: [email protected]
> Sent: Wednesday, October 24, 2007 2:17:28 AM
> Subject: Re: [Sisc-users] Newbie question about numbers
>
>
> 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/