README.md 4.38 KB
Newer Older
Duncan McGreggor's avatar
Duncan McGreggor committed
1
# horatio
Duncan McGreggor's avatar
Duncan McGreggor committed
2

Duncan McGreggor's avatar
Duncan McGreggor committed
3
[![Build Status][travis badge]][travis] [![LFE Versions][lfe badge]][lfe] [![Erlang Versions][erlang badge]][versions] [![Tags][github tags badge]][github tags] [![Downloads][hex downloads]][hex package]
Duncan McGreggor's avatar
Duncan McGreggor committed
4

5
[![Horatio logo][logo]][logo]
Duncan McGreggor's avatar
Duncan McGreggor committed
6

Duncan McGreggor's avatar
Duncan McGreggor committed
7
8
9
*The rational and most trusted friend of the tragic hero, Floating Point*


Duncan McGreggor's avatar
Duncan McGreggor committed
10
## Table of Contents
Duncan McGreggor's avatar
Duncan McGreggor committed
11

Duncan McGreggor's avatar
Duncan McGreggor committed
12
13
14
15
16
17
* [Introduction](#introduction-)
* [Installation](#installation-)
* [Usage](#usage-)
  * [Creating Fractions](#creating-fractions-)
  * [Convenience Functions](#convenience-functions-)
    * [Printing](#printing-fractions-)
Duncan McGreggor's avatar
Duncan McGreggor committed
18
    * [Converting](#converting-)
Duncan McGreggor's avatar
Duncan McGreggor committed
19
20
21
22
  * [Math](#math-)
    * [Arithmatic](#arithmatic-)
    * [Operations](#operations-)
    * [Powers](#powers-)
Duncan McGreggor's avatar
Duncan McGreggor committed
23
* [API](#api-)
Duncan McGreggor's avatar
Duncan McGreggor committed
24
25
* [License](#license-)

Duncan McGreggor's avatar
Duncan McGreggor committed
26
27

## Introduction [↟](#table-of-contents)
Duncan McGreggor's avatar
Duncan McGreggor committed
28

Duncan McGreggor's avatar
Duncan McGreggor committed
29
Add content to me here!
Duncan McGreggor's avatar
Duncan McGreggor committed
30

Duncan McGreggor's avatar
Duncan McGreggor committed
31

Duncan McGreggor's avatar
Duncan McGreggor committed
32
## Installation [↟](#table-of-contents)
Duncan McGreggor's avatar
Duncan McGreggor committed
33
34
35
36

Just add it to your ``rebar.config`` deps:

```erlang
Duncan McGreggor's avatar
Duncan McGreggor committed
37
38
39
40
41
{deps, [
  ...
  {horatio, ".*",
    {git, "git@github.com:lfex/horatio.git", "master"}}
    ]}.
Duncan McGreggor's avatar
Duncan McGreggor committed
42
43
44
45
46
```

And then do the usual:

```bash
Duncan McGreggor's avatar
Duncan McGreggor committed
47
48
$ rebar get-deps
$ rebar compile
Duncan McGreggor's avatar
Duncan McGreggor committed
49
50
```

Duncan McGreggor's avatar
Duncan McGreggor committed
51

Duncan McGreggor's avatar
Duncan McGreggor committed
52
## Usage [↟](#table-of-contents)
Duncan McGreggor's avatar
Duncan McGreggor committed
53

Duncan McGreggor's avatar
Duncan McGreggor committed
54
### Creating Fractions [↟](#table-of-contents)
Duncan McGreggor's avatar
Duncan McGreggor committed
55

Duncan McGreggor's avatar
Duncan McGreggor committed
56
57
58
59
60
61
62
63
64
65
66
67
You may create frations using any one of the following:

```lfe
> (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)
```
Duncan McGreggor's avatar
Duncan McGreggor committed
68
69
70
71
72

### Convenience Functions [↟](#table-of-contents)

#### Printing Fractions [↟](#table-of-contents)

Duncan McGreggor's avatar
Duncan McGreggor committed
73
74
75
76
77
78
79
80
```lfe
> (set r (ratio:new 2 3))
#(ratio 2 3)
> (ratio:print r)
2/3
ok
```

Duncan McGreggor's avatar
Duncan McGreggor committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
You can also use the alias ``pp`` (for "pretty print"):

```lfe
> (ratio:pp r)
2/3
ok
```

#### Converting [↟](#table-of-contents)

To strings, atoms, and floats:

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

Floats to rational numbers:

```lfe
> (ratio:float->ratio 0.5)
#(ratio 1 2)
> (ratio:float->ratio 1.5)
#(ratio 3 2)
> (ratio:float->ratio 3.1415)
#(ratio 6283 2000)
```

Duncan McGreggor's avatar
Duncan McGreggor committed
113
114
### Math [↟](#table-of-contents)

Duncan McGreggor's avatar
Duncan McGreggor committed
115
116
117
118
119
120
121
122
123
For the following examples we'll use these rational numbers:

```lfe
> (set r1 (ratio:new '1/3))
#(ratio 1 3)
> (set r2 (ratio:new '1/4))
#(ratio 1 4)
```

Duncan McGreggor's avatar
Duncan McGreggor committed
124
#### Arithmatic [↟](#table-of-contents)
Duncan McGreggor's avatar
Duncan McGreggor committed
125
126
127
128

Addition:

```lfe
Duncan McGreggor's avatar
Duncan McGreggor committed
129
> (ratio:pp (ratio:add r1 r2))
Duncan McGreggor's avatar
Duncan McGreggor committed
130
131
132
133
134
135
136
7/12
ok
```

Subtraction:

```lfe
Duncan McGreggor's avatar
Duncan McGreggor committed
137
> (ratio:pp (ratio:sub r1 r2))
Duncan McGreggor's avatar
Duncan McGreggor committed
138
139
140
141
142
143
144
1/12
ok
```

Multiplication:

```lfe
Duncan McGreggor's avatar
Duncan McGreggor committed
145
> (ratio:pp (ratio:mult r1 r2))
Duncan McGreggor's avatar
Duncan McGreggor committed
146
147
148
149
150
151
152
1/12
ok
```

Division:

```lfe
Duncan McGreggor's avatar
Duncan McGreggor committed
153
> (ratio:pp (ratio:div r1 r2))
Duncan McGreggor's avatar
Duncan McGreggor committed
154
155
156
157
4/3
ok
```

Duncan McGreggor's avatar
Duncan McGreggor committed
158
#### Operations [↟](#table-of-contents)
Duncan McGreggor's avatar
Duncan McGreggor committed
159
160
161
162
163
164
165

```lfe
> (ratio:eq r1 r2)
false
> (ratio:eq r2 r2)
true
```
Duncan McGreggor's avatar
Duncan McGreggor committed
166
167
#### Powers [↟](#table-of-contents)

Duncan McGreggor's avatar
Duncan McGreggor committed
168
```lfe
Duncan McGreggor's avatar
Duncan McGreggor committed
169
> (ratio:pp (ratio:pow r1 4))
Duncan McGreggor's avatar
Duncan McGreggor committed
170
171
1/81
ok
Duncan McGreggor's avatar
Duncan McGreggor committed
172
> (ratio:pp (ratio:pow r1 -4))
Duncan McGreggor's avatar
Duncan McGreggor committed
173
174
81/1
ok
Duncan McGreggor's avatar
Duncan McGreggor committed
175
> (ratio:pp (ratio:pow r2 4))
Duncan McGreggor's avatar
Duncan McGreggor committed
176
177
1/256
ok
Duncan McGreggor's avatar
Duncan McGreggor committed
178
> (ratio:pp (ratio:pow r2 -4))
Duncan McGreggor's avatar
Duncan McGreggor committed
179
180
256/1
ok
Duncan McGreggor's avatar
Duncan McGreggor committed
181
182
```

183
184
185
Large numbers:

```lfe
Duncan McGreggor's avatar
Duncan McGreggor committed
186
> (ratio:pp (ratio:div (ratio:pow 3 200) (ratio:pow 2 430)))
187
188
189
190
8960496791105607/93536104789177786765035829293842113257979682750464
ok
```

Duncan McGreggor's avatar
Duncan McGreggor committed
191
## API [↟](#table-of-contents)
Duncan McGreggor's avatar
Duncan McGreggor committed
192

Duncan McGreggor's avatar
Duncan McGreggor committed
193
194
195
196
197
198
199
200
The list of functions currently supported by the ratio library are as
follows:

```lfe
ratio:->atom/1
ratio:->float/1
ratio:->str/1
ratio:add/2
Duncan McGreggor's avatar
Duncan McGreggor committed
201
ratio:denom/1
Duncan McGreggor's avatar
Duncan McGreggor committed
202
203
ratio:div/2
ratio:eq/2
Duncan McGreggor's avatar
Duncan McGreggor committed
204
ratio:float->ratio/1
Duncan McGreggor's avatar
Duncan McGreggor committed
205
206
207
208
209
ratio:gcd/2
ratio:mult/2
ratio:new/1
ratio:new/2
ratio:normalize/1
Duncan McGreggor's avatar
Duncan McGreggor committed
210
211
212
ratio:normalize/2
ratio:numer/1
ratio:pow/2
Duncan McGreggor's avatar
Duncan McGreggor committed
213
ratio:pp/1
Duncan McGreggor's avatar
Duncan McGreggor committed
214
215
216
217
218
ratio:print/1
ratio:ratio/1
ratio:ratio/2
ratio:sub/2
```
Duncan McGreggor's avatar
Duncan McGreggor committed
219
220
221
222
223
224
225
226
227


## License [↟](#table-of-contents)

Apache Version 2 License

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


228
<!-- Named page links below: /-->
Duncan McGreggor's avatar
Duncan McGreggor committed
229

Duncan McGreggor's avatar
Duncan McGreggor committed
230
[logo]: priv/images/horatio.jpg
Duncan McGreggor's avatar
Duncan McGreggor committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
[org]: https://github.com/lfex
[github]: https://github.com/lfex/horatio
[gitlab]: https://gitlab.com/lfex/horatio
[travis]: https://travis-ci.org/lfex/horatio
[travis badge]: https://img.shields.io/travis/lfex/horatio.svg
[lfe]: https://github.com/rvirding/lfe
[lfe badge]: https://img.shields.io/badge/lfe-1.2.0-blue.svg
[erlang badge]: https://img.shields.io/badge/erlang-R16%20to%2019.1-blue.svg
[versions]: https://github.com/lfex/horatio/blob/master/.travis.yml
[github tags]: https://github.com/lfex/horatio/tags
[github tags badge]: https://img.shields.io/github/tag/lfex/horatio.svg
[github downloads]: https://img.shields.io/github/downloads/lfex/horatio/total.svg
[hex badge]: https://img.shields.io/hexpm/v/horatio.svg?maxAge=2592000
[hex package]: https://hex.pm/packages/horatio
[hex downloads]: https://img.shields.io/hexpm/dt/horatio.svg