round_simd

Function round_simd 

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

Compute the rounding to nearest integer of each element (SIMD-accelerated).

Rounds each element to the nearest integer, with ties (x.5) rounding away from zero (standard rounding).

§Arguments

  • x - Input 1D array

§Returns

Array1<F> with the same length as input, where each element is rounded to the nearest integer.

§Performance

  • SIMD: Automatically used for large arrays (1000+ elements)
  • Scalar: Used for small arrays or when SIMD is unavailable

§Rounding Behavior

This function uses “round half away from zero” (standard rounding):

  • 0.5 rounds to 1.0
  • 1.5 rounds to 2.0
  • 2.5 rounds to 3.0
  • -0.5 rounds to -1.0
  • -1.5 rounds to -2.0

§Mathematical Properties

  • |round(x) - x| <= 0.5 for all x
  • round(x) = x if x is already an integer
  • round(-x) = -round(x)

§Examples

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

let x = array![1.2, 2.7, -1.3, -2.9, 3.0, 2.5];
let result = round_simd(&x.view());
// Result: [1.0, 3.0, -1.0, -3.0, 3.0, 3.0]
//          note: 2.5 rounds to 3.0 (away from zero)

§Applications

  • Data Visualization: Rounding values for display
  • Statistics: Rounding statistical summaries
  • Financial: General-purpose monetary rounding
  • Machine Learning: Quantization for model compression
  • Image Processing: Pixel value normalization
  • Scientific Computing: Reducing numerical noise

§See Also