-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
feat: add decimal to fraction algorithm #1439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a5f3c3f
e240332
6dbea14
2abb00d
560127f
82fd7fc
be441e7
1e2000d
aa71162
c33f80e
ca69ae3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @function fraction | ||
* @description This function returns the fraction of a float type number. | ||
* @param {number} num - a float type number is expected, but an integer will also work. | ||
* @param {number} accuracy - the accuracy of the fraction, the default is 6. like if the accuracy is 2 then for 1.333 result will be 10/9, where for 6 it will return 1333/1000. | ||
* @return {Array} - an array containing the numerator and denominator of the fraction. | ||
* @see https://en.wikipedia.org/wiki/Repeating_decimal and | ||
* @see https://en.wikipedia.org/wiki/Decimal#Decimal_fractions | ||
* @example fraction(0.5) // [1, 2] | ||
* @example fraction(0.3333333333333333) // [1, 3] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are all these examples really useful? |
||
* @example fraction(0.25) // [1, 4] | ||
* @example fraction(5.56) // [139, 25] | ||
* @example fraction(0.33) // [33, 100] | ||
* @example fraction(0.33,2) // [10, 3] | ||
*/ | ||
function fraction(number, accuracy = 6) { | ||
if (typeof number === "number" && !Number.isNaN(number) && Number.isFinite(number) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're still not using guards here; these checks are all redundant with the if-throws in the |
||
&& typeof accuracy === "number" && !Number.isNaN(accuracy) && accuracy >= 1 && accuracy <= 16) { | ||
let neg = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd call this sign. Also, I'd just do |
||
// if number is a negative then following code will run | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant comment. |
||
if (number < 0) { | ||
neg = -1; | ||
number = Math.abs(number); | ||
} | ||
// if number is a 0 then it will return [0, 1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant comment. |
||
if (number === 0) return [0, 1]; | ||
if (Number.isInteger(number)) return [neg * number, 1]; | ||
// if number is a not an integer then following code will run | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant comment. |
||
number = number.toString(); | ||
let len; | ||
let reg = number.match(/(\d+?)\1+$/); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a hacky way to deal with "repeating" fractional parts, and I'd argue that it solves the wrong problem. Since we're dealing with finite-size numbers (floats) here, there are no "repeating" decimals; we can't distinguish a "repeating" decimal from a "deliberately ending decimal". We need to treat both the same (it seems "accuracy" is more of a "threshold" of when you consider a decimal repeating?). In fact, all floats can even be represented exactly as fractions (though perhaps maybe not as fractions of floats, that might get hairy towards some edge cases) where the denominator is a power of two and the numerator is an integer. I wouldn't exclude the possibility that some special provisions for repeating decimals may be useful, but then what problem this exactly solves (and how) and what guarantees it makes should be explicitly stated. |
||
// if number is a repeating decimal then following code will run | ||
if (reg && reg[0].length > accuracy) { | ||
let pos = number.split("."); | ||
number = number.replace(reg[0], reg[1]); | ||
let rec = pos[0] + pos[1].replace(reg[0], ""); | ||
number = Number(rec + reg[1]) - Number(rec); | ||
len = Number( | ||
"9".repeat(reg[1].length) + "0".repeat(rec.length - pos[0].length) | ||
); | ||
} else { | ||
// if number is not a repeating decimal then following code will run | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again a redundant comment. |
||
number = number.replace(".", ""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String manipulation for manipulating numbers is dirty here. Just multiply your numerator by the denominator, then round. Shouldn't the denominator be determined by the accuracy? |
||
len = 10 ** (number.length - 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not apparent to me why this is correct: Isn't |
||
number = Number(number); | ||
} | ||
// it will find out the gcd of the number and the len to reduce the fraction nomitor and denominator like 4/8 will be 1/2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is called "shortening" the fraction. |
||
let div = gcd(number, len); | ||
number /= div; | ||
len /= div; | ||
return [neg * number, len]; | ||
} else { | ||
if (typeof number !== "number") throw new TypeError("Invalid number, a number type value expected"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These "guards" should all be moved to the top of the function. They could also be simplified a bit. |
||
if (typeof accuracy !== "number") throw new TypeError("Invalid accuracy, a number type value expected"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant with the negated |
||
if (Number.isNaN(number)) throw new TypeError("Invalid number, a number type value expected"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The NaN check is redundant with the negated |
||
if (Number.isNaN(accuracy)) throw new TypeError("Invalid accuracy, a number type value expected"); | ||
if (!Number.isFinite(number)) throw new RangeError("Invalid number, a finite number expected"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep only this check for |
||
if (accuracy < 1 || accuracy > 16) throw new RangeError("Invalid accuracy, a integer type value expected between 1 and 16"); | ||
} | ||
} | ||
|
||
function gcd(a, b) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a gcd implementation in this repo. Import and reuse it. |
||
if (b == 0) return a; | ||
return gcd(b, a % b); | ||
} | ||
|
||
export { fraction }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { fraction } from './../Fraction.js'; | ||
|
||
describe('Fraction', () => { | ||
it('should return [1, 2] for 0.5', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests should use |
||
expect(fraction(0.5)).toEqual([1, 2]); | ||
}); | ||
it('should return [1, 3] for 0.3333333333333333', () => { | ||
expect(fraction(0.3333333333333333)).toEqual([1, 3]); | ||
}); | ||
it('should return [1, 4] for 0.25', () => { | ||
expect(fraction(0.25)).toEqual([1, 4]); | ||
}); | ||
it('should return [0, 1] for 0', () => { | ||
expect(fraction(0)).toEqual([0, 1]); | ||
}); | ||
it('should return [139, 25] for 5.56', () => { | ||
expect(fraction(5.56)).toEqual([139, 25]); | ||
}); | ||
it('should return [33, 100] for 0.33', () => { | ||
expect(fraction(0.33)).toEqual([33, 100]); | ||
}); | ||
it('should return [5, 1] for 1', () => { | ||
expect(fraction(5)).toEqual([5, 1]); | ||
}); | ||
it('should return [3333, 1000] for 3.333', () => { | ||
expect(fraction(3.33)).toEqual([333, 100]); | ||
}); | ||
it('should return [1, 3] for 0.3333', () => { | ||
expect(fraction(0.3333, 3)).toEqual([1, 3]); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please formulate this mathematically - something like "fraction - num <= 10^-accuracy".