Rational numbers for PureScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
docs/Data
src/Data
test
.gitignore
.travis.yml
README.md
bower.json
package.json

README.md

Rational numbers for PureScript

Using numbers to do arithmetic with fractions in PureScript may yield surprising results:

> 0.1 + 0.2
0.30000000000000004

The same can be expressed accurately with Rational using the (%) operator:

> import Data.Rational
> (1 % 10) + (2 % 10)
3 % 10

You can turn a Rational to a Number:

> toNumber (3 % 10)
0.3

Other Ratios

Rational is just a type alias for Ratio Int and you might want to use Ratio with other than Int. The type you choose must however be an EuclideanRing.

For example, one limitation with Rational is that it can easily overflow the 32-bit PureScript Int. You can get around this problem by using BigInt.

> import Data.Ratio ((%), reduce)
> import Data.BigInt (fromInt, fromString)
> :type fromInt 1 % fromInt 3
Ratio BigInt
> reduce <$> fromString "10" <*> fromString "857981209301293808359384092830482"
(Just fromString "5" % fromString "428990604650646904179692046415241")

Installation

bower install purescript-rationals

API documentation

Module documentation is published on Pursuit.