LarryBattle / Ratio.js

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

Add floor() and ceil(), random(), and makeProper() #20

Closed LarryBattle closed 11 years ago

LarryBattle commented 11 years ago

Add floor() and ceil(), random(), and makeProper()

/**
* Returns a new Ratio from the floor of the current instance.
*
* @chainable
* @return {Ratio}
* @example 
Ratio.parse(4.2).floor().toString() === "4/1"
*/
Ratio.prototype.floor = function () {
    return this.clone(Math.floor(this.valueOf()), 1);
};
/**
* Returns a new Ratio from the ceil of the current instance.
*
* @chainable
* @return {Ratio}
* @example 
Ratio.parse(4.2).ceil().toString() === "5/1"
*/
Ratio.prototype.ceil = function () {
    return this.clone(Math.ceil(this.valueOf()), 1);
};
/**
* Returns a new Ratio by removing the integer part of the current instance.
* In otherwords, returns the decimal portion as a fraction.
*
* @chainable
* @return {Ratio}
* @example 
Ratio.parse(4.2).makeProper().toString() === "2/100"
*/
Ratio.prototype.makeProper = function () {
    return this.clone( Math.abs(this.numerator) - (Math.floor(this.valueOf()) * this.denominator), this.denominator );
};
LarryBattle commented 11 years ago

I'm not sure how the random function is going to work.

//???
Ratio.random = function( min, max, wholeNumberOnly ){
    var val = (min || 0) + Math.random() * (max||1);????
    return Ratio.parse(val);
};
LarryBattle commented 11 years ago

All but random have been added in Ratio version 0.3.6

LarryBattle commented 11 years ago

Done. All four methods added in Ratio-0.3.7.js