[Haskell-beginners] Question on fromIntegral

Jonathan Drews jondrews at fastmail.com
Thu Dec 1 05:17:49 UTC 2022


On Wed, Nov 30, 2022 at 11:05:21PM -0500, David McBride wrote:
> It is because (**) takes floating point numbers. In your first equation
> both 4s are different. The second 4 is an Integer but the first is a some
> Floating type aka 4.0.

Thank you David:

I switched from x**1 to x^i and it worked

fact :: Integer -> Integer
fact n = product [1..n]


expon :: Double -> Double
expon x  = sum [x^i/fromIntegral(fact(i)) | i <- [0..50]]

$ ghci
GHCi, version 9.2.4: https://www.haskell.org/ghc/  :? for help
ghci> :l ePowerSeries.hs
[1 of 1] Compiling Main             ( ePowerSeries.hs, interpreted )
Ok, one module loaded.
ghci> exp
exp       expon     exponent
ghci> expon 2
7.389056098930649
ghci> exp 2
7.38905609893065


I believe x^i has a similar effect to x**fromIntegral(i)

--
Kind regards,
Jonathan 


More information about the Beginners mailing list