Can someone help me find out what my problem is? And could you tell me what I did wrong so I can prevent this in the future? Thanks =)
Sponsor Sponsor
OneOffDriveByPoster
Posted: Fri Apr 11, 2008 10:52 pm Post subject: Re: pow() function
Your compiler/libraries/processor/environment is not up to spec. AFAIK, pow should work fine for those values on modern systems. Write your own integer power function to avoid this.
CodeMonkey2000
Posted: Fri Apr 11, 2008 11:00 pm Post subject: RE:pow() function
Wow that is really weird. Oh well, as OneOffDriveByPoster said, make your own pow function that returns i to the j-th power. i to the j-th power is the same as 1*i*i...j times.
Dragan
Posted: Sat Apr 12, 2008 4:51 am Post subject: RE:pow() function
@OneOffDriveByPoster, try this code on any modern system.
#include <stdio.h>
#include <math.h>
Quote:
int main( void )
{
int i = 0;
int j = 0;
for( i = 1; i < 100000; i++ )
{
j = j + 0.1;
printf( "%d\n", j ); // or %f, i forgot how to print float number
}
getch();
return 0;
}
you expect:
0.1
0.2
0.3
0.4
0.5
0.6
0.7
....
but, you will see something interesting
OneOffDriveByPoster
Posted: Sat Apr 12, 2008 9:39 am Post subject: Re: RE:pow() function
Dragan @ Sat Apr 12, 2008 4:51 am wrote:
@OneOffDriveByPoster, try this code on any modern system.
You chose 0.1, which we all know is 0.0001100110011... in binary. The OP had nice whole numbers, the operands and the correct result can all be represented as an IEEE double. I don't know what underlying algorithm they are using in the OP's case, but I would not call their implementation good.
Oh, and you are assigning to an int every time. I replaced with double (and it really does not look weird for the number of digits that shows up and the numbers that I bothered to look at).
Dragan
Posted: Sat Apr 12, 2008 9:55 am Post subject: RE:pow() function
OK. j need to be float (that was my mistake), I try that code in Microsoft visual c++ 6.0 on many systems and i got results
Posted: Sat Apr 12, 2008 10:39 am Post subject: RE:pow() function
Isn't Microsoft Visual C++ 6.0 really out dated? Switch to minGW, or update it.
Sponsor Sponsor
Dragan
Posted: Sat Apr 12, 2008 11:08 am Post subject: RE:pow() function
@OneOffDriveByPoster I don't know why but I get wrong results, maybe because of old Microsoft Visual, I realy need to upate Microsoft Visual c++.
Saad
Posted: Sat Apr 12, 2008 1:20 pm Post subject: Re: pow() function
These errors are floating point inaccuracies. No floating point number can accurately represented. Your results can depend on how your CPU handles floating point numbers
As for the original question, it's a casting issue. Try the following
c:
#include <stdio.h> #include <math.h>
int main(void) { int i;
for( i = 1; i < 10; i++ ) { //~~ printf( "%d\n", (int)pow( 10, i ) ); printf("%f\n",pow(10, i ));
}
getch();
return0;
}
OneOffDriveByPoster
Posted: Sat Apr 12, 2008 1:55 pm Post subject: Re: pow() function
Saad @ Sat Apr 12, 2008 1:20 pm wrote:
No floating point number can accurately represented.
*hack* *cough*
littlewontons
Posted: Sat Apr 12, 2008 4:10 pm Post subject: RE:pow() function
Thanks guys, but does that mean my C library is out of date? I am not too sure why it does this exactly. I have thought about alternate ways to fix it, but for the future, can i fix it some how? I am using devc++ 4.9.9.2.
littlewontons
Posted: Sat Apr 12, 2008 4:13 pm Post subject: RE:pow() function
Oh yea, Sadd, I have tried that already, it outputs correct, it just does not work with %d and (int)
Dragan
Posted: Sat Apr 12, 2008 4:23 pm Post subject: RE:pow() function
@littlewontons, you need to do that on alternate way, I think that is not software problem, that is hardware problem (in processor, in float point unit)
littlewontons
Posted: Sat Apr 12, 2008 4:28 pm Post subject: RE:pow() function
But I tried it at school, and I get the same error. They are different machines. Does this mean that they both have problems?