Fix prelude definitions of abs/signum for Floats/Doubles

The current definition of "abs" and "signum" for the types Float/Double is not IEEE-754 compliant. To wit: Prelude> abs (-0.0::Float) -0.0 Prelude> signum (-0.0::Float) 0.0 The correct result should be the other way around; abs returning 0.0 and signum returning -0.0 when they receive a negative-zero. The same also holds for the type Double. The issue came up several times in several forums, with general consensus that the behavior should match the IEEE-754 specs. Here're three different discussions on this matter: http://www.haskell.org/pipermail/libraries/2011-January/015761.html http://stackoverflow.com/questions/10395761/absolute-value-of-negative-zero-... http://www.haskell.org/pipermail/haskell-cafe/2013-April/107471.html Proposed fix: Section 6.4.4 of the report http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" definitions for abs/signum; which fails to take into account of negative-zero values for floating-point types. An easy fix would be to add to the report a note on the status of negative-zero for Real/Float instances; with individual implementations explicitly checking for negative-0 first. For instance GHC's implementation can be changed as follows: instance Num Float where signum x | isNegativeZero x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x A similar change would need to be done for the "Num Double" instance as well. Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform. Library guidelines suggest a discussion period of 2 weeks on this issue. I'm hoping that we can resolve the issue in a timely manner, and at least GHC's implementation can match the desired semantics in the next release. If there's consensus at the end of 2-weeks; I'll go ahead and create a corresponding ticket for GHC. Thanks, -Levent.

+1
On Wed, Apr 10, 2013 at 3:24 PM, Levent Erkok
The current definition of "abs" and "signum" for the types Float/Double is not IEEE-754 compliant. To wit:
Prelude> abs (-0.0::Float) -0.0 Prelude> signum (-0.0::Float) 0.0
The correct result should be the other way around; abs returning 0.0 and signum returning -0.0 when they receive a negative-zero. The same also holds for the type Double.
The issue came up several times in several forums, with general consensus that the behavior should match the IEEE-754 specs. Here're three different discussions on this matter:
http://www.haskell.org/pipermail/libraries/2011-January/015761.html
http://stackoverflow.com/questions/10395761/absolute-value-of-negative-zero-... http://www.haskell.org/pipermail/haskell-cafe/2013-April/107471.html
Proposed fix: Section 6.4.4 of the report http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" definitions for abs/signum; which fails to take into account of negative-zero values for floating-point types. An easy fix would be to add to the report a note on the status of negative-zero for Real/Float instances; with individual implementations explicitly checking for negative-0 first. For instance GHC's implementation can be changed as follows:
instance Num Float where signum x | isNegativeZero x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x
A similar change would need to be done for the "Num Double" instance as well. Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform.
Library guidelines suggest a discussion period of 2 weeks on this issue. I'm hoping that we can resolve the issue in a timely manner, and at least GHC's implementation can match the desired semantics in the next release. If there's consensus at the end of 2-weeks; I'll go ahead and create a corresponding ticket for GHC.
Thanks,
-Levent.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
-- Felipe.

+1, would be good for me On Wed, Apr 10, 2013 at 2:27 PM, Felipe Almeida Lessa < felipe.lessa@gmail.com> wrote:
+1
On Wed, Apr 10, 2013 at 3:24 PM, Levent Erkok
wrote: The current definition of "abs" and "signum" for the types Float/Double is not IEEE-754 compliant. To wit:
Prelude> abs (-0.0::Float) -0.0 Prelude> signum (-0.0::Float) 0.0
The correct result should be the other way around; abs returning 0.0 and signum returning -0.0 when they receive a negative-zero. The same also holds for the type Double.
The issue came up several times in several forums, with general consensus that the behavior should match the IEEE-754 specs. Here're three different discussions on this matter:
http://www.haskell.org/pipermail/libraries/2011-January/015761.html
http://stackoverflow.com/questions/10395761/absolute-value-of-negative-zero-...
http://www.haskell.org/pipermail/haskell-cafe/2013-April/107471.html
Proposed fix: Section 6.4.4 of the report http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" definitions for abs/signum; which fails to take into account of negative-zero values for floating-point types. An easy fix would be to
add
to the report a note on the status of negative-zero for Real/Float instances; with individual implementations explicitly checking for negative-0 first. For instance GHC's implementation can be changed as follows:
instance Num Float where signum x | isNegativeZero x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x
A similar change would need to be done for the "Num Double" instance as well. Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform.
Library guidelines suggest a discussion period of 2 weeks on this issue. I'm hoping that we can resolve the issue in a timely manner, and at least GHC's implementation can match the desired semantics in the next release. If there's consensus at the end of 2-weeks; I'll go ahead and create a corresponding ticket for GHC.
Thanks,
-Levent.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
-- Felipe.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

