Video: Metaprogramming in Groovy
Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94
This post improves the code at “Code of the Day Javascript Decimal to fraction” by using Euclid’s Algorithm to find the Great Common Factor or GCF, aka GCD. This method provides a speed up of 10x when compared to the original algorithm posted.
Code
/** *@function gcd (aka Euclid's algorithm) *@purpose returns the greatest common factor between two numbers; */ function gcd(a, b) { return (b) ? gcd(b, a % b) : a; } /** *@function dec2Frac *@purpose returns a decimal as a fraction. */ var dec2Frac = function ( num ) { var top = num.toString().replace(/\d+[.]/, ''); var bot = Math.pow(10, top.length); if (num > 1) { top = +top + Math.floor(num) * bot; } var x = gcd(top, bot); return (top / x) + "/" + (bot / x); }; |
Usage
dec2Frac( 1.24325 ) // returns "4973/4000" dec2Frac( 2.45 ) // returns "49/20" |
Speed Test between Version 1 and 2 of dec2Frac()
//@requires Firebug or Chrome Developer Tools //The following code test the speed difference between the Version 1 and 2 of dec2Frac. function gcd(a, b) { return (b) ? gcd(b, a % b) : a; } var dec2FracV2 = function (d) { var top = d.toString().replace(/\d+[.]/, ''); var bot = Math.pow(10, top.length); if (d > 1) { top = +top + Math.floor(d) * bot; } var x = gcd(top, bot); return (top / x) + "/" + (bot / x); }; function dec2FracV1(d) { var df = 1, top = 1, bot = 1; var limit = 1e5; //Increase the limit to get more precision. while (df != d && limit-- > 0) { if (df < d) { top += 1; } else { bot += 1; top = parseInt(d * bot, 10); } df = top / bot; } return top + '/' + bot; } var getRandomNum = function (i, decLen) { return (Math.random() * (i || 1)).toFixed(decLen); }; var runTest = function ( funcName ) { var func = { "V1": dec2FracV1, "V2": dec2FracV2 }[ funcName ]; var i = 1000; while (i--) { num = getRandomNum(10, 4); var x = func(num); if (num != eval(x)) { console.warn("ERROR: Function %s, The fraction %s != %s", funcName, num, x); } } }; console.time( 'V1' ); runTest( "V1" ); console.timeEnd( 'V1' ); console.time( 'V2' ); runTest( "V2" ); console.timeEnd( 'V2' ); |
Chrome Result:
V1: 11628ms
V2: 185ms
Try the test out yourself
jsbin.com
Source: mivk from StackOverflow.com
Code
/** @function dec2frac @returns string @purpose Convert a decimal to a fraction */ function dec2frac(d) { var df = 1, top = 1, bot = 1; var limit = 1e5; //Increase the limit to get more precision. while (df != d && limit-- > 0) { if (df < d) { top += 1; } else { bot += 1; top = parseInt(d * bot, 10); } df = top / bot; } return top + '/' + bot; } |
Demo
JSBIN link
Example Output
dec2frac ( 0.1) returns “1/10”
dec2frac ( 0.2) returns “1/5”
dec2frac ( 0.5) returns “1/2”
dec2frac ( 0.9999) returns “9999/10000”
dec2frac ( 0.75) returns “3/4”
dec2frac ( 2.718281828459045) returns “135915/50000”, but this evaluates to 2.7183
dec2frac ( 3.141592653589793) returns “157079/50000”, but this evaluates to 3.14158

What’s Groovy?
Groovy is an agile and dynamic language for the Java Virtual Machine and much more. Check out the
Groovy Homepage for more information.
Installation Guide:
Requires: Java JDK 1.5+ (Download here)
Windows-Installer for Groovy 1.8.4: (Download Here)
Alternative installation method: Guide.
You can check to see if groovy was properly installed by trying “groovy –version” in the command prompt.
Note: You need to close and re-open the console after changing the path environment variable.