Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#8780 closed bug (duplicate)

abs for IEEE floating point is slightly wrong.

Reported by: augustss Owned by:
Priority: low Milestone:
Component: Compiler Version: 7.6.3
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect result at runtime Test Case:
Blocked By: Blocking:
Related Tickets: #7858 Differential Rev(s):
Wiki Page:

Description

Evaluating abs(-0) gives the answer -0. This unexpected since it breaks invariants like 1/(abs x) >= 0. So abs(-0) should be 0. This is also the norm for other languages.

Together with this change, signum should also be changed so signum(-0) is -0. This maintains the invariant abs x * signum x == x.

Change History (4)

comment:1 Changed 5 years ago by thomie

Status: newinfoneeded
Type of failure: None/UnknownIncorrect result at runtime

I cannot reproduce your findings. Can you try again, specifying your GHC version, OS and architecture. Thanks.

$ uname -op
x86_64 GNU/Linux

$ ghc-7.6.3 --interactive
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.

Prelude> abs(-0)
0

Prelude> signum(-0) == 0
True

Prelude> let x = -0 in abs x * signum x == x
True

comment:2 Changed 5 years ago by augustss

It seems the printing of -0 has changed. Here's my session

$ ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let x = -0
Prelude> x
0
Prelude> isNegativeZero x
True
Prelude> isNegativeZero (abs x)
True
Prelude> isNegativeZero (signum x)
False

comment:3 Changed 5 years ago by thomie

Resolution: duplicate
Status: infoneededclosed

A fix for both issues will be in 7.10, see #7858.

comment:4 in reply to:  2 Changed 5 years ago by rwbarton

Replying to augustss:

It seems the printing of -0 has changed. Here's my session

$ ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let x = -0
Prelude> x
0

This part is just because x defaulted to Integer on this line; -0.0 is still printed as -0.0. (7.8.3 has the MR disabled in ghci by default.)

Note: See TracTickets for help on using tickets.