+1 from me. Code breaks are unlikely, because people who stumbled over this probably patched it up rock solid. On 2013-04-10 21:13, Carter Schonwald wrote:
+1, would be good for me
On Wed, Apr 10, 2013 at 2:27 PM, Felipe Almeida Lessa
mailto:felipe.lessa@gmail.com> wrote: +1
On Wed, Apr 10, 2013 at 3:24 PM, Levent Erkok
mailto:erkokl@gmail.com> wrote: > The current definition of "abs" and "signum" for the types Float/Double is > not IEEE-754 compliant. To wit: > > Prelude> abs (-0.0::Float) > -0.0 > Prelude> signum (-0.0::Float) > 0.0 > > The correct result should be the other way around; abs returning 0.0 and > signum returning -0.0 when they receive a negative-zero. The same also holds > for the type Double. > > The issue came up several times in several forums, with general consensus > that the behavior should match the IEEE-754 specs. Here're three different > discussions on this matter: > > http://www.haskell.org/pipermail/libraries/2011-January/015761.html > > http://stackoverflow.com/questions/10395761/absolute-value-of-negative-zero-... > http://www.haskell.org/pipermail/haskell-cafe/2013-April/107471.html > > Proposed fix: Section 6.4.4 of the report > http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" > definitions for abs/signum; which fails to take into account of > negative-zero values for floating-point types. An easy fix would be to add > to the report a note on the status of negative-zero for Real/Float > instances; with individual implementations explicitly checking for > negative-0 first. For instance GHC's implementation can be changed as > follows: > > instance Num Float where > signum x | isNegativeZero x = x > | x == 0 = 0 > | x > 0.0 = 1 > | otherwise = negate 1 > abs x | isNegativeZero x = 0 > | x >= 0 = x > | otherwise = negate x > > A similar change would need to be done for the "Num Double" instance as > well. Of course, implementations can take advantage of the underlying CPU's > native floating-point abs/sign functions if available as well, avoiding > explicit tests at the Haskell code; based on the underlying platform. > > Library guidelines suggest a discussion period of 2 weeks on this issue. I'm > hoping that we can resolve the issue in a timely manner, and at least GHC's > implementation can match the desired semantics in the next release. If > there's consensus at the end of 2-weeks; I'll go ahead and create a > corresponding ticket for GHC. > > Thanks, > > -Levent. > > > _______________________________________________ > Libraries mailing list > Libraries@haskell.org mailto:Libraries@haskell.org > http://www.haskell.org/mailman/listinfo/libraries > -- Felipe.
_______________________________________________ Libraries mailing list Libraries@haskell.org mailto:Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

