fract_simd

Function fract_simd 

Source
pub fn fract_simd<F>(x: &ArrayView1<'_, F>) -> Array1<F>
where F: Float + SimdUnifiedOps,
Expand description

Compute the fractional part of each element (SIMD-accelerated).

Returns the signed fractional component of each value (x - trunc(x)).

§Arguments

  • x - Input 1D array

§Returns

Array1<F> with the same length as input, where each element is the signed fractional part in the range (-1, 1).

§Performance

  • SIMD: Automatically used for large arrays (1000+ elements)
  • Scalar: Used for small arrays or when SIMD unavailable
  • Speedup: 2-4x for large arrays on AVX2/NEON systems

§Mathematical Definition

fract(x) = x - trunc(x)

For positive x: same as x - floor(x) For negative x: preserves sign (e.g., fract(-1.5) = -0.5)

§Properties

  • -1 < fract(x) < 1 for all finite x
  • fract(x + n) = fract(x) for any integer n (periodic with period 1)
  • fract(x) = 0 if and only if x is an integer
  • fract(x) + trunc(x) = x
  • fract(-x) = -fract(x) (odd function)

§Applications

  • Computer Graphics: Texture coordinate wrapping, repeating patterns
  • Animation: Cyclic motion, looping animations
  • Signal Processing: Modulo 1 operations, phase wrapping
  • Numerical Methods: Fractional part extraction, decimal decomposition
  • Game Development: Tile-based rendering, repeating textures
  • Scientific Computing: Periodic boundary conditions, modulo arithmetic
  • Audio Processing: Phase accumulation, waveform generation
  • Time Calculations: Extracting fractional seconds, subsecond precision
  • Cryptography: Linear congruential generators, pseudo-random sequences
  • Financial: Fractional share calculations, interest accrual

§Examples

use scirs2_core::ndarray::array;
use scirs2_core::ndarray_ext::elementwise::fract_simd;

let x = array![1.5_f64, 2.7, -1.3, 0.0, 3.0];
let result = fract_simd(&x.view());
assert!((result[0] - 0.5_f64).abs() < 1e-10);    // 1.5 - trunc(1.5) = 0.5
assert!((result[1] - 0.7_f64).abs() < 1e-10);    // 2.7 - trunc(2.7) = 0.7
assert!((result[2] - (-0.3_f64)).abs() < 1e-10); // -1.3 - trunc(-1.3) = -0.3
assert_eq!(result[3], 0.0_f64);                   // 0.0 - trunc(0.0) = 0.0
assert_eq!(result[4], 0.0_f64);                   // 3.0 - trunc(3.0) = 0.0

// Property: fract(x) + trunc(x) = x
let values = array![5.25_f64, -2.75, 0.5, 10.0];
let fract_parts = fract_simd(&values.view());
let trunc_parts = values.mapv(|v: f64| v.trunc());
let reconstructed = &fract_parts + &trunc_parts;
for i in 0..values.len() {
    assert!((reconstructed[i] - values[i]).abs() < 1e-10);
}

§See Also