[GHC] #14425: approxRational is wrong when (rat - eps) overflows in negative direction

#14425: approxRational is wrong when (rat - eps) overflows in negative direction -------------------------------------+------------------------------------- Reporter: danielk | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: | Version: 8.2.1 libraries/base | Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: Incorrect result Unknown/Multiple | at runtime Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- This program is wrong. The second output should be 0 % 1. {{{ import Data.Ratio main = do print (approxRational (0 % 1 :: Ratio Int) (1 % 10)) -- 0%1, correct print (approxRational (0 % 1 :: Ratio Word) (1 % 10)) -- 1%1, incorrect }}} The problem is that (rat-eps) overflows in the negative direction (see [http://hackage.haskell.org/package/base-4.10.0.0/docs/src/Data.Ratio.html#ap... source]). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14425 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14425: approxRational is wrong when (rat - eps) overflows in negative direction -------------------------------------+------------------------------------- Reporter: danielk | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: libraries/base | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by danielk): I suggest the following simple fix. Convert rat and eps to Rational before subtracting and adding epsilon. This eliminates the possibility of overflow (in either direction). {{{ approxRational :: (RealFrac a) => a -> a -> Rational approxRational rat eps = simplest (toRational rat - toRational eps) (toRational rat + toRational eps) ... }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14425#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14425: approxRational is wrong when (rat - eps) overflows in negative direction -------------------------------------+------------------------------------- Reporter: danielk | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: libraries/base | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by vilem): +1 This caused a very hard-to-find bug in my code. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14425#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14425: approxRational is wrong when (rat - eps) overflows in negative direction -------------------------------------+------------------------------------- Reporter: danielk | Owner: (none) Type: bug | Status: patch Priority: normal | Milestone: 8.4.1 Component: libraries/base | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: T14425 Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D4168 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * testcase: => T14425 * status: new => patch * differential: => Phab:D4168 * milestone: => 8.4.1 Comment: See Phab:D4168. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14425#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14425: approxRational is wrong when (rat - eps) overflows in negative direction
-------------------------------------+-------------------------------------
Reporter: danielk | Owner: (none)
Type: bug | Status: patch
Priority: normal | Milestone: 8.4.1
Component: libraries/base | Version: 8.2.1
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
Type of failure: Incorrect result | Unknown/Multiple
at runtime | Test Case: T14425
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D4168
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Ben Gamari

#14425: approxRational is wrong when (rat - eps) overflows in negative direction
-------------------------------------+-------------------------------------
Reporter: danielk | Owner: (none)
Type: bug | Status: patch
Priority: normal | Milestone: 8.4.1
Component: libraries/base | Version: 8.2.1
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
Type of failure: Incorrect result | Unknown/Multiple
at runtime | Test Case: T14425
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D4168
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Ben Gamari

#14425: approxRational is wrong when (rat - eps) overflows in negative direction
-------------------------------------+-------------------------------------
Reporter: danielk | Owner: (none)
Type: bug | Status: patch
Priority: normal | Milestone: 8.4.1
Component: libraries/base | Version: 8.2.1
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
Type of failure: Incorrect result | Unknown/Multiple
at runtime | Test Case: T14425
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D4168
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Ben Gamari

#14425: approxRational is wrong when (rat - eps) overflows in negative direction -------------------------------------+------------------------------------- Reporter: danielk | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: 8.4.1 Component: libraries/base | Version: 8.2.1 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: T14425 Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D4168 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: patch => closed * resolution: => fixed -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14425#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC