Last active
April 18, 2024 02:51
-
-
Save burgil/d64f86bdc1b9a7cfa37c828c0e6dfd1c to your computer and use it in GitHub Desktop.
#Fixed math, the universe and everything in between ! Serverless compatible Math-Only JavaScript EVAL ! Introducing math eval - A math only eval that can actually calculate numbers unlike python and javascript lol, and does not require any library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const evalMath = (str) => { | |
const operatorToFunction = { | |
"+": (a, b) => a + b, | |
"-": (a, b) => a - b, | |
"*": (a, b) => a * b, | |
"/": (a, b) => a / b | |
}; | |
const operationStr = str.replace(/\s/g, ''); | |
const numbers = operationStr.split(/[-+*/]/).map(Number); | |
const operators = operationStr.split(/\d+/).filter(Boolean).filter(operator => operator !== '.'); | |
for (let i = 0; i < operators.length; i++) { | |
const operator = operators[i]; | |
const nextNumber = numbers[i + 1]; | |
if (operator === '*' || operator === '/') { | |
numbers[i] = operatorToFunction[operator](numbers[i], nextNumber); | |
numbers.splice(i + 1, 1); | |
operators.splice(i, 1); | |
i--; | |
} | |
} | |
let result = numbers[0]; | |
for (let i = 0; i < operators.length; i++) { | |
const operator = operators[i]; | |
const nextNumber = numbers[i + 1]; | |
result = operatorToFunction[operator](result, nextNumber); | |
} | |
const numberString = result.toFixed(15); | |
const decimalIndex = numberString.indexOf('.'); | |
let trimmedNumberString = numberString; | |
if (decimalIndex !== -1) { | |
let i = numberString.length - 1; | |
while (numberString[i] === '0' && i > decimalIndex) { | |
i--; | |
} | |
trimmedNumberString = numberString.substring(0, i + 1); | |
if (trimmedNumberString.charAt(trimmedNumberString.length - 1) === '.') trimmedNumberString = trimmedNumberString.slice(0, -1); | |
} | |
const finalResult = parseFloat(trimmedNumberString); | |
// test: (compares itself to eval) | |
const DEBUG = true; | |
if (DEBUG) { | |
const PERFORM_TEST = false; // <<<<< enable this | |
if (!PERFORM_TEST) { | |
console.log(str, '=', finalResult) | |
} else { | |
const ONLY_SHOW_INVALID = false; | |
try { | |
const evalResult = eval(str); | |
const numberString2 = evalResult.toFixed(15); | |
const decimalIndex2 = numberString2.indexOf('.'); | |
let trimmedNumberString2 = numberString2; | |
if (decimalIndex2 !== -1) { | |
let i2 = numberString2.length - 1; | |
while (numberString2[i2] === '0' && i2 > decimalIndex2) { | |
i2--; | |
} | |
trimmedNumberString2 = numberString2.substring(0, i2 + 1); | |
if (trimmedNumberString2.charAt(trimmedNumberString2.length - 1) === '.') trimmedNumberString2 = trimmedNumberString2.slice(0, -1); | |
} | |
const finalEvalResult = parseFloat(trimmedNumberString2); | |
const isValid = finalResult === finalEvalResult; | |
if (ONLY_SHOW_INVALID) { | |
if (!isValid) console.log(str, '=', finalResult, 'Invalid: ' + finalEvalResult) | |
} else { | |
console.log(str, '=', finalResult, "Is Valid?", isValid, isValid ? 'Valid' : 'Invalid: ' + finalEvalResult) | |
} | |
} catch(e) { | |
console.warn("Eval Error:", str, e.message); | |
} | |
} | |
} | |
// you can remove the test (which uses eval to verify itself) ^^ | |
return finalResult; | |
}; | |
let example; | |
example = evalMath('1 + 1'); | |
example = evalMath('4 - 1'); | |
example = evalMath('2 * 5'); | |
example = evalMath('16 / 4'); | |
example = evalMath('1 + 1 + 2'); | |
example = evalMath('4 - 1 - 2'); | |
example = evalMath('15 * 1000 * 2 / 2 * 2'); | |
example = evalMath('1 + 15 * 1000 * 2 / 2 * 2'); | |
example = evalMath('15 * 1000 * 2 / 2 * 2 + 1'); | |
example = evalMath('16 / 4 / 2'); | |
example = evalMath('5 * 5'); | |
example = evalMath('10 / 2'); | |
example = evalMath('3 + 5 * 2'); | |
example = evalMath('3 * 5 + 2'); | |
example = evalMath('10 - 3 * 2 + 5'); | |
example = evalMath('10 - 3 * 2'); | |
example = evalMath('3 * 2 + 5'); | |
example = evalMath('2 * 3 + 5 * 4'); | |
// example = evalMath('2 * (3 + 5)'); // Error | |
// example = evalMath('a + a'); // Error | |
// example = evalMath('b'); // Error | |
example = evalMath('0'); | |
example = evalMath('20'); | |
example = evalMath('-10 + 10'); | |
example = evalMath('-10 + 12'); | |
example = evalMath('10 - 15'); | |
// example = evalMath('1-'); // Error | |
example = evalMath('-1'); | |
example = evalMath('10 / 3'); | |
example = evalMath('1+1+1*2+1+1+1'); | |
example = evalMath('2.5 * 5'); | |
example = evalMath('1.5 + 1.5'); | |
example = evalMath('4.5 - 1.2'); | |
example = evalMath('2.3 * 2.5'); | |
example = evalMath('16.8 / 4.2'); | |
example = evalMath('1.1 + 1.2 + 2.3'); | |
example = evalMath('4.5 - 1.2 - 2.1'); | |
example = evalMath('15.25 * 100.5 * 2.2 / 2.5 * 2.5'); | |
example = evalMath('1.1 + 15.5 * 100.25 * 2.2 / 2.1 * 2.1'); | |
example = evalMath('15.5 * 1000.2 * 2.1 / 2.5 * 2.5 + 1.5'); | |
example = evalMath('16.5 / 4.5 / 2.5'); | |
example = evalMath('5.5 * 5.5'); | |
example = evalMath('10.5 / 2.5'); | |
example = evalMath('3.5 + 5.5 * 2.5'); | |
example = evalMath('3.2 * 5.5 + 2.5'); | |
example = evalMath('10.5 - 3.2 * 2.5 + 5.5'); | |
example = evalMath('10.5 - 3.2 * 2.5'); | |
example = evalMath('3.5 * 2.5 + 5.5'); | |
example = evalMath('2.1 * 3.5 + 5.5 * 4.5'); | |
example = evalMath('2.5 * 5.2'); | |
example = evalMath('0.1'); | |
example = evalMath('20.2'); | |
example = evalMath('-10.5 + 10.5'); | |
example = evalMath('-10.5 + 12.2'); | |
example = evalMath('10.5 - 15.5'); | |
// example = evalMath('1.1-'); // Error | |
example = evalMath('-1.1'); | |
example = evalMath('10.5 / 3.5'); | |
example = evalMath('1.2+1.3+1.5*2.5+1.7+1.8+1.9'); | |
"Finished" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To solve the python one just use:
![image](https://private-user-images.githubusercontent.com/41600149/323446635-e3acd06c-746b-4418-af6b-0e3806d67351.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjEzNzQ2NTQsIm5iZiI6MTcyMTM3NDM1NCwicGF0aCI6Ii80MTYwMDE0OS8zMjM0NDY2MzUtZTNhY2QwNmMtNzQ2Yi00NDE4LWFmNmItMGUzODA2ZDY3MzUxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzE5VDA3MzIzNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTc5MDgyODQ2MWYwOTQxODk2OTY2MzU2ZmEyMjNhYWMyMjYyMzFhMzI3YTc1ODRhYmI5YzlmNmU4Y2M0ODAyNDcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.Xz_9D3PGMpqUeMENPiAnhJq8Y7lCw7h9xZZj7GAa9Ss)
format(3.3-2.1, '.1f')
or