Why does pow(5,2) become 24? [closed]

陌路散爱 提交于 2019-12-10 00:35:20

问题


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;
                        }
                    }
                }
            }
            }
        }

    }
    }

回答1:


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.




回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/22264236/why-does-pow5-2-become-24

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!