+1 from me as I too have been bitten by this.
On Wed, Apr 10, 2013 at 2:24 PM, Levent Erkok
The current definition of "abs" and "signum" for the types Float/Double is not IEEE-754 compliant. To wit:
Prelude> abs (-0.0::Float) -0.0 Prelude> signum (-0.0::Float) 0.0
The correct result should be the other way around; abs returning 0.0 and signum returning -0.0 when they receive a negative-zero. The same also holds for the type Double.
The issue came up several times in several forums, with general consensus that the behavior should match the IEEE-754 specs. Here're three different discussions on this matter:
http://www.haskell.org/pipermail/libraries/2011-January/015761.html
http://stackoverflow.com/questions/10395761/absolute-value-of-negative-zero-...
http://www.haskell.org/pipermail/haskell-cafe/2013-April/107471.html
Proposed fix: Section 6.4.4 of the report http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" definitions for abs/signum; which fails to take into account of negative-zero values for floating-point types. An easy fix would be to add to the report a note on the status of negative-zero for Real/Float instances; with individual implementations explicitly checking for negative-0 first. For instance GHC's implementation can be changed as follows:
instance Num Float where signum x | isNegativeZero x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x
A similar change would need to be done for the "Num Double" instance as well. Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform.
Library guidelines suggest a discussion period of 2 weeks on this issue. I'm hoping that we can resolve the issue in a timely manner, and at least GHC's implementation can match the desired semantics in the next release. If there's consensus at the end of 2-weeks; I'll go ahead and create a corresponding ticket for GHC.
Thanks,
-Levent.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

Hi, +1 from me. Cheers, Milan
-----Original message----- From: Levent Erkok
Sent: 10 Apr 2013, 11:24 The current definition of "abs" and "signum" for the types Float/Double is not IEEE-754 compliant. To wit:
Prelude> abs (-0.0::Float) -0.0 Prelude> signum (-0.0::Float) 0.0
The correct result should be the other way around; abs returning 0.0 and signum returning -0.0 when they receive a negative-zero. The same also holds for the type Double.
The issue came up several times in several forums, with general consensus that the behavior should match the IEEE-754 specs. Here're three different discussions on this matter:
http://www.haskell.org/pipermail/libraries/2011-January/015761.html
http://stackoverflow.com/questions/10395761/absolute-value-of-negative-zero-... http://www.haskell.org/pipermail/haskell-cafe/2013-April/107471.html
Proposed fix: Section 6.4.4 of the report http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" definitions for abs/signum; which fails to take into account of negative-zero values for floating-point types. An easy fix would be to add to the report a note on the status of negative-zero for Real/Float instances; with individual implementations explicitly checking for negative-0 first. For instance GHC's implementation can be changed as follows:
instance Num Float where signum x | isNegativeZero x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x
A similar change would need to be done for the "Num Double" instance as well. Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform.
Library guidelines suggest a discussion period of 2 weeks on this issue. I'm hoping that we can resolve the issue in a timely manner, and at least GHC's implementation can match the desired semantics in the next release. If there's consensus at the end of 2-weeks; I'll go ahead and create a corresponding ticket for GHC.
Thanks,
-Levent.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

Levent Erkok
Proposed fix: Section 6.4.4 of the report http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" definitions for abs/signum; which fails to take into account of negative-zero values for floating-point types. An easy fix would be to add to the report a note on the status of negative-zero for Real/Float instances; with individual implementations explicitly checking for negative-0 first. For instance GHC's implementation can be changed as follows:
instance Num Float where signum x | isNegativeZero x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x
A similar change would need to be done for the "Num Double" instance as well. Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform.
+1

Sounds good to me. It would be fantastic if someone could investigate Levent's suggestion "Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform" Otherwise we'll just end up adding an extra test and everyone's code will run a little bit slower. Simon From: libraries-bounces@haskell.org [mailto:libraries-bounces@haskell.org] On Behalf Of Levent Erkok Sent: 10 April 2013 19:25 To: libraries@haskell.org Subject: Fix prelude definitions of abs/signum for Floats/Doubles The current definition of "abs" and "signum" for the types Float/Double is not IEEE-754 compliant. To wit: Prelude> abs (-0.0::Float) -0.0 Prelude> signum (-0.0::Float) 0.0 The correct result should be the other way around; abs returning 0.0 and signum returning -0.0 when they receive a negative-zero. The same also holds for the type Double. The issue came up several times in several forums, with general consensus that the behavior should match the IEEE-754 specs. Here're three different discussions on this matter: http://www.haskell.org/pipermail/libraries/2011-January/015761.html http://stackoverflow.com/questions/10395761/absolute-value-of-negative-zero-... http://www.haskell.org/pipermail/haskell-cafe/2013-April/107471.html Proposed fix: Section 6.4.4 of the report http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" definitions for abs/signum; which fails to take into account of negative-zero values for floating-point types. An easy fix would be to add to the report a note on the status of negative-zero for Real/Float instances; with individual implementations explicitly checking for negative-0 first. For instance GHC's implementation can be changed as follows: instance Num Float where signum x | isNegativeZero x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x A similar change would need to be done for the "Num Double" instance as well. Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform. Library guidelines suggest a discussion period of 2 weeks on this issue. I'm hoping that we can resolve the issue in a timely manner, and at least GHC's implementation can match the desired semantics in the next release. If there's consensus at the end of 2-weeks; I'll go ahead and create a corresponding ticket for GHC. Thanks, -Levent.

