Topic

Number literals

The parsing functions read one number literal each (no operators), in every exact form RatMath uses: repeating decimals, mixed numbers, continued fractions, intervals, uncertainty brackets and certified prefixes.

Entry points

functionacceptsreturns
parseNumberany literalInteger, Rational, RationalInterval or CertifiedApproximation
parseRationalany scalar formalways a Rational; rejects intervals
parseDecimal, parseRepeatingDecimalfinite or repeating decimalsRational
parseMixedNumberwhole..num/denRational
parseContinuedFractiona0.~a1~a2 or an array of termsRational (CertifiedApproximation with ?)
parseIntervalany interval form, or a scalarRationalInterval (a scalar becomes a point)
parseCertifiedApproximationa decimal or continued fraction with ?CertifiedApproximation

Each trims surrounding whitespace. Underscores may separate digits (1_000). Everything is base 10; for other bases use BaseSystem or R with a prefix such as 0x.

Repeating decimals

# starts the block that repeats forever. Reading one is the geometric series in reverse: if the pre-period AA has mm digits and the block BB has kk digits,

0.AB‾=A10m+B10m (10k−1),e.g.0.16‾=110+610⋅9=16. 0.A\overline{B} = \frac{A}{10^m} + \frac{B}{10^m\,(10^k - 1)}, \qquad\text{e.g.}\quad 0.1\overline{6} = \frac{1}{10} + \frac{6}{10 \cdot 9} = \frac16.
literalvalue
0.#313\tfrac13
0.1#616\tfrac16
12.34#5612.3456‾12.34\overline{56}
1.25#054\tfrac54 (a repeating 0 ends the decimal)
0.{0~8}110−910^{-9}: {digits~count} repeats a run of digits

Mixed numbers and continued fractions

import { parseMixedNumber, parseContinuedFraction } from "@ratmath/core";

parseMixedNumber("2..1/4").toString();         // "9/4"
parseMixedNumber("-2..1/4").toString();        // "-9/4": the sign covers the whole value
parseContinuedFraction("3.~7~15").toString();  // "333/106"
parseContinuedFraction([3n, 7n, 15n]).toString();   // "333/106"
parseContinuedFraction("~-3.~1~3").toString(); // "-9/4": ~ before a negative first term
parseContinuedFraction("-3.~1~3").toString();  // "-15/4": a plain - negates the whole value

The sign rule for continued fractions is explained on the continued fractions page.

Intervals and uncertainty

A colon joins two scalar ends. Brackets after a decimal give shorter forms:

formexampleinterval
more digits for each end1.23[56:67][1.2356,1.2367][1.2356, 1.2367]
signed offsets1.23[+5:-6][1.17,1.28][1.17, 1.28]
symmetric offset1.3[+-1][1.2,1.4][1.2, 1.4]
fractional offset1.2[+-0.1][1.19,1.21][1.19, 1.21]
repeating ends0.[#3:#6][13,23][\tfrac13, \tfrac23]

An offset is counted in units of the last written digit: with dd decimal places, an offset xx means x⋅10−dx \cdot 10^{-d}. So in 1.23[+5:-6] the unit is 0.010.01, and in 1.2[+-0.1] it is 0.1×0.1=0.010.1 \times 0.1 = 0.01.

import { parseInterval } from "@ratmath/core";

parseInterval("1/3:2/3").toString();    // "1/3:2/3"
parseInterval("1.3[+-1]").toString();   // "6/5:7/5"
parseInterval("3/4").toString();        // "3/4:3/4"

What is rejected

parseNumber("1 + 2");       // throws: an expression, not a number (use R)
parseRational("1:2");       // throws: an interval
parseDecimal("1/2");        // throws: not decimal notation
parseMixedNumber("9/4");    // throws: needs the .. form
parseInterval("1:2:3");     // throws: three ends
Try itEdit the code and run it with @ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs