1 Şubat 2019 Cuma

std::remainder

Giriş
Açıklaması şöyle.
The IEEE floating-point remainder of the division operation x/y calculated by this function is exactly the value x - n*y, where the value n is the integral value nearest the exact value x/y. When |n-x/y| = 1/2 the value n is chosen to be even.
std::fmod ile Farkı 
Açıklaması şöyle
std::remainder() is like std::fmod() except that it rounds the internal quotient n to the nearest integer instead of towards zero to an integer.
Bu yüzden sonucun artı/eksi değeri ilk parametre olan x'ten farklı olabiliyor. Açıklaması şöyle.
In contrast to std::fmod(), the returned value is not guaranteed to have the same sign as x.
Örnek
Elimizde şöyle bir kod olsun.
std::remainder(+5.1, +3.0) = -0.9
std::remainder(-5.1, +3.0) = 0.9
Açıklaması şöyle.
So to take the example in the question, when x = +5.1 and y = +3.0 The nearest integral value to x/y (1.7) is 2. So n is 2. So the remainder will yield x - 2y which is 5.1 - 2 * 3.0 which is 5.1 - 6.0 which is -0.9.

But when x = -5.1 and y = +3.0 The nearest integral value to x/y (-1.7) is -2. So n is -2. So the remainder will yield x - 2y which is -5.1 - (-2) * 3.0 which is -5.1 + 6.0 which is +0.9

Hiç yorum yok:

Yorum Gönder