R
rembremading
Hi all!
The following piece of code has (for me) completely unexpected behaviour.
(I compile it with gcc-Version 4.0.3)
Something goes wrong with the integer to float conversion.
Maybe somebody out there understands what happens.
Essentially, when I subtract the (double) function value GRID_POINT(2) from
a variable which has been assigned the same value before this gives a
non-zero result and I really do not understand why.
The program prints
5.000000000000000000e-01; -2.775557561562891351e-17;
0.000000000000000000e+00
And the comparison
if(temp1==GRID_POINT(2))
has negative outcome.
When I replace
((double)(i)) by 2.0
everything behaves as expected. So the output is
5.000000000000000000e-01; 0.000000000000000000e+00; 0.000000000000000000e+00
But: even if the integer to float conversion is inexact (which, I think,
should not be the case) something like
temp1 = GRID_POINT(2);
temp3 = temp1-GRID_POINT(2);
should still result in temp3==0.0, whatever function GRID_POINT does.
What do You think?
Thank you!
---------------------------------------------------
#include <stdio.h>
double GRID_POINT(int i);
double GRID_POINT(int i)
{
return ( 0.1 + ( (80.1-0.1)/(400.0) )*((double)(i)) );
}
int main (void) {
double temp1, temp2, temp3;
temp1 = GRID_POINT(2);
temp2 = GRID_POINT(2);
temp3 = temp1-GRID_POINT(2);
printf("%.18e; %.18e; %.18e\n", temp1, temp3, temp1-temp2 );
if(temp1==GRID_POINT(2)){
printf("these two are equal\n");
}
return 0;
}
---------------------------------------------------
The following piece of code has (for me) completely unexpected behaviour.
(I compile it with gcc-Version 4.0.3)
Something goes wrong with the integer to float conversion.
Maybe somebody out there understands what happens.
Essentially, when I subtract the (double) function value GRID_POINT(2) from
a variable which has been assigned the same value before this gives a
non-zero result and I really do not understand why.
The program prints
5.000000000000000000e-01; -2.775557561562891351e-17;
0.000000000000000000e+00
And the comparison
if(temp1==GRID_POINT(2))
has negative outcome.
When I replace
((double)(i)) by 2.0
everything behaves as expected. So the output is
5.000000000000000000e-01; 0.000000000000000000e+00; 0.000000000000000000e+00
But: even if the integer to float conversion is inexact (which, I think,
should not be the case) something like
temp1 = GRID_POINT(2);
temp3 = temp1-GRID_POINT(2);
should still result in temp3==0.0, whatever function GRID_POINT does.
What do You think?
Thank you!
---------------------------------------------------
#include <stdio.h>
double GRID_POINT(int i);
double GRID_POINT(int i)
{
return ( 0.1 + ( (80.1-0.1)/(400.0) )*((double)(i)) );
}
int main (void) {
double temp1, temp2, temp3;
temp1 = GRID_POINT(2);
temp2 = GRID_POINT(2);
temp3 = temp1-GRID_POINT(2);
printf("%.18e; %.18e; %.18e\n", temp1, temp3, temp1-temp2 );
if(temp1==GRID_POINT(2)){
printf("these two are equal\n");
}
return 0;
}
---------------------------------------------------