cartazio / numbers

Other
29 stars 13 forks source link

Problems with ** #13

Open roytu opened 10 years ago

roytu commented 10 years ago

The \ operator triggers an exception when the base is 0 or negative. Tested with Prec10 and Prec50 for epsilon values.

Prelude> :module + Data.Number.Fixed
Prelude Data.Number.Fixed> (0 ** 2) :: Fixed Prec10
Loading package numbers-3000.2.0.1 ... linking ... done.
*** Exception: Fraction.log
Prelude Data.Number.Fixed> (0 ** (-2)) :: Fixed Prec10
*** Exception: Fraction.log
Prelude Data.Number.Fixed> ((-2) ** 2) :: Fixed Prec10
*** Exception: Fraction.log
Prelude Data.Number.Fixed> 

Probably makes sense that negative values crash, but 0 shouldn't.

crockeea commented 8 years ago

This really is a deficiency of Haskell's Floating class. It gives a default implementation for (**) which is

x ** y = exp ((log x) * y)

and of course log 0 is undefined. For a similar reason, an error also occurs for sqrt (0 :: Fixed Prec10).

EarthCitizen commented 6 years ago

In some cases the behavior is not the same as built-in types:

> (-1) ** (-5) :: Double
-1.0

> (-1) ** (-5) :: BigFloat Prec50
*** Exception: Fraction.log
danwdart commented 1 week ago

Would that be why doing this is wrong?:

> floor (2 ** (1 / 2) :: CReal) :: Integer
0

> ceiling  (2 ** (1 / 2) :: CReal) :: Integer
1

whilst doing this is right?

> floor (sqrt 2 :: CReal) :: Integer
1

> round (sqrt 2 :: CReal) :: Integer
1

> ceiling (sqrt 2 :: CReal) :: Integer
2

> round (2 ** (1 / 2) :: CReal) :: Integer
1