[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Octave-bug-tracker] [bug #31579] Floating point mod function does not m
From: |
Rik |
Subject: |
[Octave-bug-tracker] [bug #31579] Floating point mod function does not match Matlab |
Date: |
Tue, 09 Nov 2010 18:20:41 +0000 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.12) Gecko/20101027 Ubuntu/9.10 (karmic) Firefox/3.6.12 |
Follow-up Comment #4, bug #31579 (project octave):
Do we bother to inform the glibc folks that something is odd with their
implementation of fmod?
I used some extended features of C99 to set the rounding mode used by
ordinary math operations and it appears that the fmod function is using floor
in place of trunc, even though the documentation for fmod says that trunc
should be used.
Compile with extra option '-std=c++0x'
#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;
int
main (void)
{
double x = 2.1;
double y = 0.1;
int rnd_mode = fegetround();
cout << "Current round mode is " << rnd_mode << endl;
fesetround(FE_DOWNWARD);
rnd_mode = fegetround();
cout << "Current round mode is " << rnd_mode << endl;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "floor (x / y) = " << floor (x / y) << endl;
cout << "x - y * floor (x / y) = " << x - y * floor (x / y) << endl;
cout << "fmod (x, y) = " << fmod (x, y) << endl;
cout << "(int) (x / y) = " << static_cast<int> (x / y) << endl;
cout << "x - y * (int) (x / y) = " << x - y * static_cast<int> (x / y) <<
endl;
cout << "fmodf (float (x), float (y)) = " << fmodf (float (x), float (y))
<< endl;
cout << "remainder (x, y) = " << remainder (-x, y) << endl;
return 0;
}
The results are
Current round mode is 0
Current round mode is 1024
x = 2.1
y = 0.1
floor (x / y) = 20
x - y * floor (x / y) = 0.1
fmod (x, y) = 0.1
(int) (x / y) = 20
x - y * (int) (x / y) = 0.1
fmodf (float (x), float (y)) = 2.98023e-08
remainder (x, y) = 2.77556e-17
_______________________________________________________
Reply to this item at:
<http://savannah.gnu.org/bugs/?31579>
_______________________________________________
Message sent via/by Savannah
http://savannah.gnu.org/