to_precision

Function to_precision 

Source
pub fn to_precision<F, T>(value: T) -> F
where F: RealFloat + NumCast, T: NumCast,
Expand description

Casts a numeric value into the target floating-point type F.

This function provides a transparent conversion mechanism for numeric values (T) into a chosen target type (F), typically f32 or f64.

Internally it uses num_traits::NumCast::from and will panic if the cast is not representable by the target type (e.g. out-of-range values, or non-finite floats when converting to an integer type).

The main purpose is to abstract over floating-point precision in generic code where the target type F: RealFloat may vary. This enables you to write a single numeric implementation that automatically adapts to either f32 or f64 precision without explicit as conversions.

§Arguments

  • value - The numeric value to convert to the target floating-point type

§Returns

The input value converted to the target floating-point type F.

§Behaviour

  • Uses NumCast::from(value).
  • Panics if the conversion fails.

In practice, if F and T are the same type (e.g. f32 → f32), this operation is a compile-time no-op with no runtime overhead.

§Examples

use audio_samples::to_precision;

let value_i32 = 42i32;
let value_f32: f32 = to_precision(value_i32);
assert_eq!(value_f32, 42.0);

let value_f64: f64 = to_precision(value_i32);
assert_eq!(value_f64, 42.0);

§Panics

Panics if the numeric conversion fails.