Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #19222: RIF: fix min/max when one of the arguments is a NaN
Browse files Browse the repository at this point in the history
Fix min/max when one of the arguments is a NaN conforming to
IEEE-754-2008

URL: http://trac.sagemath.org/19222
Reported by: mmezzarobba
Ticket author(s): Marc Mezzarobba
Reviewer(s): Clemens Heuberger
  • Loading branch information
Release Manager authored and vbraun committed Sep 24, 2015
2 parents fd52571 + 263a6ca commit 973504c
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/sage/rings/real_mpfi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3907,6 +3907,16 @@ cdef class RealIntervalFieldElement(sage.structure.element.RingElement):
sage: min(RIF(-1, 1), RIF(-100, 100)).endpoints()
(-1.00000000000000, 1.00000000000000)
Note that calls involving NaNs try to return a number when possible.
This is consistent with IEEE-754-2008 but may be surprising. ::
sage: RIF('nan').min(2, 1)
1
sage: RIF(-1/3).min(RIF('nan'))
-0.3333333333333334?
sage: RIF('nan').min(RIF('nan'))
[.. NaN ..]
.. SEEALSO::
:meth:`~sage.rings.real_mpfi.RealIntervalFieldElement.max`
Expand All @@ -3932,7 +3942,11 @@ cdef class RealIntervalFieldElement(sage.structure.element.RingElement):
else:
other = self._parent(_other)

if mpfr_cmp(&result.value.right, &other.value.left) <= 0:
if result.is_NaN():
result = other
elif other.is_NaN():
pass
elif mpfr_cmp(&result.value.right, &other.value.left) <= 0:
pass
elif mpfr_cmp(&other.value.right, &result.value.left) <= 0:
result = other
Expand Down Expand Up @@ -3997,6 +4011,16 @@ cdef class RealIntervalFieldElement(sage.structure.element.RingElement):
sage: max(RIF(-1, 1), RIF(-100, 100)).endpoints()
(-1.00000000000000, 1.00000000000000)
Note that calls involving NaNs try to return a number when possible.
This is consistent with IEEE-754-2008 but may be surprising. ::
sage: RIF('nan').max(1, 2)
2
sage: RIF(-1/3).max(RIF('nan'))
-0.3333333333333334?
sage: RIF('nan').max(RIF('nan'))
[.. NaN ..]
.. SEEALSO::
:meth:`~sage.rings.real_mpfi.RealIntervalFieldElement.min`
Expand All @@ -4022,7 +4046,11 @@ cdef class RealIntervalFieldElement(sage.structure.element.RingElement):
else:
other = self._parent(_other)

if mpfr_cmp(&result.value.right, &other.value.left) <= 0:
if result.is_NaN():
result = other
elif other.is_NaN():
pass
elif mpfr_cmp(&result.value.right, &other.value.left) <= 0:
result = other
elif mpfr_cmp(&other.value.right, &result.value.left) <= 0:
pass
Expand Down

0 comments on commit 973504c

Please sign in to comment.