Expand description
§FlexFloat
A high-precision library for arbitrary-precision floating-point arithmetic with growable exponents and fixed-size fractions. FlexFloat extends IEEE 754 double-precision format to handle numbers far beyond the standard range while maintaining computational efficiency and precision consistency.
§Quick start
use flexfloat::prelude::*;
let radius = FlexFloat::from(3.0);
let circumference = radius * FlexFloat::from(core::f64::consts::TAU);
assert_ff_almost_eq!(circumference, FlexFloat::from(18.84955592153876));§Type alias
For ergonomic use, the crate root re-exports:
// Equivalent to crate::flexfloat::FlexFloat<crate::bitarray::DefaultBitArray>
use flexfloat::FlexFloat;The full generic type flexfloat::flexfloat::FlexFloat<Exp, Frac> is available for
custom-backend users.
§Special values
use flexfloat::prelude::*;
let zero = FlexFloat::zero();
let pos_inf = FlexFloat::pos_infinity();
let neg_inf = FlexFloat::neg_infinity();
let nan = FlexFloat::nan();
assert!(zero.is_zero());
assert!(pos_inf.is_infinite());
assert!(nan.is_nan());§Arithmetic
FlexFloat supports all standard arithmetic operators (+, -, *, /, %) plus
rem_euclid, div_euclid, powi, powf, and mul_add. Exponents grow automatically
when a result would overflow the current width:
use flexfloat::prelude::*;
let huge = FlexFloat::from(f64::MAX) * FlexFloat::from(f64::MAX);
assert!(!huge.is_infinite()); // exponent grew instead of overflowing
assert!(huge.exponent_bits() > 11);§Math functions
All standard transcendental functions are available through the math module and as
methods:
| Function | Method | Free fn |
|---|---|---|
| Exponential | .exp() | math::exp |
| Natural log | .ln() | math::ln |
| Square root | .sqrt() | math::sqrt |
| Trigonometry | .sin(), .cos(), .tan(), … | math::sin, … |
| Hyperbolic | .sinh(), .cosh(), .tanh(), … | math::sinh, … |
| Rounding | .round(), .floor(), .ceil(), .round_ties_even() | math::round, … |
| Simultaneous | .sin_cos() | math::sin_cos |
§Conversions
§Into FlexFloat (lossless From)
use flexfloat::FlexFloat;
use num_bigint::BigInt;
let _ = FlexFloat::from(1.5_f64);
let _ = FlexFloat::from(1.0_f32);
let _ = FlexFloat::from(42_i64);
let _ = FlexFloat::from(42_u64);
let _ = FlexFloat::from(42_i32);
let _ = FlexFloat::from(42_u32);
let _ = FlexFloat::from(BigInt::from(12345));§Out of FlexFloat (fallible TryFrom)
use flexfloat::FlexFloat;
use flexfloat::FlexFloatToF64Error;
let x = FlexFloat::from(1.5_f64);
let f: Result<f64, _> = x.try_into();
assert_eq!(f, Ok(1.5_f64));§Const-context constants
Math constants like PI, E, TAU etc. in flexfloat::consts are typed as
FlexFloat<StaticBitArray<11>, StaticBitArray<52>> — a zero-overhead const-generic
type — and can be used directly in arithmetic expressions without .convert_to():
use flexfloat::prelude::*;
let pi = FlexFloat::from(core::f64::consts::PI);
// consts::PI is FlexFloat<StaticBitArray<11>, StaticBitArray<52>> and works
// transparently as an RHS operand via the mixed-backend arithmetic impls.§Grown-aware instance methods
Because FlexFloat has no meaningful compile-time MIN/MAX/EPSILON constants
(the range depends on the current exponent width), runtime methods are provided instead:
use flexfloat::prelude::*;
let x = FlexFloat::from(1.0);
assert_eq!(x.exponent_bits(), 11); // standard IEEE 754 width
assert_eq!(x.mantissa_digits(), 53); // 52 fraction bits + 1 implicit
assert_eq!(x.epsilon(), FlexFloat::from(f64::EPSILON));§Module map
| Module | Contents |
|---|---|
bitarray | Pluggable bit-array backends (BoolBitArray, UsizeBitArray, StaticBitArray<N>) |
flexfloat | Core FlexFloat<Exp, Frac> struct and all operation impls |
flexfloat::math | Transcendental / rounding functions |
flexfloat::consts | Compile-time math constants (PI, E, TAU, …) |
flexfloat::error | FlexFloatToF64Error, FlexFloatToIntError, ConversionError |
Re-exports§
pub use bitarray::BitArray;pub use bitarray::BitArrayArith;pub use bitarray::BoolBitArray;pub use bitarray::DefaultBitArray;pub use flexfloat::error::ConversionError;pub use flexfloat::error::FlexFloatToF64Error;pub use flexfloat::error::FlexFloatToIntError;pub use flexfloat::math;
Modules§
Macros§
- assert_
ff_ almost_ eq - Asserts that two
FlexFloatvalues are approximately equal.
Type Aliases§
- Flex
Float - Ergonomic alias for
FlexFloatusing the default (BoolBitArray) backend.