Liking cljdoc? Tell your friends :D

skuld.ledger.rational

Exact rationals, for the quantities where they are safe.

Where they are safe, and where they are not

Clojure has exact rationals natively, and the Cosmos SDK carries a documented 3-ulp margin-of-error hack whose comment says outright that the problem would vanish given arbitrary-precision rationals. That makes exact arithmetic look like free correctness.

It is free only for BOUNDED, NON-ACCUMULATED quantities. Adding rationals multiplies denominators, so anything summed over time grows without bound. Measured on a cumulative reward ratio, the reduced denominator grew linearly at roughly 26 bits per period:

10 periods       294 bits

100 periods 2,748 bits 400 periods 10,363 bits

which is a multi-megabyte integer after a million blocks, stored, hashed into the app-hash and compared every block.

So: commission rates, slash fractions and tax rates -- fixed, small, never accumulated -- are exact rationals here. The cumulative reward ratio is fixed-point in skuld.ledger.stake, and that is a deliberate choice rather than an inherited limitation.

They cannot be hashed directly

hasch/edn-hash has no PHashCoercion for clojure.lang.Ratio, so any value containing one raises rather than hashing. That does not affect persistence -- 1/10 prints and reads back exactly -- nor the app-hash, which covers stored datoms and sees the printed string. It does mean a Ratio must never reach a digest directly, which is a real constraint on where these values may travel, discovered by a three-JVM determinism run that could not hash its own state.

Exact rationals, for the quantities where they are safe.

## Where they are safe, and where they are not

Clojure has exact rationals natively, and the Cosmos SDK carries a
documented 3-ulp margin-of-error hack whose comment says outright that the
problem would vanish given arbitrary-precision rationals. That makes exact
arithmetic look like free correctness.

It is free only for BOUNDED, NON-ACCUMULATED quantities. Adding rationals
multiplies denominators, so anything summed over time grows without bound.
Measured on a cumulative reward ratio, the reduced denominator grew linearly
at roughly 26 bits per period:

    10 periods       294 bits
   100 periods     2,748 bits
   400 periods    10,363 bits

which is a multi-megabyte integer after a million blocks, stored, hashed
into the app-hash and compared every block.

So: commission rates, slash fractions and tax rates -- fixed, small, never
accumulated -- are exact rationals here. The cumulative reward ratio is
fixed-point in `skuld.ledger.stake`, and that is a deliberate choice rather
than an inherited limitation.

## They cannot be hashed directly

`hasch/edn-hash` has no `PHashCoercion` for `clojure.lang.Ratio`, so any
value containing one raises rather than hashing. That does not affect
persistence -- `1/10` prints and reads back exactly -- nor the app-hash,
which covers stored datoms and sees the printed string. It does mean a
Ratio must never reach a digest directly, which is a real constraint on
where these values may travel, discovered by a three-JVM determinism run
that could not hash its own state.
raw docstring

addclj

(add a b)

divclj

(div a b)

floorclj

(floor r)

Truncate DOWN to a long.

Always down, everywhere, because these values are paid out: a fraction too much is a solvency hole, a fraction too little is dust the caller routes to the community pool.

Truncate DOWN to a long.

Always down, everywhere, because these values are paid out: a fraction too
much is a solvency hole, a fraction too little is dust the caller routes to
the community pool.
raw docstring

in-unit-interval?clj

(in-unit-interval? r)

Is r in [0,1]? Commission and slash fractions must be.

Is `r` in [0,1]? Commission and slash fractions must be.
raw docstring

mul-intclj

(mul-int r n)

r * n, exactly. The result is still rational.

`r * n`, exactly. The result is still rational.
raw docstring

ofclj

(of n d)

An exact rational n/d.

An exact rational `n/d`.
raw docstring

subclj

(sub a b)

zero?clj

(zero? r)

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close