ekmett / ad

Automatic Differentiation
http://hackage.haskell.org/package/ad
BSD 3-Clause "New" or "Revised" License
372 stars 73 forks source link

Difference between `auto` and `realToFrac` #98

Closed dschrempf closed 7 months ago

dschrempf commented 2 years ago

Many times, I can embed Double constants into functions to be derived by ad by just using realToFrac. As an example, I use realToFrac on the example using auto:

λ: diff (\x -> realToFrac (4.0 :: Double) * sin x) 0
4.0

Is there a difference between using realToFrac, and using auto? I can't see any difference in the results, but maybe there is a difference in speed? Thank you!

ekmett commented 7 months ago

The difference would mostly be in speed and in handling of NaNs and the performance you mentioned. realToFrac usually eats NaNs in a bad way: it also pays to do a division.

ghci> realToFrac (0/0) :: Double -Infinity

Also auto can be used on non Fractional data types like syntax trees and the like.