On 11/04/13 10:24, Simon Peyton-Jones wrote:
Sounds good to me.
It would be fantastic if someone could investigate Levent’s suggestion “Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform”
Otherwise we’ll just end up adding an extra test and everyone’s code will run a little bit slower.
For IEEE floating point numbers, the sign is stored as a single bit, which can be changed with simple bit twiddling: import Data.Bits import Data.Word import Unsafe.Coerce w2f x = unsafeCoerce (x :: Word32) :: Float f2w x = unsafeCoerce (x :: Float) :: Word32 absFloat = w2f . (`clearBit` 31) . f2w signumFloat = w2f . (.|. 0x3f800000) . (.&. 0x80000000) . f2w w2d x = unsafeCoerce (x :: Word64) :: Double d2w x = unsafeCoerce (x :: Double) :: Word64 absDouble = w2d . (`clearBit` 63) . d2w signumDouble = w2d . (.|. 0x3ff0000000000000) . (.&. 0x8000000000000000) . d2w (Tested on linux x86-64) Twan

On Thu, Apr 11, 2013 at 2:19 AM, Twan van Laarhoven
On 11/04/13 10:24, Simon Peyton-Jones wrote:
Sounds good to me.
It would be fantastic if someone could investigate Levent's suggestion "Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform"
Otherwise we'll just end up adding an extra test and everyone's code will run a little bit slower.
For IEEE floating point numbers, the sign is stored as a single bit, which can be changed with simple bit twiddling:
import Data.Bits import Data.Word import Unsafe.Coerce
w2f x = unsafeCoerce (x :: Word32) :: Float f2w x = unsafeCoerce (x :: Float) :: Word32 absFloat = w2f . (`clearBit` 31) . f2w signumFloat = w2f . (.|. 0x3f800000) . (.&. 0x80000000) . f2w
w2d x = unsafeCoerce (x :: Word64) :: Double d2w x = unsafeCoerce (x :: Double) :: Word64 absDouble = w2d . (`clearBit` 63) . d2w signumDouble = w2d . (.|. 0x3ff0000000000000) . (.&. 0x8000000000000000) . d2w
(Tested on linux x86-64)
Twan
I didn't see Twan's email when I sent my previous one, but relatedly it should be noted that an unsafeCoerce implementation that doesn't respect NaNs can allow distinguishing between NaNs, i.e. λ> signumDouble (w2d 0xfff8000000000000) -1.0 λ> signumDouble (w2d 0x7ff8000000000000) 1.0 Possibly this is considered a bad thing. Shachaf

On April 11, 2013 04:24:26 Simon Peyton-Jones wrote:
It would be fantastic if someone could investigate Levent's suggestion "Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform"
Compiling double test(double value) { return fabs(value) } with -O2 -S in gcc gives you test: .LFB3: .cfi_startproc movsd .LC0(%rip), %xmm1 andpd %xmm1, %xmm0 ret .cfi_endproc .LFE3: .size test, .-test .section .rodata.cst16,"aM",@progbits,16 .align 16 .LC0: .long 4294967295 .long 2147483647 .long 0 .long 0 .ident "GCC: (Debian 4.7.2-5) 4.7.2" .section .note.GNU-stack,"",@progbits That is, it does an andpd (and packed double) operation against the constant 2^63-1 (all 1 except the top bit) to just mask out the sign bit. This should work with NaN and Inf too as the former ignores the sign bit and the later interpets it in the expectd manner. Cheers! -Tyson

