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
| function | accepts | returns |
|---|---|---|
| parseNumber | any literal | Integer, Rational, RationalInterval or CertifiedApproximation |
| parseRational | any scalar form | always a Rational; rejects intervals |
| parseDecimal, parseRepeatingDecimal | finite or repeating decimals | Rational |
| parseMixedNumber | whole..num/den | Rational |
| parseContinuedFraction | a0.~a1~a2 or an array of terms | Rational (CertifiedApproximation with ?) |
| parseInterval | any interval form, or a scalar | RationalInterval (a scalar becomes a point) |
| parseCertifiedApproximation | a 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 has digits and the block has digits,
| literal | value |
|---|---|
| 0.#3 | |
| 0.1#6 | |
| 12.34#56 | |
| 1.25#0 | (a repeating 0 ends the decimal) |
| 0.{0~8}1 | : {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:
| form | example | interval |
|---|---|---|
| more digits for each end | 1.23[56:67] | |
| signed offsets | 1.23[+5:-6] | |
| symmetric offset | 1.3[+-1] | |
| fractional offset | 1.2[+-0.1] | |
| repeating ends | 0.[#3:#6] |
An offset is counted in units of the last written digit: with decimal places, an offset means . So in 1.23[+5:-6] the unit is , and in 1.2[+-0.1] it is .
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
@ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.