LarryBattle / Ratio.js

Rational numbers for Javascript
http://larrybattle.github.com/Ratio.js/
MIT License
113 stars 9 forks source link

Match accuracy and irrational numbers #54

Open m1sta opened 10 years ago

m1sta commented 10 years ago

Great library. Have a feature request.

It would be great if ratio.js could achieve the following...

var oneThird = Ratio.parse(1/3, 10)
var pi = Ratio.parse("3.14159", 10)
var piPlusThree = Ratio.parse("6.1314159", 10)

I appreciate that the last one might be infeasable for the time being. I'm thinking that the first two might be possible if the second argument is considered some kind of 'match precision' or 'iterations to attempt match'. Basically, if the provided input matches an irrational or a repeating rational after a certain number of iterations, the existence of this parameter notes that the library user wants to take the irrational if it exists.

LarryBattle commented 10 years ago

How is this different than using .simplify() and .toQuantityOf()?

Ratio.parse(1/3, 10).valueOf() === 0.03333333333333333
Ratio.parse(1/3, 10).toString() === "3333333333333333/100000000000000000"
Ratio.parse(1/3, 10).simplify().toString() === "1/30"
Ratio.parse(1/3, 10).toQuantityOf(10).toString() === "0/10"
Ratio.parse(1/3).simplify().toString() === "1/3"
Ratio.parse(1/3).toQuantityOf(10).toString() === "1/30"

Please provide a few examples of the input and expected output.

m1sta commented 10 years ago

Ratio appears to do some approximations. I'm looking for control over the approximation rules.

I'd love to see the api able to take the number 0.3333 and a configuration around the 'accuracy to three decimal places' and for Ratio to convert the 0.3333 to 1/3. Same story with pi and other major irrational numbers.

My original request was obviously not ideal given that Ratio.parse(x,y) already means something else. Perhaps making the 'accuracy' an argument of simplify()?