Struct rust_examples::rsqrt::PositiveFloat[][src]

pub struct PositiveFloat(_);
Expand description

Thin wrapper around f32 with additional semantics that the values can only be positive floats and excluding infinity and nan.

Zero-cost abstraction

use std::mem::size_of;
use rust_examples::rsqrt::PositiveFloat;

assert_eq!(size_of::<PositiveFloat>(), size_of::<f32>());

Automatic derivation or arithmetic operators

Notice that some of impls on PositiveFloat are automatically derived using derive_more.

Implementations

Constructs new PositiveFloat from given f32 only if:

  • it is sign positive
  • is a normal float value (i.e. not a zero, nan or infinity)

Note that this factory ensures the safety of PositiveFloat::fast_rsqrt as it makes the illegal states mentioned above unrepresentable.

Example
use rust_examples::rsqrt::PositiveFloat;

assert_eq!(PositiveFloat::new(-4.2), None);
assert_eq!(PositiveFloat::new(0.0), None);
assert_eq!(PositiveFloat::new(f32::NAN), None);
assert_eq!(PositiveFloat::new(f32::INFINITY), None);

Retrieves inner f32 value

Calculates the inverse square root of given number

Approximates the inverse square root of given number.

This implementation is safe in the sense that by the construction of PositiveFloat, it is not possible to call PositiveFloat::fast_rsqrt on any invalid value: negative floats, zero, nan or infinity.

Constant generic parameter ITERS determines the number of iterations of the Newton’s method used to find the approximation. Note that despite the fact that it is usize, the implementation executes at least one iteration even if it is set to 0.

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.