7

I'm new with C programming, and I've written this code as I've been asked to do something using mainly printf() and scanf(). I know there could be better ways to handle this, which I'll need to learn soon, but anyways, for now this is what I have:

int add1, add2, exponent, exponent_result, multiplier, parenthese, product, sub, total;

printf("Let's try a slightly more complex calculation, in which we'll use an exponent.\n\n");
printf("Type 5 whole numbers or integers.\n\n");
scanf("%i %i %i %i %i", &add1, &add2, &exponent, &multiplier, &sub);
printf("Out of the numbers you typed, we're going to make this operation: (%i + %i^%i) * %i - %i\n\n", add1, add2, exponent, multiplier, sub);

exponent_result = pow(add2, exponent);
parenthese = add1 + exponent_result;
product = parenthese * multiplier;
total = (add1 + exponent_result) * multiplier - sub;

printf("Per PEMDAS, the correct order of operation is Parentheses, then Exponents, then Multiplications and Divisions, and finally Additions and Subtractions.\n\n");
printf("Therefore: (%i + %i^%i) * %i - %i\n\n", add1, add2, exponent, multiplier, sub);
printf("...is equal to: (%i + %i) * %i - %i\n\n", add1, exponent_result, multiplier, sub);
printf("...is equal to: %i * %i - %i\n\n", parenthese, multiplier, sub);
printf("...is equal to: %i - %i\n\n", product, sub);
printf("...is equal to: %i", total);

If you run this code, you'll realize that the output for exponent_result, which is calculated using the pow() function, always has 1 subtracted from it. For instance, if exponent_result is supposed to be the result of 5^3, the result for that will be 124 instead of 125.

What am I doing wrong?

Fyi, I have this at the beginning of my file.

#include <stdio.h>
#include <math.h>
Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
Isaac Asante
  • 435
  • 1
  • 5
  • 21
  • Had you checked your problem. As while I tried to run your program it working fine. Without any issue. If you still have problem share the online ide runnable code so that we can check the issue. I got the proper output. Do you want to say that the input what you have placed are having certain issue? – Jaffer Wilson Aug 31 '17 at 10:12
  • do not try to use float atirthmetics for integer calculations. Integers are always precise, floats are almost always not. I – 0___________ Aug 31 '17 at 10:28
  • 1
    Avoid truncation, round instead `exponent_result = lround(pow(add2, exponent));` – chux - Reinstate Monica Aug 31 '17 at 13:41

1 Answers1

7

pow is evaluated in floating point arithmetic, and is probably implemented as pow(x, y) = exp(y * log(x)).

This can cause the result to "go off": you probably get a value just shy of 125, which is truncated to 124 when the double return type from pow is converted back to an int.

The simplest remedy is to build your own pow function for integral arguments. See The most efficient way to implement an integer based power function pow(int, int)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • seems that using `5` as radix triggers that often: https://stackoverflow.com/questions/45960102/inaccurate-calculation-caused-by-inaccurate-data-type-usage/45960231#45960231 (asked 2 days ago :)) – Jean-François Fabre Aug 31 '17 at 13:03