Skip to content

Commit

Permalink
Improved SCientific Notation
Browse files Browse the repository at this point in the history
  • Loading branch information
Canna71 committed Dec 17, 2022
1 parent 743f327 commit b07a4f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/Math/PadScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function toLaTeX(num: number, scientific = false, precision = 21) {
// Check if the number is zero
if (num === 0) return "0";
let numDecimals = num.toString().split(".")[1]?.length || 0;
const numStr = (numDecimals > precision)
? num.toFixed(precision)
: num.toString()

if (scientific) {
// Get the absolute value of the number
Expand All @@ -23,7 +26,10 @@ function toLaTeX(num: number, scientific = false, precision = 21) {
const exponent = Math.floor(Math.log10(absNum));

// Divide the number by 10 raised to the exponent to get the coefficient
let coefficient = absNum / Math.pow(10, exponent);

let coefficient = exponent > 0
? absNum / Math.pow(10, exponent)
: absNum * Math.pow(10, -exponent) ;

numDecimals = coefficient.toString().split(".")[1]?.length || 0;

Expand All @@ -36,15 +42,13 @@ function toLaTeX(num: number, scientific = false, precision = 21) {
: coefficient.toString();

// Return the number in scientific notation
if (exponent !== 0) {
if (exponent !== 0 && numStr.length > coeffStr.length+2) {
return `${coeffStr} \\times 10^{${exponent}}`;
} else {
return `${coeffStr}`;
return numStr;
}
} else {
const numStr = (numDecimals > precision)
? num.toFixed(precision)
: num.toString()

return numStr;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/MathpadSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const DEFAULT_SETTINGS: MathpadSettings = {
plotWidth: 0,
plotDerivatives: false,
precision: 21,
scientific: true
scientific: false
}

// export default function getSettings() : IMathpadSettings{
Expand Down

0 comments on commit b07a4f9

Please sign in to comment.