# Thread: The case of missing '1'

1. ## The case of missing '1'

Hi there,

I've made a basic code for solving exponents:
Code:
```#include <stdio.h>
#include <math.h>
/*exponent*/
int main ()
{
int a,b,c;
printf("Format: a^b\n");
printf("Enter a and b\n");
scanf("%d%d", &a, &b);
c = exp(b*log(a));
c = c+1; /*WHY???*/
printf("%d", c);
return 0;
}```
When I run it, strangely the value of output is less by '1' than the correct one. Now this can be easily taken care of by the 'c = c+1' equation. But, where did that '1' go?

2. Aren't you just using the wrong function?
a^b is pow(a, b);
exp(x); is e^x.

3. Not really, I'm making use of the following properties:
:
Here log represents natural logarithm which has the base 'e'.
By the way, thanks. I was not aware of the 'pow' function.

4. It works for me ... in that, when I run it, the result is one higher than it should be due to your "correction". What input/output are you seeing?

5. Maybe there is some bug in my compiler, Matticus.
Here's a snip of the input without the correction with the respective output:

I am using jGRASP version 2.0.1_01.

6. Originally Posted by culater
Code:
```int a,b,c;
c = exp(b*log(a));```
The parameters toandare doubles, and the results are doubles, too.

Because you use the int type, the compiler promotes the parameters a and b to double automatically (according to standard C rules, that is), then truncates the result to an integer when storing to c, exactly as it should.

You seem to expect different behaviour, and therefore added the c = c+1; /*WHY???*/ line. That is your error.

If you need correct rounding for printing only -- and rounding is the correct thing to do here, since we are dealing with floating-point precision values here --, use
Code:
`printf("%.0f\n", exp( (double)b * log( (double)a ));`
The (double)b and (double)a are explicit casts. They are technically optional, but recommended: having them there tells both the compiler and the human reader that the int parameters should be cast to double type before being operated on.

In practice, I'd declare a, b, and c as double, and use
Code:
```    if (a < 0.0)
c = exp(b * log(-a));
else
if (a > 0.0)
c = exp(b * log(a));
else
if (b == 0.0)
c = 1.0;
else
c = 0.0;
printf("%.0f\n", c);```
since there is no practical reason to use int here at all, and your implementation would choke at nonpositive a. The last two clauses implement 00 = 1, 0b = 0 for all b ≠ 0; you might wish to modify them. (In particular, typically 00=1, 0b=0 for all b>0, and 0b is undefined for all b<0, so you might wish to insert a last if clause to print an error message instead if b is negative.)

Thepattern for a double is %lf.

7. Thanks a lot for your time and patience Nominal Animal! The code runs smoothly now. I will keep this concept in mind.