# \[SOLVED\] Identical pure expressions evaluate to two different outputs

**URL:** <https://discourse.haskell.org/t/solved-identical-pure-expressions-evaluate-to-two-different-outputs/7028>\
**Category:** Learn\
**Created:** [July 17, 2023, 11:09pm UTC](https://discourse.haskell.org/t/solved-identical-pure-expressions-evaluate-to-two-different-outputs/7028 "2023-07-17T23:09:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CarrotHead](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@CarrotHead](https://discourse.haskell.org/u/CarrotHead)\
**Post date:** [July 17, 2023, 11:09pm UTC](https://discourse.haskell.org/t/solved-identical-pure-expressions-evaluate-to-two-different-outputs/7028/1 "2023-07-17T23:09:58Z")

</div>

I was reading through Iago Leal’s post on [lazily solving differential equations](https://iagoleal.com/posts/calculus-symbolic-ode/), so I have this `Stream a` type which I think represents the coefficients of the Taylor series of f(x) about x=a.

```haskell
{-# LANGUAGE DeriveTraversable, NoMonomorphismRestriction #-}
import Data.Foldable (toList)

data Stream a = a :> Stream a
  deriving (Functor, Foldable)

infixr 2 :>

ex = 1.0 :> ex

```

where `ex` represents the coefficients of the series of f(x)=e^x.  
Then I define a helper function to return the overall coefficients of the Taylor series by dividing the first term by 0!, the second by 1!, the third by 2! etc.

```haskell
toTaylor :: Fractional a => Stream a -> [a]
toTaylor f = zipWith (/) (toList f) (fromIntegral <$> facs)
  where
    facs :: [Int]
    facs = (:) 1 $ scanl1 (*) [1..]

```

My output from running `mapM_ print $ take 25 $ zipWith (/) (toList ex) (fromIntegral <$> facs)` in `ghci-9.2.8` is:

```haskell
1.0
1.0
0.5
0.16666666666666666
4.1666666666666664e-2
8.333333333333333e-3
1.388888888888889e-3
1.984126984126984e-4
2.48015873015873e-5
2.7557319223985893e-6
2.755731922398589e-7
2.505210838544172e-8
2.08767569878681e-9
1.6059043836821613e-10
1.1470745597729725e-11
7.647163731819816e-13
4.779477332387385e-14
2.8114572543455206e-15
1.5619206968586225e-16
8.22063524662433e-18
4.110317623312165e-19
1.9572941063391263e-20
8.896791392450574e-22
3.8681701706306835e-23
1.6117375710961184e-24

```

However my output from running `mapM_ print $ take 25 $ toTaylor ex` is:

```haskell
1.0
1.0
0.5
0.16666666666666666
4.1666666666666664e-2
8.333333333333333e-3
1.388888888888889e-3
1.984126984126984e-4
2.48015873015873e-5
2.7557319223985893e-6
2.755731922398589e-7
2.505210838544172e-8
2.08767569878681e-9
1.6059043836821613e-10
1.1470745597729725e-11
7.647163731819816e-13
4.779477332387385e-14
2.8114572543455206e-15
1.5619206968586225e-16
8.22063524662433e-18
4.110317623312165e-19
-2.353334294364486e-19
-7.995773634431128e-19
1.2302708207447328e-19
-1.2762938906401676e-19

```

which is completely different as the first list converges to 0 whereas the above oscillates between positive and negative numbers, despite the fact that `toTaylor ex = zipWith (/) (toList ex) (fromIntegral <$> facs)` so they should be the same list. What’s more, both lists give the same output up to the number `4.110317623312165e-19` beyond which they diverge from each other.

I can’t think of anything that can explain this behaviour. Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Probie](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/probie/32/3232_2.png) [@Probie](https://discourse.haskell.org/u/Probie)\
**Post date:** [July 17, 2023, 11:18pm UTC](https://discourse.haskell.org/t/solved-identical-pure-expressions-evaluate-to-two-different-outputs/7028/2 "2023-07-17T23:18:53Z")

</div>

I’m 99% sure that in `mapM_ print $ take 25 $ zipWith (/) (toList ex) (fromIntegral <$> facs)` you either have given the type signature `facs :: [Integer]`, or haven’t explicitly given `facs` one, in which case it’ll default to `[Integer]`.

`Integer` is arbitrary precision, but `Int` is not and will overflow to a negative compare. Try running `take 25 $ 1:(scanl1 (*) [1::Integer ..])` and `take 25 $ 1:(scanl1 (*) [1::Int ..])` in GHCi and comparing the results.

---

<div class="post-metadata">

**Author:** ![CarrotHead](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@CarrotHead](https://discourse.haskell.org/u/CarrotHead)\
**Post date:** [July 17, 2023, 11:29pm UTC](https://discourse.haskell.org/t/solved-identical-pure-expressions-evaluate-to-two-different-outputs/7028/3 "2023-07-17T23:29:48Z")

</div>

I swapped `[Int]` for `[Integer]` in `facs` and it works, I get the first list. Thanks for the help and I’ll bear in mind that `Int` is fixed precision so it overflows at high precision.

---

<div class="post-metadata">

**Author:** ![sclv](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/sclv/32/71_2.png) [@sclv](https://discourse.haskell.org/u/sclv)\
**Post date:** [July 18, 2023, 12:25am UTC](https://discourse.haskell.org/t/solved-identical-pure-expressions-evaluate-to-two-different-outputs/7028/4 "2023-07-18T00:25:14Z")

</div>

I hadn’t seen that post, neat!

I’d like to add that there’s some related (subsequent) work I did ages ago that makes such (approximate numerical) solvers more practical by moving from a single taylor series to a lazy list of them and providing compositional “solver” strategies: [http://gbaz.github.io/slides/ode-draft-2009.pdf](http://gbaz.github.io/slides/ode-draft-2009.pdf)
