MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / machineValueOfString

Function machineValueOfString

src/math-json/utils.ts:264–283  ·  view source on GitHub ↗
(s: string)

Source from the content-addressed store, hash-verified

262}
263
264function machineValueOfString(s: string): number {
265 s = s
266 .toLowerCase()
267 .replace(/[nd]$/, '')
268 .replace(/[\u0009-\u000d\u0020\u00a0]/g, '');
269
270 if (s === 'nan') return NaN;
271 if (/^(infinity|\+infinity|oo|\+oo)$/i.test(s)) return Infinity;
272 if (/^(-infinity|-oo)$/.test(s)) return -Infinity;
273
274 // Are there some repeating decimals?
275 if (/\([0-9]+\)/.test(s)) {
276 const [_, body, repeat, trail] = s.match(/(.+)\(([0-9]+)\)(.*)$/) ?? [];
277 s = body + repeat.repeat(Math.ceil(16 / repeat.length)) + (trail ?? '');
278 }
279 // CAUTION: By using parseFloat, numbers with a precision greater than
280 // machine range will be truncated. Numbers with an exponent outside of the
281 // machine range will be returned as `Infinity` or `-Infinity`
282 return parseFloat(s);
283}
284
285/**
286 * CAUTION: `machineValue()` will return a truncated value if the number

Callers 1

machineValueFunction · 0.85

Calls 3

replaceMethod · 0.65
matchMethod · 0.65
ceilMethod · 0.45

Tested by

no test coverage detected