Full version: jsB@nk » Calculation » Math » Vitaliy's Math Javascript Library
URL: https://www.javascriptbank.com/vitaliy-math-javascript-library-demo.html
Vitaliy's Math Javascript Library - is the collection of mathematical routines not included in JavaScript built-in objects (Math, Number) . The tool on this page is used to demonstrate how these functions work. This is still a beta version of the library, since testing is not complete yet.
Full version: jsB@nk » Calculation » Math » Vitaliy's Math Javascript Library
URL: https://www.javascriptbank.com/vitaliy-math-javascript-library-demo.html
<script language="JavaScript"><!--/************************************************************************************//* math.js - the library of mathematical functions that are not part of Math object.*//* AUTHOR: Vitaliy Rabotnik WWW: http://chat.carleton.ca/~vrabotni/JavaScript/Math/ *//* VERSION *//* VERSION: 1 DATE: 01/03/2000 *//************************************************************************************//****************************************************************************//* DESCRIPTION: Returns a string representing expansion of (x + y)^n. *//* PARAMETERS: n - a positive integer; *//* format(optional) - the output format either text ("txt") or html ("html")*//* default is "html" format *//****************************************************************************/function binomial(n, format){ var out = ""; var temp; var pow1, pow2; if(format == "txt") { pow1 = "^";pow2 =""; } else { pow1 = "<sup>";pow2 = "</sup>"; } if(!isInteger(n) || n < 0) { // alert("Error \"binomial\": " + n + " is not valid number."); return; } for(var i = 0; i <= n; i++) { temp = choose(n,i);if(temp > 1) out += temp;if(i > 1) out += "x" + pow1 + i + pow2; if(i == 1) out += "x"; if(n - i > 1) out += "y" + pow1 + (n - i) + pow2; if(n - i == 1) out += "y"; if(i < n) out += " + "; } return out;}/*******************************************************************************************//* DESCRIPTION: Returns the result of "n choose r"; Returns null if "n" and r are invalid.*//* PARAMETERS: n and r - are positive integers, where r is less or equal to n. *//*******************************************************************************************/function choose(n,r){ if(!isInteger(n) || !isInteger(r)) { // alert("Error \"choose\": " + n + " or " + r + " are not valid number."); return; } if(r > n) { // alert("Error \"choose\": " + r + " must be less or equal to " + n + "."); return; } return factorial(n)/(factorial(n - r) * factorial(r));}/************************************************************************************//* DESCRIPTION: Returns the factorial of namber "n"; Returns null if "n" is invalid.*//* PARAMETERS: n - a positive integer *//************************************************************************************/function factorial(n){ if(n < 0 || !isInteger(n)) { // alert("Error \"factorial\": " + n + " is not valid number."); return; } if(n == 0) return 1; if(n == 1) return n; return (n * factorial(n - 1));}/*******************************************************************************//* DESCRIPTION: Returns n th fibonacci number; Returns null if "n" is invalid. *//* PARAMETERS: n - a positive integer greater than zero *//*******************************************************************************/function fibonacci(n){ if(n < 1 || !isInteger(n)) { // alert("Error \"fibonacci\": " + n + " is not valid number."); return; } if(n == 1 || n == 2) return 1; return fibonacci(n - 1) + fibonacci(n - 2);}/********************************************************************//* DESCRIPTION: Returns wheather the number "n" is an even number. *//* PARAMETERS: n - an integer or a string representing integer. *//********************************************************************/function isEven(n){ if(!isInteger(n)) { // alert("Error \"roundOff\": " + n + " or " + r + " is not valid number."); return; } return(n%2 == 0);}/*************************************************************//* DESCRIPTION: Returns wheather the number "n" is a float. *//* PARAMETERS: n - a float or a string representing float. *//*************************************************************//*function isFloat(n){ return (parseFloat("" + n) === n);} *//******************************************************************//* DESCRIPTION: Returns wheather the number "n" is an integer. *//* PARAMETERS: n - an integer or a string representing integer. *//******************************************************************/function isInteger(n){ if(isNaN(n)) return false; return(parseInt("" + n) == n);} /******************************************************************//* DESCRIPTION: Returns wheather the number "n" is a prime number.*//* PARAMETERS: n - a positive integer greater than zero. *//******************************************************************/function isPrime(n){ if(!isInteger(n) || n < 1) { // alert("Error \"isPrime\": " + n + " is not valid number."); return; } for(var i = 2; i < n - 1; i++) { if(n % i == 0) return false; } return true;} /********************************************************************//* DESCRIPTION: Returns wheather the number "n" is an odd number. *//* PARAMETERS: n - an integer or a string representing integer. *//********************************************************************/function isOdd(n){ if(!isInteger(n)) { // alert("Error \"roundOff\": " + n + " or " + r + " is not valid number."); return; } return(!isEven(n));}/********************************************************************//* DESCRIPTION: Logarithm of number "n" to the base a. *//* PARAMETERS: n - a number; a - base. *//********************************************************************/function log(n,a){ if(isNaN(a) || isNaN(n)) return; return(Math.log(n)/Math.log(a));}/*********************************************************************//* DESCRIPTION: Rounds off the number "n" to "digits" decimal places.*//* PARAMETERS: n - a number; digits - number of decimal places. *//*********************************************************************/function roundOff(n,digits){ var temp; if(isNaN(n) || !isInteger(digits)) { // alert("Error \"roundOff\": " + n + " or " + r + " is not valid number."); return; } if(digits < 0) { // alert("Error \"roundOff\": " + digits + " must be greater or equal to zero."); return null; } if(digits == 0) return Math.round(n); temp = Math.pow(10,digits) return(Math.round(n * temp)/temp);}/********************************************************************************//* DESCRIPTION: Returns a string representation of the binary convertion of "n".*//* PARAMETERS: n - a positive integer. *//********************************************************************************/function toBinary(n){ var out = ""; var temp; if(!isInteger(n) || n < 0) { // alert("Error \"toBinary\": " + n + " is not valid number."); return; } if(n == 0) return 0; temp = n; while(temp > 0) { out = (temp % 2) + out; temp = parseInt(temp / 2); } return out;}/********************************************************************************//* DESCRIPTION: Returns an integer. *//* PARAMETERS: n - a string representation of the binary number. *//********************************************************************************/function toDecimal(n){ var out = 0; var temp = ""; var startFound = false; temp += n; if(!validBinaryNumber(temp)) return; for(var i = 0; i < temp.length; i++) { if(temp.charAt(i) == '1') startFound = true; if(startFound) out += parseInt(temp.charAt(i)) * Math.pow(2, temp.length - (i + 1)); } return out;}function validBinaryNumber(n){ for(var i = 0; i< n.length; i++) { if(n.charAt(i) != '0' && n.charAt(i) != '1') return false; } return true; }//--></script><script language="JavaScript">var functionNames;var rules;var farguments;var n, r;var IE = document.all;var NS = document.layers;function setText(what,str){ if(IE) what.innerHTML = str; else if(NS) { what.document.open(); what.document.write(str);what.document.close(); }}function getInput(){ var f = document.forms[0]; var list = f.functions; var args = farguments[list.selectedIndex]; var func = functionNames[list.selectedIndex]; var type = inputTypes[list.selectedIndex]; n = prompt("Please enter the value for n: ", ""); if(n == null) { alert("The action was aborted by user!") return;} if(type[0] == "int") n = parseInt(n); else if (type[0] == "float") n = parseFloat(n); if(args == 2) { r = prompt("Please enter the value for r: ", ""); if(r == null) { alert("The action was aborted by user!") return; } if(type[1] == "int") r = parseInt(r); else if (type[1] == "float") r = parseFloat(r); } solve(func,args); }function solve(func, args){ var field = getField("answ"); var msg = "<b>Answer is:</b> "; if(args == 1) { if(isNaN(n)) msg += eval(func + "(\"" + n + "\")"); else msg += eval(func + "(" + n + ")"); } else if(args == 2) msg += eval(func + "(" + n + "," + r + ")"); setText(field, msg);}function update(ptr){ var index = ptr.selectedIndex; setText(getField("description"), rules[index]); setText(getField("answ"), "");}function getField(name){ if(IE) return document.all[name]; else if(NS) { return document.layers[name]; }}// add an Option object into a combo box at indexfunction addOption(list, index, opt){ list.options[index] = opt;}// fill up a combobox with Option objectsfunction fillUpList(list, options, values){ for(var i = 0; i < options.length; i++) addOption(list, i, new Option(options[i]),values[i]); list.selectedIndex = 0; }function fixStupidNetscapeBug(){ var pointer = document["ptr"];var troubleLayer = document.layers["answ"];troubleLayer.top = pointer.pageY;troubleLayer.left = pointer.pageX;}</script><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->
<body onload="init();"><form><table cellspacing="7" cellpadding="7"><tbody><tr><td><select name="functions" onchange="update(this);"><option>binomial</option><option>choose</option><option>factorial</option><option>fibonacci</option><option>isEven</option><option>isInteger</option><option>isPrime</option><option>isOdd</option><option>log</option><option>roundOff</option><option>toBinary</option><option>toDecimal</option></select><input type="button" value="Calculate" onclick="getInput();"></td><td><span id="answ"><ilayer name="ptr"></ilayer><layer name="answ"></layer></span></td></tr></tbody></table><p><span id="description"><layer name="description"></layer></span><script language="JavaScript">function init(){ var f = document.forms[0]; functionNames = new Array("binomial", "choose", "factorial", "fibonacci", "isEven", "isInteger", "isPrime", "isOdd", "log", "roundOff", "toBinary", "toDecimal"); rules = new Array(); rules[0] = "<b>binomial(n)</b> - returns binomial expansion of equation (x + y)<sup>n</sup>.<br><i>n</i> must be a positive integer."; rules[1] = "<b>choose(n,r)</b> - returns number of different groups of size <i>r</i> selected from a set of <i>n</i> objects, where <i>r <= n</i>."; rules[2] = "<b>factorial(n)</b> - returns a factorial of the number <i>n</i>. <br><i>n</i> must be a positive integer."; rules[3] = "<b>fibonacci(n)</b> - returns <i>n<sup>th</sup></i> fibonacci number. <br><i>n</i> must be a positive integer."; rules[4] = "<b>isEven(n)</b> - whether <i>n</i> is an even number.<br><i>n</i> must be an integer."; rules[5] = "<b>isInteger(n)</b> - whether <i>n</i> is an integer."; rules[6] = "<b>isPrime(n)</b> - whether <i>n</i> is a prime number.<br><i>n</i> must be an integer greater than zero."; rules[7] = "<b>isOdd(n)</b> - whether <i>n</i> is an odd number.<br><i>n</i> must be an integer."; rules[8] = "<b>log(n,r)</b> - returns logarithm of <i>n</i> to the base <i>r</i>."; rules[9] = "<b>roundOff(n,r)</b> - rounds off <i>n</i> to specified (<i>r</i>) number of digits."; rules[10] = "<b>toBinary(n)</b> - returns a string representing binary number conversion of <i>n</i>.<br><i>n</i> must be a positive integer."; rules[11] = "<b>toDecimal(n)</b> - returns a decimal integer representation of binary string <i>n</i>.<br><i>n</i> must be a valid binary string."; farguments = new Array(1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1); inputTypes = new Array(); inputTypes[0] = new Array("int"); inputTypes[1] = new Array("int","int"); inputTypes[2] = new Array("int"); inputTypes[3] = new Array("int"); inputTypes[4] = new Array("int"); inputTypes[5] = new Array("any"); inputTypes[6] = new Array("int"); inputTypes[7] = new Array("int"); inputTypes[8] = new Array("float", "float"); inputTypes[9] = new Array("float","int"); inputTypes[10] = new Array("int"); inputTypes[11] = new Array("str"); fillUpList(f.functions, functionNames, functionNames); setText(getField("description"), rules[0]); if(NS) fixStupidNetscapeBug();}</script></form></body><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->