unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: Mathieu Lirzin <mthl@gnu.org>
Cc: 30953@debbugs.gnu.org
Subject: bug#30953: ‘min’ and ‘max’ behavior when mixing exact and inexact numbers.
Date: Mon, 26 Mar 2018 22:21:25 -0400	[thread overview]
Message-ID: <87bmfa45sq.fsf@netris.org> (raw)
In-Reply-To: <87sh8nkjuy.fsf@gnu.org> (Mathieu Lirzin's message of "Mon, 26 Mar 2018 16:11:01 +0200")

tags 30953 + notabug
close 30953
thanks

Hi Mathieu,

Mathieu Lirzin <mthl@gnu.org> writes:

> I am observing a unexpected behavior of ‘min’ and ‘max’:
>
> --8<---------------cut here---------------start------------->8---
> scheme@(guile-user)> (min 1 2.4)
> $2 = 1.0
> scheme@(guile-user)> (min 1/2 4.0)
> $7 = 0.5
> scheme@(guile-user)> (max 4 3.5)
> $4 = 4.0
> --8<---------------cut here---------------end--------------->8---
>
> I would expect the results to be integers instead.

1.0 and 4.0 _are_ integers, in the terminology of both Scheme and
mathematics.  However, they are _inexact_ integers.

I'm not sure why you would expect (min 1/2 4.0) to return an integer.

By the result of exactness propagation in Scheme, these results must be
inexact, because the results depend on the value of an inexact argument.
For example, in (min 1 2.4), if the 2.4 were replaced with 0.8, then the
result would be 0.8 instead of 1.0.  It's entirely possible that the
inexact arithmetic leading to 2.4 might have a maximum error greater
than 1.4.

The idea is that if Scheme tells you that the result of some computation
is exact, then it could in principle be proved to be the true
mathematical result, and therefore known to be unaffected by any inexact
computation.

So, (min 1 2.4) could only return an exact 1 if it were somehow known
that the 2.4 has a maximum error of 1.4.  I suspect you are thinking to
yourself "the 2.4 might be imprecise, but surely it's not so far off to
affect the result here."  However, there's no upper bound on the error
of an inexact number, when one considers the entire history of inexact
operations that led to it.

For details, see R5RS section 6.2.2 (Exactness), R6RS section 11.7.1
(Propagation of exactness and inexactness), and R7RS section 6.2.2
(Exactness).

> AIUI the
> implementation of the ‘min’ procedure should to be equivalent to:
>
>   (define (min val . rest)
>     (let loop ((x val) (other rest))
>       (match other
>         (() x)
>         ((y . rest) (loop (if (< x y) x y) rest)))))

This would violate the exactness propagation rules.

> Maybe there is a good performance reason for the current behavior.  If
> that's the case then it should be specified in the manual that exact
> numbers are converted to real numbers when at least one of the arguments
> is inexact.

In Scheme (and mathematics), 1 and 1/2 are considered real numbers, and
1.0 is both a real number and an integer.  In Scheme, the only
difference between 1 and 1.0 is that 1 is exact and 1.0 is inexact.

Lesser programming languages say that 1 is not a real number and that
1.0 is not an integer, but from a mathematical point of view, that's
nonsense :)

     Regards,
       Mark





      reply	other threads:[~2018-03-27  2:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-26 14:11 bug#30953: ‘min’ and ‘max’ behavior when mixing exact and inexact numbers Mathieu Lirzin
2018-03-27  2:21 ` Mark H Weaver [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bmfa45sq.fsf@netris.org \
    --to=mhw@netris.org \
    --cc=30953@debbugs.gnu.org \
    --cc=mthl@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).