On Wed, Apr 10, 2013 at 11:24 AM, Levent Erkok
The current definition of "abs" and "signum" for the types Float/Double is not IEEE-754 compliant. To wit:
Prelude> abs (-0.0::Float) -0.0 Prelude> signum (-0.0::Float) 0.0
The correct result should be the other way around; abs returning 0.0 and signum returning -0.0 when they receive a negative-zero. The same also holds for the type Double.
The issue came up several times in several forums, with general consensus that the behavior should match the IEEE-754 specs. Here're three different discussions on this matter:
http://www.haskell.org/pipermail/libraries/2011-January/015761.html
http://stackoverflow.com/questions/10395761/absolute-value-of-negative-zero-... http://www.haskell.org/pipermail/haskell-cafe/2013-April/107471.html
Proposed fix: Section 6.4.4 of the report http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" definitions for abs/signum; which fails to take into account of negative-zero values for floating-point types. An easy fix would be to add to the report a note on the status of negative-zero for Real/Float instances; with individual implementations explicitly checking for negative-0 first. For instance GHC's implementation can be changed as follows:
instance Num Float where signum x | isNegativeZero x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x
A similar change would need to be done for the "Num Double" instance as well. Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform.
Library guidelines suggest a discussion period of 2 weeks on this issue. I'm hoping that we can resolve the issue in a timely manner, and at least GHC's implementation can match the desired semantics in the next release. If there's consensus at the end of 2-weeks; I'll go ahead and create a corresponding ticket for GHC.
Thanks,
-Levent.
While we're at it: abs NaN is NaN, but signum NaN is currently -1.0. Maybe it should also be NaN? Shachaf

Great catch! To be IEEE-754 compliant, signum *should* return NaN on NaN.
Here's the new code:
instance Num Float where
signum x | isNegativeZero x = x
| isNaN x = x
| x == 0 = 0
| x > 0.0 = 1
| otherwise = negate 1
abs x | isNegativeZero x = 0
| x >= 0 = x
| otherwise = negate x
Of course, this extra test makes Simon's point even more relevant; i.e.;
these should map to underlying CPU's native signum/abs functions if
available.
-Levent.
On Thu, Apr 11, 2013 at 2:23 AM, Shachaf Ben-Kiki
On Wed, Apr 10, 2013 at 11:24 AM, Levent Erkok
wrote: The current definition of "abs" and "signum" for the types Float/Double is not IEEE-754 compliant. To wit:
Prelude> abs (-0.0::Float) -0.0 Prelude> signum (-0.0::Float) 0.0
The correct result should be the other way around; abs returning 0.0 and signum returning -0.0 when they receive a negative-zero. The same also holds for the type Double.
The issue came up several times in several forums, with general consensus that the behavior should match the IEEE-754 specs. Here're three different discussions on this matter:
http://www.haskell.org/pipermail/libraries/2011-January/015761.html
http://stackoverflow.com/questions/10395761/absolute-value-of-negative-zero-...
http://www.haskell.org/pipermail/haskell-cafe/2013-April/107471.html
Proposed fix: Section 6.4.4 of the report http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default" definitions for abs/signum; which fails to take into account of negative-zero values for floating-point types. An easy fix would be to
add
to the report a note on the status of negative-zero for Real/Float instances; with individual implementations explicitly checking for negative-0 first. For instance GHC's implementation can be changed as follows:
instance Num Float where signum x | isNegativeZero x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x
A similar change would need to be done for the "Num Double" instance as well. Of course, implementations can take advantage of the underlying CPU's native floating-point abs/sign functions if available as well, avoiding explicit tests at the Haskell code; based on the underlying platform.
Library guidelines suggest a discussion period of 2 weeks on this issue. I'm hoping that we can resolve the issue in a timely manner, and at least GHC's implementation can match the desired semantics in the next release. If there's consensus at the end of 2-weeks; I'll go ahead and create a corresponding ticket for GHC.
Thanks,
-Levent.
While we're at it: abs NaN is NaN, but signum NaN is currently -1.0. Maybe it should also be NaN?
Shachaf

