-9

I'm making a simple calculator that you can choose a function, then 2 inputs to get your answer. It's a neat little program and everything is running smoothly except for the powers. Every number works correctly.

But according to this: 5^2=24, 5^3=624. I am using pow(number1,number2).

#include <iostream>
#include<math.h>

using namespace std;

int main()
{
    for(;;){
    int func;
    int number1;
    int number2;
    cout << "Input your function (+,-,x,/,Square,Power)(1,2,3,4,5,6) ";
    cin >> func;
    cout << "input #1: ";
    cin >> number1;
    cout << "input #2: ";
    cin >> number2;
    if (func==1){
        int answer;
        answer = number1 + number2;
        cout << number1 << " + " << number2 << " = " << answer << endl;
    }
    else {
            if (func==2){
            int answer;
            answer = number1 - number2;
            cout << number1 << " - " << number2 << " = " << answer << endl;
        }
        else {
            if (func==3){
                int answer;
                answer = number1 * number2;
                cout << number1 << " x " << number2 << " = " << answer << endl;
            }
            else {
               if (func==4){
                    int answer;
                    answer = number1 / number2;
                    int R = number1 % number2;
                    cout << number1 << " / " << number2<< " = " << answer  << " R "<< R << endl;
               }
                else {
                    if (func==5){
                        int answer;
                        answer = pow(number1,0.5);
                        cout << "√" << number1 << "=" << answer << endl;
                    }
                    else {
                        if (func==6){
                            int answer;
                            answer = pow(number1,number2);
                            cout << "√" << number1 << "^" << number2 << "=" << answer << endl;
                        }
                    }
                }
            }
            }
        }

    }
    }
Melebius
  • 6,183
  • 4
  • 39
  • 52
Trevader24135
  • 77
  • 1
  • 4
  • 9
  • Floating point calculations are not exact. – Brian Bi Mar 08 '14 at 02:33
  • Probably b/c you are using `int` for the variables instead of `double`. – Shafik Yaghmour Mar 08 '14 at 02:33
  • 3
    Have you ever used `else if`? you should use that instead of keeping on doing `else{if {}}` – stack smasher Mar 08 '14 at 02:33
  • 2
    You might also try a switch statement instead of a bunch of if-else statements in a row. Not related to the question, but just a thought :-) – developering Mar 08 '14 at 02:36
  • I'm skeptical this is a floating point error guys. Even a very imprecise floating point can represent these numbers exactly. Here is [a quick Ideone showing this should be fine](http://ideone.com/ar1MBv). – Radiodef Mar 08 '14 at 02:43
  • For your information, C++ already uses the `^` operator and it does not denote a power. – Ben Voigt May 22 '14 at 23:48
  • [Why pow(10,5) = 9,999 in C++](http://stackoverflow.com/q/9704195/995714), [Strange pow(x, y); behaviour](http://stackoverflow.com/q/14714115/995714), [Why does pow(n,2) return 24 when n=5, with my compiler and OS?](http://stackoverflow.com/q/25678481/995714)... – phuclv Jan 13 '17 at 03:43

3 Answers3

8

pow() returns numbers as floating point. What is really returning is 24.99997 or something similar. But then you truncate it by assigning it to an integer and it comes out as 24. It's ok to assignit to an integer in this case because you know then answer will always be an integer so you need to round it - not truncate it. The normal way to round to an integer is to add 0.5 like so:

int answer;
answer = pow(number1,number2)+0.5;

On the other hand the square root one probably should NOT be assigned to an integer - almost every square root will have decimal places.

Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32
2
int answer;
^^^
answer = pow(number1,number2);

pow() function will first convert number1 and number2 to float. Note that floating point calculations are not exact, which means you will probably get a value 24.999999 instead of exact 25. After computation, it then casts the result to int, which will cut off the floating part (e.g. 24.999999 => 24).

Try to use float to catch the result:

float answer;
answer = pow(number1,number2);

If you do want to store the result to int, you should round it correctly:

int answer;
answer = (int) (pow(number1,number2) + 0.5);

To read on, check out What Every Programmer Should Know About Floating-Point Arithmetic.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
0

Try using double answer; instead. pow is returning a floating point number, and you are doing an implicit cast to int. That will cause the behavior you are seeing.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173