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.

Demo:        Your start number is:
The base of that number is:
The base to convert it to is:
Answer:
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.
[Return to Top]





Variable Based Logarithm

Description: Tired of being limited to Log base 10 or base e? Want something new? Try this function on for size.

Demo:        Log of Answer:
Code:
function custLog(x,base) {
	// Created 1997 by Brian Risk.  http://brianrisk.com
	return (Math.log(x))/(Math.log(base));
}
How to Use:    For example, to return the answer for Log 2 16, type:
    custLog(16,2);
Explanation: This function implements the rule:

Log y z = ( Log x z ) / ( Log x y )
[Return to Top]





Custom Rounding

Description: Round your decimals to as few, or as many places as you desire! Life can't get much better than this.

Demo:        Round to decimal places.
Answer:
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.
[Return to Top]





Fraction Approximator

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.

Demo:        Approximate the fraction for with the maximum possible denominator being of . Answer:
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.
[Return to Top]






function custLog(x,base) {
	// Created 1997 by Brian Risk.  http://brianrisk.com
	return (Math.log(x))/(Math.log(base));
}

function custRound(x,places) {
	// Created 1997 by Brian Risk.  http://brianrisk.com
	return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
}

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);
}


function baseConverter (number,ob,nb) {
	// Created 1997 by Brian Risk.  http://brianrisk.com
	number = number.toUpperCase();
	var list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var dec = 0;
	for (var i = 0; i <=  number.length; i++) {
		dec += (list.indexOf(number.charAt(i))) * (Math.pow(ob , (number.length - i - 1)));
	}
	number = "";
	var magnitude = Math.floor((Math.log(dec))/(Math.log(nb)));
	for (var i = magnitude; i >= 0; i--) {
		var amount = Math.floor(dec/Math.pow(nb,i));
		number = number + list.charAt(amount); 
		dec -= amount*(Math.pow(nb,i));
	}
	return number;
}


[Return to Top]