The rational and most trusted friend of the tragic hero, Floating Point
Introduction ↟
Add content to me here!
Installation ↟
Just add it to your rebar.config deps:
{deps, [
...
{horatio, ".*",
{git, "git@github.com:lfex/horatio.git", "master"}}
]}.And then do the usual:
$ rebar get-deps
$ rebar compileUsage ↟
Creating Fractions ↟
You may create frations using any one of the following:
> (ratio:new 2 3)
#(ratio 2 3)
> (ratio:new "2/3")
#(ratio 2 3)
> (ratio:new '2/3)
#(ratio 2 3)
> (ratio:new #(2 3))
#(ratio 2 3)Convenience Functions ↟
Printing Fractions ↟
> (set r (ratio:new 2 3))
#(ratio 2 3)
> (ratio:print r)
2/3
okYou can also use the alias pp (for "pretty print"):
> (ratio:pp r)
2/3
okConverting ↟
To strings, atoms, and floats:
> (ratio:->str r)
"2/3"
> (ratio:->atom r)
2/3
> (ratio:->float r)
0.6666666666666666Floats to rational numbers:
> (ratio:float->ratio 0.5)
#(ratio 1 2)
> (ratio:float->ratio 1.5)
#(ratio 3 2)
> (ratio:float->ratio 3.1415)
#(ratio 6283 2000)Math ↟
For the following examples we'll use these rational numbers:
> (set r1 (ratio:new '1/3))
#(ratio 1 3)
> (set r2 (ratio:new '1/4))
#(ratio 1 4)Arithmatic ↟
Addition:
> (ratio:pp (ratio:add r1 r2))
7/12
okSubtraction:
> (ratio:pp (ratio:sub r1 r2))
1/12
okMultiplication:
> (ratio:pp (ratio:mult r1 r2))
1/12
okDivision:
> (ratio:pp (ratio:div r1 r2))
4/3
okOperations ↟
> (ratio:eq r1 r2)
false
> (ratio:eq r2 r2)
truePowers ↟
> (ratio:pp (ratio:pow r1 4))
1/81
ok
> (ratio:pp (ratio:pow r1 -4))
81/1
ok
> (ratio:pp (ratio:pow r2 4))
1/256
ok
> (ratio:pp (ratio:pow r2 -4))
256/1
okLarge numbers:
> (ratio:pp (ratio:div (ratio:pow 3 200) (ratio:pow 2 430)))
8960496791105607/93536104789177786765035829293842113257979682750464
okAPI ↟
The list of functions currently supported by the ratio library are as follows:
ratio:->atom/1
ratio:->float/1
ratio:->str/1
ratio:add/2
ratio:denom/1
ratio:div/2
ratio:eq/2
ratio:float->ratio/1
ratio:gcd/2
ratio:mult/2
ratio:new/1
ratio:new/2
ratio:normalize/1
ratio:normalize/2
ratio:numer/1
ratio:pow/2
ratio:pp/1
ratio:print/1
ratio:ratio/1
ratio:ratio/2
ratio:sub/2License ↟
Apache Version 2 License
Copyright © 2015-2016, Duncan McGreggor oubiwann@gmail.com
