Skip to content

Instantly share code, notes, and snippets.

@jiggzson
jiggzson / MathML2Expression.js
Created February 5, 2020 14:50
Convert MathML to an expression
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function(search, rawPos) {
var pos = rawPos > 0 ? rawPos|0 : 0;
return this.substring(pos, pos + search.length) === search;
}
});
}
function MLParser() {
/*
https://sciencenotes.org/projectile-motion-example-problem-physics-homework-help/
A cannon is fired with muzzle velocity of 150 m/s at an angle of elevation = 45°. Gravity = 9.8 m/s2.
a) What is the maximum height the projectile reaches?
b) What is the total time aloft?
c) How far away did the projectile land? (Range)
d) Where is the projectile at 10 seconds after firing?
*/
var knowns = {
v0: 150, // m/s
@jiggzson
jiggzson / removeDuplicates.js
Created December 13, 2019 21:28
This function removes duplicates from an array. It can also be used to remove duplicates that are near another value with a tolerance
var removeDuplicates = function(arr, condition) {
var conditionType = typeof condition;
if(conditionType !== 'function' || conditionType === 'undefined') {
condition = function(a, b) {
return a === b;
};
}
var seen = [];
@jiggzson
jiggzson / toScientific.js
Last active December 4, 2019 22:04
Uses strings instead of Number to convert to Scientific. Enables use with big decimal library
/*
* Javascript has the toExponential method but this allows you to work with string and therefore any number of digits of your choosing
* For example Scientific('464589498449496467924197545625247695464569568959124568489548454');
*/
var isInt = function(n) {
return n%1 === 0;
}
var nround = function (x, s) {
if (isInt(x)) {
if (x >= Number.MAX_VALUE)
@jiggzson
jiggzson / convert_number_bases.js
Created January 16, 2019 04:49
Convert between different number bases. No radix of 36 restriction
/**
* Convert a number to base 10 give a base and a number array
* @param {int[]} n_array
* @param {int} base
* @returns {int}
*/
var to_base10 = function(n_array, base) {
n_array.reverse();
var n = 0;
for(var i=0, l=n_array.length; i<l; i++) {
@jiggzson
jiggzson / sqrt.js
Created January 6, 2019 05:07
Calculate the square root using Newton's method
function sqrt(n) {
var xn, d, ld;
var c = 0; //counter
var done = false;
var delta = 1e-17;
xn = n/2;
var safety = 1000;
do {
//break if we're not converging
if(c > safety)
@jiggzson
jiggzson / log.js
Created May 17, 2018 02:12
This calculates the log of a number and is independent of Math.log. This can be used with a big number or bigInt library to calculate a custom precision value of log.
//TODO: Still depends on Math.sqrt and Math.pow
var factorial = function(n) {
++n;
var r = 1;
while(n-->1)
r*=n;
return r;
};
var PI = function(n) {
@jiggzson
jiggzson / factorial.js
Last active April 21, 2019 02:12
A fast factorial function
var factorial = function(n) {
if(n === 0)
return 1;
var f = n;
while(n > 1) {
f *= --n;
}
return f;
};
var factorial = function(n) {
++n;
var r = 1;
while(n-->1)
r*=n;
return r;
}
var calculatePI = function(n) {
n = n || 30;
@jiggzson
jiggzson / floating_point.js
Last active February 5, 2018 16:05
A wrapper to deal with floating point numbers in JavaScript. It does a rough conversion to a fraction and then does the operation. The toDecimal method returns the number back as a decimal. For larger numbers use a bigNumber library
function F(d) {
if(!(this instanceof F))
return new F(d);
if(Array.isArray(d))
this.fraction = d;
else
this.fraction = this.convert(d);
}