[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 <ben@…>): In [changeset:"5834da4872877736eefb85daedaf7b137ae702a1/ghc" 5834da48/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="5834da4872877736eefb85daedaf7b137ae702a1" base: Fix #14425 Test Plan: Validate Reviewers: hvr Subscribers: rwbarton, thomie GHC Trac Issues: #14425 Differential Revision: https://phabricator.haskell.org/D4167 }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/14425#comment:6> 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 <ben@…>): In [changeset:"ce9a67784390735aa9749667934af49665b8402e/ghc" ce9a6778/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="ce9a67784390735aa9749667934af49665b8402e" base: Add test for #14425 Test Plan: Validate Reviewers: hvr Subscribers: rwbarton, thomie GHC Trac Issues: #14425 Differential Revision: https://phabricator.haskell.org/D4166 }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/14425#comment:4> 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 <ben@…>): In [changeset:"c59d6da8639fd88919090b29cf4e76c4d0d8bbde/ghc" c59d6da/ghc]: {{{ #!CommitTicketReference repository="ghc" revision="c59d6da8639fd88919090b29cf4e76c4d0d8bbde" base: Normalize style of approxRational Stumbled upon this odd bit of style while looking at #14425. Usually I don't like to do this sort of reformatting, but this seemed like it would be necessary in the course fo fixing #14425. Reviewers: hvr Subscribers: rwbarton, thomie Differential Revision: https://phabricator.haskell.org/D4168 }}} -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/14425#comment:5> 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: 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