Additional JavaScript Math Functions, Utilities and Tools
(A no nonsense page)
NOTICE: These functions are public domain. Use at your own discretion.
I ask that you leave in the comment that pegs me as the creator, and if you modify the
scripts, please add a "// Modified (date) by (your name)" comment to the
script.
DISCLAIMER: I wouldn't use these functions in situations that could potentially lead
to injury or damage of person or property.
General Base Converter Convert a number with base 36 or
less to any other base of 36 or less. Hex to dec is a walk in the park for this function.
Variable Based Logarithm Compute logarithms with bases
other than the standard e or 10.
Modulo Computes the modulo (remainder)
function.
Custom Rounding Rounds to user specified number of
decimal places
Fraction Approximator Give a denominator limit, and it
will return the best fraction equivalent to a given decimal (up to that denominator
limit.)
All of these functions ready to be copied and pasted into
your page.
General Base Converter
Description: This is the base converter to beat all base converters!
Convert hex to dec, dec to hex, binary to oct, base 2 to base 36! You heard me right,
base 36.
Code:
How to Use: If someone tells you that 46406810206 is the
name of a short-lived Swedish band in hexadecimal, to find out you would enter:
baseConverter(46406810206,10,16);
To Convert 127005713555726 from base 8 to base 29 the line of code would be:
baseConverter(127005713555726,8,29);
Note: Output is a string, not a number.
CAUTION: Do not try to convert numbers greater
than 9007199254740992 (20000000000000 in hex.) It seems to be the boundary between
good and bad taste. Converting any number larger than that results in distorted
answers.
Explanation:
This is rather involved. If you are simply dying for a more detailed explanation please email me. Basically though, this function first
converts the number to base 10 and then from there to the new base. I have been working
on a way to eliminate the base 10 middle step, but it would require a totally different
methodology than what is being used at present.
Description: Round your decimals to as few, or as many places
as you desire! Life can't get much better than this.
Code:
function custRound(x,places) {
// Created 1997 by Brian Risk. http://brianrisk.com
return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
}
How to Use: To round 3.14159265 to 4 decimal places type:
custRound(3.14159265,4);
Explanation:
Basically what it does is multiplies the number in question by 10 to the power of
the number of digits to be rounded to. It is then rounded and then divided by 10 to
the number of digits... Voila.
Description: Have you ever looked at a decimal and thought to yourself,
"Ya know, I wonder what fraction would best approximate that if I had to limit
how large the denominator could be." I know you have, so you can stop
your wondering, kiddo.
Code:
function fractApprox(x,maxDenominator) {
// Created 1997 by Brian Risk. http://brianrisk.com
maxDenominator = parseInt(maxDenominator);
var approx = 0;
var error = 0;
var best = 0;
var besterror = 0;
for (var i=1; i <= maxDenominator; i++) {
approx = Math.round(x/(1/i));
error = (x - (approx/i))
if (i==1) {best = i; besterror = error;}
if (Math.abs(error) < Math.abs(besterror))
{best = i; besterror = error;}
}
return (Math.round(x/(1/best)) + "/" + best);
}
How to Use: To approximate 1.58496 with the largest
denominator being 16, type:
fractApprox(1.58496,16);
Note: Output is a string, not a number.
Explanation:
Basically, this function goes from 1 to your given upper bound, calculates the best
fraction for each denominator in the series, finds the difference between the approximated
answer and the actual answer, and the fraction with the smallest difference between it
and the objective wins.