[GHC] #10378: min/max for Double/Float instances are incorrect

GHC ghc-devs at haskell.org
Mon May 4 18:13:15 UTC 2015


#10378: min/max for Double/Float instances are incorrect
-------------------------------------+-------------------------------------
        Reporter:  lerkok            |                   Owner:
            Type:  bug               |                  Status:  new
        Priority:  high              |               Milestone:
       Component:  Compiler          |                 Version:  7.10.1
      Resolution:                    |                Keywords:
Operating System:  Unknown/Multiple  |            Architecture:
 Type of failure:  None/Unknown      |  Unknown/Multiple
      Blocked By:                    |               Test Case:
 Related Tickets:                    |                Blocking:
                                     |  Differential Revisions:
-------------------------------------+-------------------------------------
Description changed by lerkok:

Old description:

> This is similar to many other numeric issues around `Double`s and
> `Float`s.
>
> The IEEE754 requires `min` and `max` on floats to return the "other"
> number, if one of the arguments is `NaN`. The default definitions used in
> Haskell do not satisfy this property.
>
> Unfortunately, the IEEE-spec isn't exactly clear on how min/max should
> work around +0/-0. It's deliberately underspecified. Most Intel
> implementations simply return the second argument in this case. So, the
> following would be the "reference" implementation, assuming we map to the
> Intel CPU instructions for floating-point min/max:
>
> {{{#!hs
> max x y
>     | isNaN x                               = y
>     | isNaN y                               = x
>     | (x == 0) && (y == 0)                  = y
>     | x > y                                 = x
>     | True                                  = y
>
> min x y
>     | isNaN x                               = y
>     | isNaN y                               = x
>     | (x == 0) &&& (y == 0)                 = y
>     | x < y                                 = x
>     | True                                  = y
> }}}

New description:

 This is similar to many other numeric issues around `Double`s and
 `Float`s.

 The IEEE754 requires `min` and `max` on floats to return the "other"
 number, if one of the arguments is `NaN`. The default definitions used in
 Haskell do not satisfy this property.

 Unfortunately, the IEEE-spec isn't exactly clear on how min/max should
 work around +0/-0. It's deliberately underspecified. Most Intel
 implementations simply return the second argument in this case. So, the
 following would be the "reference" implementation, assuming we map to the
 Intel CPU instructions for floating-point min/max:

 {{{#!hs
 max x y
     | isNaN x                               = y
     | isNaN y                               = x
     | (x == 0) && (y == 0)                  = y   -- takes care of +/-0
     | x > y                                 = x
     | True                                  = y

 min x y
     | isNaN x                               = y
     | isNaN y                               = x
     | (x == 0) &&& (y == 0)                 = y   -- takes care of +/-0
     | x < y                                 = x
     | True                                  = y
 }}}

--

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10378#comment:7>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list