3 ms·
This smells like a good fit for Haskell, since computation is deferred until a result is demanded. I haven't tried it but I can imagine an implementation of, fo
by shadowgovt 4y ago
This smells like a good fit for Haskell, since computation is deferred until a result is demanded. I haven't tried it but I can imagine an implementation of, for example, division that would do its best to keep the numerator and denominator intact in their original formats until forced to kick out a value.
(My Haskell-fu isn't deep, but I suspect it would even be possible to write it so that, for example, multiplication of two division operation expressions multiplied the numerators together instead of doing divide -> divide -> multiply...).
- tromp 4y agoThere's Data.Ratio which represents fractions by their numerator and denominator in lowest terms: $ ghci GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help Prelude> :m +Data.Ratio Prelude Data.Ratio> :t (%) (%) :: Integral a => a -> a -> Ratio a Prelude Data.Ratio> 18 % 21 6 % 7 Prelude Data.Ratio> 1%10 + 2%10 3 % 10 There's even Data.CReal for working with the computable reals: Prelude> :m +Data.CReal Data.Complex Prelude Data.CReal Data.Complex> let i = 0 :+ 1 Prelude Data.CReal Data.Complex> exp (i * pi) + 1 :: Complex (CReal 0) 0 :+ 0
- initplus 4y agoYou could overload operators like + and / so they evaluate to an AST representing the calculation rather than the actual calculated value. Then you need to do some analysis on your AST to try and restructure it in a way that preserves as much precision as possible. You don’t really need Haskell for this though, in theory it will work in any language (but more ergonomically if you have operator overloading).