Skip to content

torch/rational

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 

Rational numbers

Implement rational number operations, with appropriate operators. Example of usage:

> rat = require 'rational'

> print(rat(5, 4) * rat(3, 6))
5/8

> print(rat(5, 4) + rat(3, 6))
7/4

> print(rat(5, 2) - rat(3, 6))
2

> print(rat(5, 2) / rat(3, 6))
5

> print(rat(5, 2)^3)
125/8

> print(-rat(5, 6))
-5/6

> print(rat(5, 6) == rat(25, 30))
true

> print(rat(5, 6) <= rat(25, 30))
true

> print(rat(5, 6) < rat(5, 4))
true

> print(rat(5, 6) <= rat(5, 4))
true

> print(rat(5, 6) > rat(5, 4))
false

> print(rat.max(rat(5, 6), rat(5, 4)))
5/4

> print(rat.min(rat(5, 6), rat(5, 4)))
5/6

Converting to a number can be done with the () operator:

print(rat(5, 6)())
0.83333333333333

About

rational numbers for lua

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages