unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#30426: division inconsistency?
@ 2018-02-11 22:56 bil
  2018-02-12 21:01 ` Mark H Weaver
  0 siblings, 1 reply; 7+ messages in thread
From: bil @ 2018-02-11 22:56 UTC (permalink / raw)
  To: 30426

A possible inconsistency:

scheme@(guile-user)> (version)
$1 = "2.0.13"

scheme@(guile-user)> (/ 1 (* 0 +nan.0))
$2 = +nan.0
scheme@(guile-user)> (/ 1 0 +nan.0)
<unnamed port>:3:0: In procedure #<procedure 5557c36be9c0 at <current 
input>:3:0 ()>:
<unnamed port>:3:0: Throw to key `numerical-overflow' with args `("/" 
"Numerical overflow" #f #f)
scheme@(guile-user)> (/ 1 +nan.0 0)
<unnamed port>:5:0: In procedure #<procedure 55ff47f4ad00 at <current 
input>:5:0 ()>:
<unnamed port>:5:0: Throw to key `numerical-overflow' with args `("/" 
"Numerical overflow" #f #f)

scheme@(guile-user)> (* +nan.0 0)
$1 = +nan.0
scheme@(guile-user)> (/ 1 +nan.0)
$2 = +nan.0

similarly with +inf.0:

scheme@(guile-user)> (/ 1 (* +inf.0 0))
$3 = +nan.0
scheme@(guile-user)> (/ 1 +inf.0 0)
<unnamed port>:6:0: In procedure #<procedure 5557c36f7740 at <current 
input>:6:0 ()>:
<unnamed port>:6:0: Throw to key `numerical-overflow' with args `("/" 
"Numerical overflow" #f #f)







^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#30426: division inconsistency?
  2018-02-11 22:56 bug#30426: division inconsistency? bil
@ 2018-02-12 21:01 ` Mark H Weaver
  2018-02-12 21:15   ` Mark H Weaver
  0 siblings, 1 reply; 7+ messages in thread
From: Mark H Weaver @ 2018-02-12 21:01 UTC (permalink / raw)
  To: bil; +Cc: 30426

Hi,

bil@ccrma.Stanford.EDU writes:

> scheme@(guile-user)> (help '/)
> - Scheme Procedure: / [x [y .  rest]]
>      Divide the first argument by the product of the remaining
>      arguments.  If called with one argument Z1, 1/Z1 is returned.

This help text is indeed incorrect.  In fact, (/ x y z) is evaluated as
(/ (/ x y) z), which is not necessarily the same as (/ x (* y z)) when
using inexact arithmetic.

> A possible inconsistency:
>
> scheme@(guile-user)> (version)
> $1 = "2.0.13"
>
> scheme@(guile-user)> (/ 1 (* 0 +nan.0))
> $2 = +nan.0

Many years ago, I concluded that (* 0 +nan.0) and (* 0 +inf.0) should be
+nan.0, and that's what Guile does.  I now believe that (* 0 <anything>)
should be 0.  Both of these behaviors are allowed by the R6RS.  Note
that when I write '0', I mean an _exact_ zero.  So, the above expression
may raise an exception in a future version of Guile.

> scheme@(guile-user)> (/ 1 0 +nan.0)
> <unnamed port>:3:0: In procedure #<procedure 5557c36be9c0 at <current
> input>:3:0 ()>:
> <unnamed port>:3:0: Throw to key `numerical-overflow' with args `("/"
> "Numerical overflow" #f #f)

The R6RS specifies that if all arguments to '/' are exact, then the
divisors must all be nonzero.  That does not apply to the above '/' call
because of the inexact +nan.0 argument.  However, our compiler
transforms (/ x y z) to (/ (/ x y) z) in an early pass.

> scheme@(guile-user)> (/ 1 +nan.0 0)
> <unnamed port>:5:0: In procedure #<procedure 55ff47f4ad00 at <current
> input>:5:0 ()>:
> <unnamed port>:5:0: Throw to key `numerical-overflow' with args `("/"
> "Numerical overflow" #f #f)

This should probably return +nan.0, and that's what happens in Guile 2.2
for compiled code.  For some of these edge cases, our compiler has
different behavior than our core numeric procedures.

Our core '/' operator, as defined in numbers.c, raises an exception for
(/ x 0), for any 'x'.  This does not conform to the R6RS, which
specifies that (/ 0.0 0) => +inf.0.  I don't think that rule makes sense
because the sign of the result cannot be justified.  (/ 1 0.0) => +inf.0
and (/ 1 -0.0) => -inf.0 are more justifiable because of the signed
inexact zeroes, but an exact zero is not signed.

However, it may be that we should change this to conform to the R6RS,
and certainly it would be good for our compiler and interpreter to agree
on all of these edge cases.

> scheme@(guile-user)> (* +nan.0 0)
> $1 = +nan.0
> scheme@(guile-user)> (/ 1 +nan.0)
> $2 = +nan.0
>
> similarly with +inf.0:
>
> scheme@(guile-user)> (/ 1 (* +inf.0 0))
> $3 = +nan.0

If we change Guile so that (* 0 x) => 0 for all x, then the expression
above will raise an exception in a future version of Guile.

> scheme@(guile-user)> (/ 1 +inf.0 0)
> <unnamed port>:6:0: In procedure #<procedure 5557c36f7740 at <current
> input>:6:0 ()>:
> <unnamed port>:6:0: Throw to key `numerical-overflow' with args `("/"
> "Numerical overflow" #f #f)

As with the (/ 1 +nan.0 0) case above, this should probably return
+nan.0, and that's what happens in Guile 2.2 for compiled code.

What do you think?

    Regards,
      Mark





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#30426: division inconsistency?
  2018-02-12 21:01 ` Mark H Weaver
@ 2018-02-12 21:15   ` Mark H Weaver
  2018-02-12 21:59     ` bil
  0 siblings, 1 reply; 7+ messages in thread
From: Mark H Weaver @ 2018-02-12 21:15 UTC (permalink / raw)
  To: bil; +Cc: 30426

Mark H Weaver <mhw@netris.org> writes:

> Our core '/' operator, as defined in numbers.c, raises an exception for
> (/ x 0), for any 'x'.  This does not conform to the R6RS, which
> specifies that (/ 0.0 0) => +inf.0.  I don't think that rule makes sense

Sorry, I meant to write (/ 1.0 0) => +inf.0, which is one of the
examples given in the R6RS.

> because the sign of the result cannot be justified.  (/ 1 0.0) => +inf.0
> and (/ 1 -0.0) => -inf.0 are more justifiable because of the signed
> inexact zeroes, but an exact zero is not signed.
>
> However, it may be that we should change this to conform to the R6RS,
> and certainly it would be good for our compiler and interpreter to agree
> on all of these edge cases.

        Mark





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#30426: division inconsistency?
  2018-02-12 21:15   ` Mark H Weaver
@ 2018-02-12 21:59     ` bil
  2018-02-14 20:54       ` Mark H Weaver
  0 siblings, 1 reply; 7+ messages in thread
From: bil @ 2018-02-12 21:59 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: 30426

Thanks very much for the informative answer!  I am not sure what
(* 0 +nan.0) should return.  I lean toward +nan.0 mainly because
I assume NaNs exist to indicate an error somewhere, and you want
that to be returned. For (* 0 +inf.0) I have no druthers.
For (/ 0.0 0) and (/ +nan.0 0) s7 throws a divide-by-zero error,
but I have no good reason for doing this.  In s7, I'd prefer
to say (/ x y ...) is equal to (/ x (* y ...)) in all cases.







^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#30426: division inconsistency?
  2018-02-12 21:59     ` bil
@ 2018-02-14 20:54       ` Mark H Weaver
  2018-02-14 22:10         ` bil
  0 siblings, 1 reply; 7+ messages in thread
From: Mark H Weaver @ 2018-02-14 20:54 UTC (permalink / raw)
  To: bil; +Cc: 30426

bil@ccrma.Stanford.EDU writes:

> Thanks very much for the informative answer!  I am not sure what
> (* 0 +nan.0) should return.  I lean toward +nan.0 mainly because
> I assume NaNs exist to indicate an error somewhere, and you want
> that to be returned.

It's a sensible position.  FWIW, my rationale is that for all exact
integers k, (* k x) => (+ x ...) with k copies of x in the arguments.
By that rule, (* 0 x) => (+).

It's certainly true that NaNs exist to indicate errors, but they might
not be propagated to the final result due to control flow, and for a
typical formal specification of multiplication-by-exact-integer
equivalent to the rule above, the NaN would not be propagated.

More generally, if the result would be the same for _any_ exact real
substituted in place of the NaN, then I believe it's justifiable to drop
the NaN.  R7RS-small states a similar rule in section 6.2.4:

  An arithmetic operation where one operand is NaN returns NaN, unless
  the implementation can prove that the result would be the same if the
  NaN were replaced by any rational number.

> For (* 0 +inf.0) I have no druthers.

> For (/ 0.0 0) and (/ +nan.0 0) s7 throws a divide-by-zero error,
> but I have no good reason for doing this.

For lack of a more comprehensive framework to decide corner cases
involving a mixture of exact and inexact arguments, I've found it useful
to use limits to decide the results of numerical operations involving
inexact zeroes or infinities.  Based on my reading of IEEE 754 and
related articles by Kahan, this is the approach that I've found is most
justifiable and consistent with IEEE 754.

For example, for (<op> 0.0 y ...), I take the limit of (<op> x y ...) as
x approaches zero from above, and similarly for 'x' in other argument
positions.  For -0.0, I take the limit as x approaches zero from below.
Similarly for the infinities.  This rule does not cover cases where more
than one argument is an inexact zero or infinity.

If one accepts this approach, then (/ 0.0 0) is undefined and
(* 0 +inf.0) is 0.

> In s7, I'd prefer to say (/ x y ...) is equal to (/ x (* y ...)) in
> all cases.

FWIW, I believe this is in violation of all RnRS at least as far back as
R3RS, which all specify that '/' returns the quotient of its arguments,
associating to the left.  On the other hand, I've not been shy to
deviate from RnRS when I thought it made sense to do so, so I can't
complain :)

     Regards,
       Mark





^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#30426: division inconsistency?
  2018-02-14 20:54       ` Mark H Weaver
@ 2018-02-14 22:10         ` bil
  2018-02-15  7:35           ` Mark H Weaver
  0 siblings, 1 reply; 7+ messages in thread
From: bil @ 2018-02-14 22:10 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: 30426

But if (* 0 x) is 0, you lose the notion that
(* exact inexact) is inexact.  So (* 0 +inf.0)
should be 0.0 or maybe +nan.0.  Similarly with
+nan.0, I suppose.







^ permalink raw reply	[flat|nested] 7+ messages in thread

* bug#30426: division inconsistency?
  2018-02-14 22:10         ` bil
@ 2018-02-15  7:35           ` Mark H Weaver
  0 siblings, 0 replies; 7+ messages in thread
From: Mark H Weaver @ 2018-02-15  7:35 UTC (permalink / raw)
  To: bil; +Cc: 30426

Hi Bill,

bil@ccrma.Stanford.EDU writes:

> But if (* 0 x) is 0, you lose the notion that
> (* exact inexact) is inexact.  So (* 0 +inf.0)
> should be 0.0 or maybe +nan.0.  Similarly with
> +nan.0, I suppose.

No, because (* 0 x) is equivalent to (+), where the x is not an input.
In fact, this specific case (multiplication by exact 0) is given as an
example where exact 0 may be returned even if the other argument is
inexact, by R4RS, R5RS, R6RS, and R7RS.

R4RS section 6.5.2, and R5RS section 6.2.2 (Exactness), state:

  With the exception of 'inexact->exact', the operations described in
  this section must generally return inexact results when given any
  inexact arguments.  An operation may, however, return an exact result
  if it can prove that the value of the result is unaffected by the
  inexactness of its arguments.  For example, multiplication of any
  number by an exact zero may produce an exact zero result, even if the
  other argument is inexact.

R6RS section 11.7.1 (Propagation of exactness and inexactness) states:

  One general exception to the rule above is that an implementation may
  return an exact result despite inexact arguments if that exact result
  would be the correct result for all possible substitutions of exact
  arguments for the inexact ones.  An example is (* 1.0 0) which may
  return either 0 (exact) or 0.0 (inexact).

R7RS section 6.2.2 (Exactness) states:

  Except for exact, the operations described in this section must
  generally return inexact results when given any inexact arguments.  An
  operation may, however, return an exact result if it can prove that
  the value of the result is unaffected by the inexactness of its
  arguments.  For example, multiplication of any number by an exact zero
  may produce an exact zero result, even if the other argument is
  inexact.

  Specifically, the expression (* 0 +inf.0) may return 0, or +nan.0, or
  report that inexact numbers are not supported, or report that
  non-rational real numbers are not supported, or fail silently or
  noisily in other implementation-specific ways.

I'm quite sensitive to this issue, so sensitive that I decided to change
Guile several years ago so that (* 0 1.0) would return 0.0 instead of 0.
My rationale was that if the 1.0 were replaced by +inf.0 or +nan.0, then
by IEEE 754 the result should be +nan.0, and therefore that the result
was not the same regardless of the value of the inexact argument.  I
didn't care that R[4567]RS specifically gave this as an example where an
exact 0 may be returned, because I judged that it violated the
principles of the exactness propagation, and I don't want to return an
exact result unless it could in principle be _proved_ to be correct.

The new language in R6RS is what changed my mind.  In R6RS, you may
return an exact result if it "would be the correct result for all
possible substitutions of _exact_ arguments for the inexact ones."  So,
we needn't consider what would happen if +inf.0 or +nan.0 were put in
place of the 1.0 in (* 0 1.0), because +inf.0 and +nan.0 are not exact.

I think this is the right principle.  In mathematics, all real numbers
are finite; there are no infinities and no NaNs.  I regard the inexact
infinities as merely inexact representations of very large finite real
numbers.

What do you think?

     Regards,
       Mark





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-02-15  7:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-11 22:56 bug#30426: division inconsistency? bil
2018-02-12 21:01 ` Mark H Weaver
2018-02-12 21:15   ` Mark H Weaver
2018-02-12 21:59     ` bil
2018-02-14 20:54       ` Mark H Weaver
2018-02-14 22:10         ` bil
2018-02-15  7:35           ` Mark H Weaver

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).