xtensor-stack / xtensor

C++ tensors with broadcasting and lazy computing
BSD 3-Clause "New" or "Revised" License
3.36k stars 398 forks source link

Divergence in default rounding of `xt::round()` from `np.round()` #2800

Open faze-geek opened 3 months ago

faze-geek commented 3 months ago

Consider following bankers rounding for xt::round() instead of rounding away from zero xtensor -

xt::xarray<double> arr1 =  {0.5, 1.5, 2.5, 3.5, 4.5};
std::cout<<xt::round(arr1)<<std::endl;

{ 1.,  2.,  3.,  4.,  5.}

numpy -

>>> import numpy as np
>>> arr1 = np.array([0.5, 1.5, 2.5, 3.5, 4.5])
>>> np.round(arr1)
array([0., 2., 2., 4., 4.])

What numpy follows when rounding exact halves is round half to nearest even. You can read why it is the default rounding model followed by IEEE_754 becuase of eliminating biases here.

faze-geek commented 3 months ago

@JohanMabille If this is a valid issue, I'd like to send in a fix. Thanks.

faze-geek commented 3 months ago

If we want to stick with the original, this must be mentioned somewhere like noteable differences.