/* starting with basic functions, then will handle suffixes later
-------- INTIGER LIMIT IS 9.9e308 -------- ignore this */
const OriginalNumber = '1e1000000'; // must be a string if above 9.9e308
const NumberSplit = OriginalNumber.split('e');
const DecimalAccuracy = 3 // how many decimal points you want soley for accuracy but anything past 4 dosent matter for this type of alogirthm
const Notation = null; // doing this later
function p(thing, testing) { // FOR PRINTING: makes my life easier basically and better QOL for testing
if (testing == undefined) {
return console.log(thing)
}
return console.log(thing + " " + testing);
}
// from number -> scientific in 'e' | works for string and numbers:
function ShortToScientific(Number, Notation) { // number : number input below limit, Notation : 1,2,3 ect notation type defined befor
if (isScientific(Number)) {
return Number
}
if ((Number.toString()).length < 2) {
return Number
}
var ScientificNum = (Number.toString())[0]+'.';
for (var i = 1; i < DecimalAccuracy; i++) {
if ((Number.toString())[i] != ".") {
ScientificNum += (Number.toString())[i];
}
}
return ScientificNum+"e"+((Number.toString()).length-1);
}
function RoundNumber(num, decimal) {
return Math.floor(num * Math.pow(10, decimal)) / Math.pow(10, decimal)
}
function Clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
function isScientific(num) { // cant cover numbers yet im to dumb + lazy
let format = /^[1-9]\d*e[0-9]\d*$/;
return format.test(num.toString());
}
function num(str) {
return parseInt(str)
}
/* math functions here
dont forget eval(x) function maybe try it to replace num() with eval() */
function add(value1, value2) {
var answer = "";
if (!isScientific(value1) || !isScientific(value2)) {
value1 = ShortToScientific(value1);
value2 = ShortToScientific(value2);
}
var splitNum = [(value1.split('e')), (value2.split('e'))];
if ((splitNum[0][1] == splitNum[1][1]) && (num(splitNum[0][0]) + num(splitNum[1][0])) > 9) { // find righte similar
answer = ((num(splitNum[0][0]) + num(splitNum[1][0])) / 10).toString() + 'e' + (num(splitNum[1][1])+1).toString();
} else if (Math.abs(splitNum[0][1]-splitNum[1][1]) <= 3) {
var format = ['', ''];
if (splitNum[0][1] > splitNum[1][1]) { // try using max later
format = [(num(splitNum[0][0]) + parseFloat(splitNum[1][0] / Math.pow(10, Math.abs(splitNum[0][1]-splitNum[1][1])))).toString(),
splitNum[0][1].toString()];
} else {
format = [(num(splitNum[1][0])+ parseFloat(splitNum[0][0] / Math.pow(10, Math.abs(splitNum[0][1]-splitNum[1][1])))).toString(),
splitNum[1][1].toString()];
}
answer = format[0] + "e" + format[1];
} else {
var beginning = "";
if (num(splitNum[0][1]) > num(splitNum[1][1])) {
beginning = splitNum[0][0];
} else if (num(splitNum[1][1]) > num(splitNum[0][1])) {
beginning = splitNum[1][0];
}
answer = beginning +"e"+Math.max(splitNum[0][1], splitNum[1][1]);
}
return value1 + " + " + value2 + " = " + answer;
}
/* improved version by GPT:
function add(value1, value2) {
// Check if the values are already in scientific notation, if not, convert them
if (!isScientific(value1) || !isScientific(value2)) {
value1 = ShortToScientific(value1);
value2 = ShortToScientific(value2);
}
// Split the values into their significand and exponent parts
var splitNum = [value1.split('e'), value2.split('e')];
// Calculate the sum of the significands
var sumSignificand = num(splitNum[0][0]) + num(splitNum[1][0]);
// Determine the exponent for the result
var resultExponent = Math.max(num(splitNum[0][1]), num(splitNum[1][1]));
// Check if the sum needs to be adjusted for exponent alignment
if ((splitNum[0][1] == splitNum[1][1]) && (sumSignificand > 9)) {
sumSignificand /= 10;
resultExponent++;
} else if (Math.abs(num(splitNum[0][1]) - num(splitNum[1][1])) <= 3) {
var difference = Math.abs(num(splitNum[0][1]) - num(splitNum[1][1]));
sumSignificand += parseFloat(splitNum[0][1] > splitNum[1][1] ? splitNum[1][0] / Math.pow(10, difference) : splitNum[0][0] / Math.pow(10, difference));
}
// Format the result in scientific notation
var result = sumSignificand.toString() + "e" + resultExponent.toString();
// Return the formatted result
return value1 + " + " + value2 + " = " + result;
}
*/
function mul(value1, value2) {
var answer = ""
if (!isScientific(value1) || !isScientific(value2)) {
value1 = ShortToScientific(value1);
value2 = ShortToScientific(value2);
}
var splitNum = [(value1.split('e')), (value2.split('e'))];
// can shorten this with 1 function later
if ((num(splitNum[0][0]) * (num(splitNum[1][0]))) >= 10)
answer = ((num(splitNum[0][0]) * num(splitNum[1][0])) / 10) + "e" + ((num(splitNum[0][1]) + num(splitNum[1][1])+1));
else
answer = num(splitNum[0][0]) * num(splitNum[1][0]) + "e" + (num(splitNum[0][1]) + num(splitNum[1][1]));
return value1 + " * " + value2 + " = " + answer;
}
function div(value1, value2) { // if (value2 == 0) answer = "Undefined"
var answer = ""
if (!isScientific(value1) || !isScientific(value2)) {
value1 = ShortToScientific(value1);
value2 = ShortToScientific(value2);
}
var splitNum = [(value1.split('e')), (value2.split('e'))];
// discontinued
return splitNum
}
p(add('9e10001', '2e10001'));
p(mul('1e5', '9e10'));
p(div('1e3', '1e3'));
/* input library for copy paste:
'1e3', '1e3'
'5e3', '1e5'
'8e5', '8e5'
'5e7', '7e153'
'1e563456', '5e1053252'
*/
To embed this project on your website, copy the following code and paste it into your website's HTML: