@mathigon/fermat
Advanced tools
Comparing version 1.1.9 to 1.1.10
@@ -14,2 +14,3 @@ /** Checks if two numbers are nearly equals. */ | ||
export declare function numberFormat(n: number, places?: number, separators?: boolean): string; | ||
export declare function scientificFormat(value: number, places?: number): string; | ||
/** | ||
@@ -16,0 +17,0 @@ * Converts a number to a string, including . or , decimal points and |
@@ -62,2 +62,3 @@ "use strict"; | ||
roundTo: () => roundTo, | ||
scientificFormat: () => scientificFormat, | ||
sign: () => sign, | ||
@@ -117,2 +118,16 @@ square: () => square, | ||
} | ||
function scientificFormat(value, places = 6) { | ||
const abs = Math.abs(value); | ||
if (isBetween(abs, Math.pow(10, -places), Math.pow(10, places))) { | ||
return numberFormat(value, places); | ||
} | ||
if (abs > Number.MAX_VALUE) | ||
return `${Math.sign(value) < 0 ? "\u2013" : ""}\u221E`; | ||
if (abs < Number.MIN_VALUE) | ||
return "0"; | ||
const [str, exponent] = value.toExponential().split("e"); | ||
const top = exponent.replace("+", "").replace("-", "\u2013"); | ||
const isNegative = top.startsWith("\u2013"); | ||
return `${str.slice(0, 5)} \xD7 10^${(isNegative ? "(" : "") + top + (isNegative ? ")" : "")}`; | ||
} | ||
var SPECIAL_DECIMAL = /^-?0,[0-9]+$/; | ||
@@ -119,0 +134,0 @@ var POINT_DECIMAL = /^-?([0-9]+(,[0-9]{3})*)?\.?[0-9]*$/; |
@@ -50,2 +50,16 @@ var __defProp = Object.defineProperty; | ||
} | ||
function scientificFormat(value, places = 6) { | ||
const abs = Math.abs(value); | ||
if (isBetween(abs, Math.pow(10, -places), Math.pow(10, places))) { | ||
return numberFormat(value, places); | ||
} | ||
if (abs > Number.MAX_VALUE) | ||
return `${Math.sign(value) < 0 ? "\u2013" : ""}\u221E`; | ||
if (abs < Number.MIN_VALUE) | ||
return "0"; | ||
const [str, exponent] = value.toExponential().split("e"); | ||
const top = exponent.replace("+", "").replace("-", "\u2013"); | ||
const isNegative = top.startsWith("\u2013"); | ||
return `${str.slice(0, 5)} \xD7 10^${(isNegative ? "(" : "") + top + (isNegative ? ")" : "")}`; | ||
} | ||
var SPECIAL_DECIMAL = /^-?0,[0-9]+$/; | ||
@@ -1233,2 +1247,3 @@ var POINT_DECIMAL = /^-?([0-9]+(,[0-9]{3})*)?\.?[0-9]*$/; | ||
roundTo, | ||
scientificFormat, | ||
sign, | ||
@@ -1235,0 +1250,0 @@ square, |
{ | ||
"name": "@mathigon/fermat", | ||
"version": "1.1.9", | ||
"version": "1.1.10", | ||
"license": "MIT", | ||
@@ -19,3 +19,3 @@ "homepage": "https://mathigon.io/fermat", | ||
"engines": { | ||
"node": ">=14" | ||
"node": ">=16" | ||
}, | ||
@@ -39,16 +39,16 @@ "main": "dist/index.cjs.js", | ||
"dependencies": { | ||
"@mathigon/core": "1.1.10" | ||
"@mathigon/core": "1.1.11" | ||
}, | ||
"devDependencies": { | ||
"@types/tape": "5.6.0", | ||
"@typescript-eslint/eslint-plugin": "5.61.0", | ||
"@typescript-eslint/parser": "5.61.0", | ||
"esbuild": "0.18.11", | ||
"eslint": "8.44.0", | ||
"eslint-plugin-import": "2.27.5", | ||
"tape": "5.6.4", | ||
"@typescript-eslint/eslint-plugin": "6.4.0", | ||
"@typescript-eslint/parser": "6.4.0", | ||
"esbuild": "0.18.17", | ||
"eslint": "8.47.0", | ||
"eslint-plugin-import": "2.28.0", | ||
"tape": "5.6.6", | ||
"ts-node": "10.9.1", | ||
"tslib": "2.6.0", | ||
"tslib": "2.6.2", | ||
"typescript": "5.1.6" | ||
} | ||
} |
@@ -75,2 +75,18 @@ // ============================================================================ | ||
export function scientificFormat(value: number, places = 6) { | ||
const abs = Math.abs(value); | ||
if (isBetween(abs, Math.pow(10, -places), Math.pow(10, places))) { | ||
return numberFormat(value, places); | ||
} | ||
// TODO Decide how we want to handle these special cases | ||
if (abs > Number.MAX_VALUE) return `${Math.sign(value) < 0 ? '–' : ''}∞`; | ||
if (abs < Number.MIN_VALUE) return '0'; | ||
const [str, exponent] = value.toExponential().split('e'); | ||
const top = exponent.replace('+', '').replace('-', '–'); | ||
const isNegative = top.startsWith('–'); | ||
return `${str.slice(0, 5)} × 10^${(isNegative ? '(' : '') + top + (isNegative ? ')' : '')}`; | ||
} | ||
// Numbers like 0,123 are decimals, even though they match POINT_DECIMAL. | ||
@@ -77,0 +93,0 @@ const SPECIAL_DECIMAL = /^-?0,[0-9]+$/; |
@@ -8,3 +8,3 @@ // ============================================================================= | ||
import tape from 'tape'; | ||
import {numberFormat, parseNumber, toWord} from '../src'; | ||
import {numberFormat, parseNumber, scientificFormat, toWord} from '../src'; | ||
@@ -47,2 +47,14 @@ | ||
test.equal(numberFormat(-0.001, 4), '0', ':: numberFormat(-0.001, 4)'); | ||
test.equal(scientificFormat(123123123, 6), '1.231 × 10^8'); | ||
test.equal(scientificFormat(123123, 6), '123,123'); | ||
test.equal(scientificFormat(-123123123, 6), '-1.23 × 10^8'); | ||
test.equal(scientificFormat(0.000123, 6), '0.00012'); | ||
test.equal(scientificFormat(0.000000123, 6), '1.23 × 10^(–7)'); | ||
test.equal(scientificFormat(-0.000000123, 6), '-1.23 × 10^(–7)'); | ||
test.equal(scientificFormat(-Number.MAX_VALUE * 10, 6), '–∞'); | ||
test.equal(scientificFormat(-Number.MIN_VALUE / 10, 6), '0'); | ||
test.end(); | ||
@@ -49,0 +61,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
306076
4422
+ Added@mathigon/core@1.1.11(transitive)
- Removed@mathigon/core@1.1.10(transitive)
Updated@mathigon/core@1.1.11