π rat-trig-rs
Rational Trigonometry - A modern approach to classical trigonometry using rational numbers instead of irrational values.
π About
Rational Trigonometry is an alternative approach developed by Norman Wildberger that replaces traditional circular definitions with line-based operations. Instead of sine, cosine, and tangent, it uses:
- Quadrance - The square of distance (replaces distance)
- Spread - The square of sine (replaces angle)
- Cross - Signed area calculations
This approach offers:
- β Exact calculations with rational numbers (no floating-point errors)
- β No irrational numbers - all operations use rational arithmetic
- β Simpler formulas - often more elegant than traditional trigonometry
- β
#![no_std]compatible - works in embedded systems
π Getting Started
Installation
Add to your Cargo.toml:
[]
= "0.1"
Quick Example
use quadrance;
use ;
π Core Concepts
Quadrance
Quadrance is the square of distance. Instead of d = β[(xβ-xβ)Β² + (yβ-yβ)Β²], we work directly with the squared value:
use quadrance;
let p1 = ;
let p2 = ;
let q = quadrance; // q = 25 (distanceΒ²)
Spread
Spread represents the square of the sine of an angle between two vectors. For exact rational arithmetic, use rational numbers:
use Ratio;
use spread;
let v1 = ;
let v2 = ;
let s = spread; // s = 1/2 (sinΒ²(45Β°))
Archimedes' Formula
Calculate 16Γ(area)Β² from three side quadrances:
use ;
let p1 = ;
let p2 = ;
let p3 = ;
let = quadrance_from_three_points;
let quadrea = archimedes;
// area = (quadrea as f64).sqrt() / 4.0 = 6.0 (when converted to float)
π§ API Overview
Tuple API
Simple and fast - works with tuples:
use *;
// 2D operations
let q = quadrance;
let s = spread;
let c = cross;
// 3D operations
let q3d = quadrance3d;
let cross3d = cross3d;
Struct API
Organized and extensible - works with structs:
use *;
let p1 = new;
let p2 = new;
let triangle = new;
let = triangle.quadrances;
let area = triangle.area;
Validation API
Check geometric properties:
use *;
let p1 = ;
let p2 = ;
let p3 = ;
assert!;
assert!;
let = spread_from_three_points;
assert!;
π¦ Features
#![no_std]- Works in embedded environments- Generic - Works with i32, i64, and rational types
- Exact arithmetic - No floating-point errors with rational types
- Const-friendly - Const-evaluable functions available for concrete types
π Examples
See the examples/ directory for more detailed examples:
π§ͺ Testing
Run the test suite:
Run with specific test names:
π Performance
The library is optimized for performance:
- All functions marked
#[inline] - LTO enabled in release builds
- Minimal dependencies (only
num-traits)
Benchmark your code:
π οΈ Development
# Build
# Build release
# Format code
# Run clippy
# Run tests
# Check documentation
π License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
π€ Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See CONTRIBUTING.md.