Skip to main content

RealField

Trait RealField 

Source
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§

Source

fn nan() -> Self

Returns the NaN value.

Source

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());
Source

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());
Source

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());
Source

fn clamp(self, min: Self, max: Self) -> Self

Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

fn sinh(self) -> Self

Computes the hyperbolic sine of a number.

Source

fn cosh(self) -> Self

Computes the hyperbolic cosine of a number.

Source

fn tanh(self) -> Self

Computes the hyperbolic tangent of a number.

§Example
use deep_causality_num::RealField;
assert!(0.0f64.tanh() == 0.0);
Source

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);
Source

fn atan2(self, other: Self) -> Self

Computes the four-quadrant arctangent of self (y) and other (x).

§Example
use deep_causality_num::RealField;
use std::f64::consts;
let y = 1.0f64;
let x = -1.0f64;
assert!((y.atan2(x) - (3.0 * consts::PI / 4.0)).abs() < 1e-9);
Source

fn pi() -> Self

Returns the constant π.

Source

fn e() -> Self

Returns the Euler number e, the base of the natural logarithm.

Source

fn epsilon() -> Self

Machine epsilon. Used for comparison comparisons to zero.

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.

Implementations on Foreign Types§

Source§

impl RealField for f32

Source§

fn nan() -> Self

Source§

fn is_nan(self) -> bool

Source§

fn is_infinite(self) -> bool

Source§

fn is_finite(self) -> bool

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn sqrt(self) -> Self

Source§

fn abs(self) -> Self

Source§

fn floor(self) -> Self

Source§

fn ceil(self) -> Self

Source§

fn round(self) -> Self

Source§

fn exp(self) -> Self

Source§

fn ln(self) -> Self

Source§

fn log(self, base: Self) -> Self

Source§

fn powf(self, n: Self) -> Self

Source§

fn sin(self) -> Self

Source§

fn asin(self) -> Self

Source§

fn cos(self) -> Self

Source§

fn acos(self) -> Self

Source§

fn tan(self) -> Self

Source§

fn sinh(self) -> Self

Source§

fn cosh(self) -> Self

Source§

fn tanh(self) -> Self

Source§

fn atan(self) -> Self

Source§

fn atan2(self, other: Self) -> Self

Source§

fn pi() -> Self

Source§

fn e() -> Self

Source§

fn epsilon() -> Self

Source§

impl RealField for f64

Source§

fn nan() -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn abs(self) -> Self

Source§

fn sqrt(self) -> Self

Source§

fn floor(self) -> Self

Source§

fn ceil(self) -> Self

Source§

fn round(self) -> Self

Source§

fn exp(self) -> Self

Source§

fn ln(self) -> Self

Source§

fn log(self, base: Self) -> Self

Source§

fn powf(self, n: Self) -> Self

Source§

fn sin(self) -> Self

Source§

fn cos(self) -> Self

Source§

fn acos(self) -> Self

Source§

fn tan(self) -> Self

Source§

fn sinh(self) -> Self

Source§

fn cosh(self) -> Self

Source§

fn tanh(self) -> Self

Source§

fn atan2(self, other: Self) -> Self

Source§

fn pi() -> Self

Source§

fn e() -> Self

Source§

fn epsilon() -> Self

Source§

fn atan(self) -> Self

Source§

fn asin(self) -> Self

Source§

fn is_nan(self) -> bool

Source§

fn is_infinite(self) -> bool

Source§

fn is_finite(self) -> bool

Implementors§