This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/cas/rational

Exact rationals and arithmetic mod p, in doubles

The coefficients of the computer algebra system (lib/cas) are exact rationals p/q: q > 0, gcd(p, q) = 1. They are doubles holding integers: this library predates Viv's Int, and Viv has no big integers yet (features 1 and 5). A double holds every integer up to exactly; every operation here checks that its result stays below that, and gives NaN when it would not. The store (lib/cas/store) turns a NaN coefficient into a refusal that says why, so a coefficient is never silently rounded. A later stage replaces these pairs with lib/bigint and lib/rational, and then nothing here refuses.

A rational is two numbers, so every operation returns two: p, q = rat_add(a, b, c, d).

Arithmetic mod (a prime) is exact in doubles: a product of two residues is below . The modular certificates of lib/cas/certify evaluate expressions there, and mp_rand draws their points from a seeded Park-Miller generator, so all three engines draw the same points.

Used with import cas.rational.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 25–355