On Thu, Apr 11, 2013 at 2:50 AM, Levent Erkok
Great catch! To be IEEE-754 compliant, signum *should* return NaN on NaN. Here's the new code:
instance Num Float where signum x | isNegativeZero x = x | isNaN x = x | x == 0 = 0 | x > 0.0 = 1 | otherwise = negate 1 abs x | isNegativeZero x = 0 | x >= 0 = x | otherwise = negate x
Of course, this extra test makes Simon's point even more relevant; i.e.; these should map to underlying CPU's native signum/abs functions if available.
-Levent.
Maybe it would be simpler to say: signum x | x == 0 = x | isNaN x = x | x > 0 = 1 | otherwise = negate 1 Shachaf

On Thu, Apr 11, 2013 at 2:58 AM, Shachaf Ben-Kiki
Maybe it would be simpler to say:
signum x | x == 0 = x | isNaN x = x | x > 0 = 1 | otherwise = negate 1
Shachaf
Thanks Shachaf. This version is equivalent to the one I proposed, and it saves one test; so it's definitely preferable. I'll put this one in the ticket that'll finally be created for GHC. -Levent.

On Thursday 11 April 2013, 08:07:57, Levent Erkok wrote:
On Thu, Apr 11, 2013 at 2:58 AM, Shachaf Ben-Kiki
wrote: Maybe it would be simpler to say:
signum x | x == 0 = x
| isNaN x = x | x > 0 = 1 | otherwise = negate 1
Shachaf
Thanks Shachaf. This version is equivalent to the one I proposed, and it saves one test; so it's definitely preferable. I'll put this one in the ticket that'll finally be created for GHC.
Also abs x | x < 0 = negate x | otherwise = x keeps the number of tests at one. Cheers, Daniel

On Sunday 21 April 2013, 16:15:09, Shachaf Ben-Kiki wrote:
On Sun, Apr 21, 2013 at 4:10 PM, Daniel Fischer
wrote: Also
abs x
| x < 0 = negate x | otherwise = x
keeps the number of tests at one.
This gives the wrong result for -0.0 (which is compare-equal to +0.0).
Shachaf
Duh. Sorry.

| >

I believe enough discussions took place on this topic, and we all seem to
be in agreement that the behavior should be fixed. Encouraged by Simon's
earlier note, here's the ticket I filed:
http://hackage.haskell.org/trac/ghc/ticket/7858
Thanks to everyone for taking the time and contributing!
-Levent.
On Mon, Apr 22, 2013 at 4:25 AM, Simon Peyton-Jones
| >
wrote: | > > Also | > > | > > abs x | > > | > > | x < 0 = negate x | > > | otherwise = x | > > | > > keeps the number of tests at one. | > | > This gives the wrong result for -0.0 (which is compare-equal to +0.0). | > | > Shachaf I'm very happy to see all this activity on making GHC's numerics better. For this particular thread, would someone like to create a ticket?
My list of numeric infelicities is under "Better numerics" on http://hackage.haskell.org/trac/ghc/wiki/Status/SLPJ-Tickets
If any of you feel able to make progress on these things, it'd be great.
Simon
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
participants (12)
-
Carter Schonwald
-
Daniel Fischer
-
David Luposchainsky
-
Edward Kmett
-
Felipe Almeida Lessa
-
Herbert Valerio Riedel
-
Levent Erkok
-
Milan Straka
-
Shachaf Ben-Kiki
-
Simon Peyton-Jones
-
Twan van Laarhoven
-
Tyson Whitehead