hyperreal
hyperreal provides exact rational arithmetic, symbolic real values, and lazy
computable real approximation.
It is useful when code needs more information than an f64 can provide:
structural sign facts, exact zero/nonzero knowledge, exact rational access,
bounded sign refinement, and recognizable forms such as pi, e, square
roots, logarithms, and rational trig constants.
Main Types
Rational: arbitrary-precision exact rationals, including exact IEEE-754f32/f64import.Computable: lazy real expressions approximated at a requested binary precision.Real: a rational scale plus a symbolic/computable class. It preserves exact structure when doing so helps arithmetic, predicates, or approximation.RealStructuralFacts: conservative public facts about sign, zero status, magnitude, and exact-rational state.Simple: a small Lisp-like expression parser, enabled by the defaultsimplefeature.
Relationship to Other Crates
realistic_blasuseshyperreal::Realas its default exact/symbolic scalar backend and forwardshyperrealstructural facts through itsScalartype.predicatedcan consumehyperreal::Realdirectly, using structural facts, finitef64approximations, and bounded sign refinement before robust fallback.
hyperreal owns scalar representation and approximation. It does not own vector
or matrix algebra, and it does not decide geometry topology.
Current State
The crate is benchmark-driven and no longer just a direct port of computable real ideas. Current implementation work includes:
- exact rational and dyadic fast paths
- cached internal constants for
pi,tau,e, common square roots, and common logarithms - symbolic classes for selected
pi,e,sqrt,ln,sin(pi*q), andtan(pi*q)forms - exact trig, inverse-trig, logarithm, exponential, and inverse-hyperbolic shortcuts where the input structure is recognizable
- argument reduction and prescaled kernels for transcendental approximation
- structural sign, zero, nonzero, magnitude, and exact-rational queries
- bounded sign refinement through
sign_untilandrefine_sign_until - borrowed arithmetic paths for
RationalandReal serdesupport for expression structure, excluding transient caches and abort signals
This is a scalar library for exact/symbolic experimentation, predicate filters, and small algebraic workloads. It is not a dense numeric BLAS replacement.
Installation
[]
= "0.10.5"
Without the Simple parser and calculator binary:
[]
= { = "0.10.5", = false }
Feature flags:
| Feature | Default | Purpose |
|---|---|---|
simple |
yes | Enables Simple and the package calculator binary. |
Examples
Exact Rationals
use Rational;
let a = fraction.unwrap;
let b = fraction.unwrap;
assert_eq!;
Symbolic Reals
use ;
let x = new.sqrt.unwrap;
let y = new.sqrt.unwrap;
let z = x * y;
let approx: f64 = z.into;
assert!;
Computable Approximation
use ;
let x = rational.sin;
let scaled = x.approx;
assert_ne!;
Structural Facts
use ;
let value = new.sqrt.unwrap;
let facts = value.structural_facts;
assert_eq!;
assert_eq!;
assert!;
assert_eq!;
Facts are conservative. Missing sign or magnitude information means the fact was not proven cheaply.
Simple Expressions
use Simple;
let expr: Simple = "(* (+ pi pi) (sin (/ 1 5)))".parse.unwrap;
let value = expr.evaluate.unwrap;
let _: f64 = value.into;
Simple supports arithmetic, roots, powers, logs, exponentials, trig, inverse
trig, inverse hyperbolic functions, integers, decimals, fractions, pi, and
e.
Conversions
Supported conversions include:
- integer types to
RationalandReal - finite
f32/f64toRationalandRealby exact IEEE-754 decoding Realtof32/f64by approximationReal::to_f64_approx()for borrowed finite approximation used by filters
Float import rejects NaN and infinities. to_f64_approx() returns None
when no finite f64 approximation can be produced.
Performance Notes
Performance shortcuts are intentionally documented next to the code that uses them. The main techniques are:
- keep exact rational and dyadic values outside generic computable graphs
- clone cached internal constants instead of rebuilding them
- preserve lightweight symbolic classes only where benchmarks show value
- reduce trig and exponential arguments before entering series kernels
- use endpoint and tiny-argument transforms for inverse trig and inverse hyperbolic functions
- answer structural queries from certificates before refining approximations
- use borrowed arithmetic to reduce expression-graph cloning in callers such as
realistic_blasandpredicated
Benchmark suites:
The generated benchmark summary is in benchmarks.md.
Development
Common checks:
When adding a shortcut, add a focused correctness test and a benchmark row for
the smallest affected surface. Keep the shortcut only if it improves the target
without regressing broader realistic_blas or predicated benchmarks.
License
Apache-2.0.