pub trait RealField:
Field
+ PartialOrd
+ Neg<Output = Self>
+ Copy
+ Clone
+ AddAssign
+ SubAssign
+ MulAssign
+ DivAssign {
Show 27 methods
// Required methods
fn nan() -> Self;
fn is_nan(self) -> bool;
fn is_infinite(self) -> bool;
fn is_finite(self) -> bool;
fn clamp(self, min: Self, max: Self) -> Self;
fn sqrt(self) -> Self;
fn abs(self) -> Self;
fn floor(self) -> Self;
fn ceil(self) -> Self;
fn round(self) -> Self;
fn exp(self) -> Self;
fn ln(self) -> Self;
fn log(self, base: Self) -> Self;
fn powf(self, n: Self) -> Self;
fn sin(self) -> Self;
fn asin(self) -> Self;
fn cos(self) -> Self;
fn acos(self) -> Self;
fn tan(self) -> Self;
fn sinh(self) -> Self;
fn cosh(self) -> Self;
fn tanh(self) -> Self;
fn atan(self) -> Self;
fn atan2(self, other: Self) -> Self;
fn pi() -> Self;
fn e() -> Self;
fn epsilon() -> Self;
}Expand description
Represents an ordered Field with additional operations for calculus and analysis.
A RealField extends the algebraic definition of a Field with properties
and functions characteristic of the real numbers, such as ordering (PartialOrd)
and transcendental functions (sin, exp, ln, etc.).
This trait abstracts over concrete floating-point types like f32 and f64,
and could be implemented for other types like dual numbers (for automatic
differentiation) or custom fixed-point types.
Required Methods§
Sourcefn is_nan(self) -> bool
fn is_nan(self) -> bool
Returns true if this value is NaN and false otherwise.
use deep_causality_num::Float;
use core::f64;
let nan = f64::NAN;
let f = 7.0;
assert!(nan.is_nan());
assert!(!f.is_nan());Sourcefn is_infinite(self) -> bool
fn is_infinite(self) -> bool
Returns true if this value is positive infinity or negative infinity and
false otherwise.
use deep_causality_num::Float;
use core::f32;
let f = 7.0f32;
let inf: f32 = Float::infinity();
let neg_inf: f32 = Float::neg_infinity();
let nan: f32 = f32::NAN;
assert!(!f.is_infinite());
assert!(!nan.is_infinite());
assert!(inf.is_infinite());
assert!(neg_inf.is_infinite());Sourcefn is_finite(self) -> bool
fn is_finite(self) -> bool
Returns true if this number is neither infinite nor NaN.
use deep_causality_num::Float;
use core::f32;
let f = 7.0f32;
let inf: f32 = Float::infinity();
let neg_inf: f32 = Float::neg_infinity();
let nan: f32 = f32::NAN;
assert!(f.is_finite());
assert!(!nan.is_finite());
assert!(!inf.is_finite());
assert!(!neg_inf.is_finite());fn clamp(self, min: Self, max: Self) -> Self
Sourcefn sqrt(self) -> Self
fn sqrt(self) -> Self
Computes the principal square root of a number.
For negative numbers, it returns NaN.
§Example
use deep_causality_num::RealField;
assert_eq!(4.0f64.sqrt(), 2.0);Sourcefn abs(self) -> Self
fn abs(self) -> Self
Computes the absolute value of a number.
§Example
use deep_causality_num::RealField;
let x = -4.0f64;
assert_eq!(x.abs(), 4.0);Sourcefn floor(self) -> Self
fn floor(self) -> Self
Returns the largest integer less than or equal to a number.
§Example
use deep_causality_num::RealField;
assert_eq!(2.99f64.floor(), 2.0);Sourcefn ceil(self) -> Self
fn ceil(self) -> Self
Returns the smallest integer greater than or equal to a number.
§Example
use deep_causality_num::RealField;
assert_eq!(2.01f64.ceil(), 3.0);Sourcefn round(self) -> Self
fn round(self) -> Self
Returns the nearest integer to a number. Rounds half-way cases away from 0.0.
§Example
use deep_causality_num::RealField;
assert_eq!(2.5f64.round(), 3.0);
assert_eq!(-2.5f64.round(), -3.0);Sourcefn exp(self) -> Self
fn exp(self) -> Self
Returns e^(self), (the exponential function).
§Example
use deep_causality_num::RealField;
use std::f64::consts;
assert_eq!(1.0f64.exp(), consts::E);Sourcefn ln(self) -> Self
fn ln(self) -> Self
Returns the natural logarithm of the number.
§Example
use deep_causality_num::RealField;
use std::f64::consts;
assert_eq!(consts::E.ln(), 1.0);Sourcefn log(self, base: Self) -> Self
fn log(self, base: Self) -> Self
Returns the logarithm of the number with respect to an arbitrary base.
§Example
use deep_causality_num::RealField;
assert_eq!(8.0f64.log(2.0), 3.0);Sourcefn powf(self, n: Self) -> Self
fn powf(self, n: Self) -> Self
Raises a number to a floating point power.
§Example
use deep_causality_num::RealField;
assert_eq!(2.0f64.powf(3.0), 8.0);Sourcefn sin(self) -> Self
fn sin(self) -> Self
Computes the sine of a number (in radians).
§Example
use deep_causality_num::RealField;
use std::f64::consts;
assert!((consts::PI / 2.0).sin() - 1.0 < 1e-9);Sourcefn asin(self) -> Self
fn asin(self) -> Self
Computes the arcsine of a number. Return value is in radians in the range [-pi/2, pi/2] or NaN if the number is outside the range [-1, 1].
§Example
use deep_causality_num::RealField;
use std::f64::consts;
let val = 1.0f64;
assert!((val.asin() - consts::FRAC_PI_2).abs() < 1e-9);Sourcefn cos(self) -> Self
fn cos(self) -> Self
Computes the cosine of a number (in radians).
§Example
use deep_causality_num::RealField;
use std::f64::consts;
assert!((consts::PI.cos() - (-1.0)).abs() < 1e-9);Sourcefn acos(self) -> Self
fn acos(self) -> Self
Computes the arccosine of a number. Return value is in radians in the range [0, pi] or NaN if the number is outside the range [-1, 1].
§Example
use deep_causality_num::RealField;
use std::f64::consts;
let val = 1.0f64;
assert!((val.acos() - 0.0).abs() < 1e-9);Sourcefn tan(self) -> Self
fn tan(self) -> Self
Computes the tangent of a number (in radians).
§Example
use deep_causality_num::RealField;
use std::f64::consts;
assert!((consts::FRAC_PI_4.tan() - 1.0).abs() < 1e-9);Sourcefn tanh(self) -> Self
fn tanh(self) -> Self
Computes the hyperbolic tangent of a number.
§Example
use deep_causality_num::RealField;
assert!(0.0f64.tanh() == 0.0);Sourcefn atan(self) -> Self
fn atan(self) -> Self
Computes the arctangent of a number. Return value is in radians in the range [-pi/2, pi/2];
use deep_causality_num::RealField;
let f = 1.0;
// atan(tan(1))
let abs_difference = (f.tan().atan() - 1.0).abs();
assert!(abs_difference < 1e-10);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.