Skip to content

Commit

Permalink
added the posibility to recognize floats
Browse files Browse the repository at this point in the history
  • Loading branch information
ashnur committed Sep 11, 2013
1 parent cc1dfd2 commit 8fb8f79
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ r(1,2).plus(r(1,3)).minus(r(1,4)).times(r(1,5)).per(r(1,6)) === r(49,70); // tru
// my personal favorite aproximation of Pi, from ancient china
r(355,113).val(); // 3.1415929203539825
// you can give floats and they will be converted into rationals
rats(0.4,0.1) == rats(4);
```

Expand Down Expand Up @@ -89,7 +91,7 @@ r(355,113).val(); // 3.1415929203539825
`r(355,113).val(); //3.1415929203539825`

# Good to know
If you provide anything else as the numerator, than a whole number in base 10 an exception will be thrown.
If you provide anything else as the numerator, than an integer, float or a numerica string an exception will be thrown.
On the other hand, if you do the same with the denominator, it will be cast to 1. This is because
I am lazy, and I do not want to handle wrong values and undefined values differently throwing for the former
and casting to 1 for the latter.
Expand Down
38 changes: 30 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,42 @@ void function(root){

})


function rat(numerator, denominator){

var index, divisor;
var index, divisor, dendecimals = 0, numdecimals = 0

if ( ! u.isInt(numerator) ) {
if ( typeof numerator != 'number' && typeof numerator != 'string'
&& (! u.isInt(numerator)) ) {
throw new Error('invalid argument '+numerator+' ('+(typeof numerator)+')')
} else {
numerator = big(numerator)
}
if ( typeof denominator != 'number' && typeof denominator != 'string'
&& (! u.isInt(denominator)) ) {
denominator = 1
}
debugger;
numerator = numerator+''
denominator = denominator+''

if ( ! u.isInt(denominator) ) {
denominator = big.ONE
} else {
denominator = big(denominator)
if ( numerator.indexOf('.') > -1 ) {
numdecimals = Math.pow(10, numerator.length - numerator.indexOf('.') - 1)
numerator = numerator.split('.').join('')
}

if ( denominator.indexOf('.') > -1 ) {
dendecimals = Math.pow(10, denominator.length - denominator.indexOf('.') - 1)
denominator = denominator.split('.').join('')
}

denominator = big.parse(denominator)
numerator = big.parse(numerator)

if ( dendecimals > 0 ) {
numerator = numerator.multiply(dendecimals)
}

if ( numdecimals > 0 ) {
denominator = denominator.multiply(numdecimals)
}

if ( denominator.isZero() ) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rationals",
"description": "rational numbers with operations",
"version": "0.1.6",
"version": "0.1.7",
"main": "index.js",
"scripts": {
"test": "mocha",
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ y = rats(3, 14)
third = rats(1, 3)


describe('common usage', function(){
it('recognizes decimal numbers, converts them into fractions', function(){
expect(rats(0.5)).to.be(rats(1,2))
expect(rats(0.33)).to.be(rats(33,100))
expect(rats(0.4,0.1)).to.be(rats(4))
})
})
describe('elementary arithmetic', function() {
it('addition', function() {
expect(one.plus(two)).to.be(three)
Expand Down Expand Up @@ -110,4 +117,5 @@ describe('operations with infinity and the origo → ', function() {
expect(origo.div(x)).to.be(origo)
})


})

0 comments on commit 8fb8f79

Please sign in to comment.