/horatio

The rational and most trusted friend of the tragic hero, Floating Point

Primary LanguageErlangOtherNOASSERTION

horatio

Build Status LFE Versions Erlang Versions Tags Downloads

Horatio logo

The rational and most trusted friend of the tragic hero, Floating Point

Table of Contents

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 compile

Usage

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
ok

You can also use the alias pp (for "pretty print"):

> (ratio:pp r)
2/3
ok

Converting

To strings, atoms, and floats:

> (ratio:->str r)
"2/3"
> (ratio:->atom r)
2/3
> (ratio:->float r)
0.6666666666666666

Floats 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
ok

Subtraction:

> (ratio:pp (ratio:sub r1 r2))
1/12
ok

Multiplication:

> (ratio:pp (ratio:mult r1 r2))
1/12
ok

Division:

> (ratio:pp (ratio:div r1 r2))
4/3
ok

Operations

> (ratio:eq r1 r2)
false
> (ratio:eq r2 r2)
true

Powers

> (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
ok

Large numbers:

> (ratio:pp (ratio:div (ratio:pow 3 200) (ratio:pow 2 430)))
8960496791105607/93536104789177786765035829293842113257979682750464
ok

API

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/2

License

Apache Version 2 License

Copyright © 2015-2016, Duncan McGreggor oubiwann@gmail.com