guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: inexact->exact in 1.7.1


From: Marius Vollmer
Subject: Re: inexact->exact in 1.7.1
Date: Fri, 15 Apr 2005 19:36:23 +0200
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3 (gnu/linux)

Rick Taube <address@hidden> writes:

> this behavior of inexact->exact in guile 1.7.1 causes my program to
> become confused:
>
> guile> (inexact->exact 48.0)
> 48
> guile> (inexact->exact (* .1 480.0))
> 48
> guile> (inexact->exact (* (- 1.1 1) 480.0))
> 3377699720527875/70368744177664

This happens because Guile now has exact rationals.  This NEWS entry
explains the behavior:

    ** inexact->exact no longer returns only integers.

    Without exact rationals, the closest exact number was always an
    integer, but now inexact->exact returns the fraction that is exactly
    equal to a floating point number.  For example:

        (inexact->exact 1.234)
        => 694680242521899/562949953421312

    When you want the old behavior, use 'round' explicitely:

        (inexact->exact (round 1.234))
        => 1


In your case, "1.1" is not exactly 11/10 since IEEE double can not
represent that number exactly.  You can write "#e1.1" instead, if you
want an exact 11/10:

    guile> #e1.1
    11/10
    guile> (inexact->exact (* (- #e1.1 1) #e480.0))
    48

A IEEE double can not represent 0.1 exactly either, and you just got
lucky that (* 0.1 480.0) is exactly 48:

guile> (- (* 0.1 480.0)
          (* (- 1.1 1) 480.0))
-4.2632564145606e-14




reply via email to

[Prev in Thread] Current Thread [Next in Thread]