Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 pow() function
Index -> Programming, C -> C Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
littlewontons




PostPosted: Fri Apr 11, 2008 10:24 pm   Post subject: pow() function

Hi Everybody,

I am new to C and I just encountered a problem. When I use the pow() function, I get some problems. The following is my code:
code:

#include <stdio.h>
#include <math.h>

int main( void )
{
    int i = 0;
    for( i = 1; i < 10; i++ )
    {
        printf( "%d\n", (int)pow( 10, i ) );
    }
    getch();
    return 0;
}


My output is the following:
code:

10
99
1000
9999
100000
1000000
9999999
99999999
999999999


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
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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

0.1
0.2
0.3
0.4
0.5
0.599999999
0.699999999
0.799999999
...

How look your results?
OneOffDriveByPoster




PostPosted: Sat Apr 12, 2008 10:02 am   Post subject: Re: pow() function

0.100000
0.200000
0.300000
0.400000
0.500000
0.600000
0.700000
0.800000
0.900000
1.000000
...
CodeMonkey2000




PostPosted: 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
Sponsor
sponsor
Dragan




PostPosted: 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




PostPosted: 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();
    return 0;
}
OneOffDriveByPoster




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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?
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 26 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: