为什么这个程序没有给出120的正确平方?我是初学者。 [英] Why does this program doesn't give correct square of 120? I am a beginner.

查看:59
本文介绍了为什么这个程序没有给出120的正确平方?我是初学者。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream  >  
#include < cmath < span class =code-keyword>>

使用 namespace std;

int squarefunction( int x)
{
return pow(x, 2 );
}

int main()
{
cout<< squarefunction( 120 )<< ENDL;
return 0 ;
}





我的尝试:



而不是返回14400而是返回14399

解决方案

std :: pow - cppreference.com [ ^ ]函数是一个浮点函数。所以第一个参数从 int 转换为 double double 从函数返回时,结果然后转换回 int



浮点值的问题是大多数数字无法准确表示,导致实际值的偏移非常小。这发生在这里:从 pow()函数返回的 double 类似于14399.999999 ....



double 转换为 int 时,小数点后的所有数字都被忽略(切断)。所以你会得到14399返回。避免这种情况的常见解决方案是在转换为 int 时添加+/- 0.5的偏移量:

 int squarefunction(int x)
{
//我们知道这里的结果永远不会为负,所以可以省略
//符号检查(然后减去0.5)
return static_cast< ; int>(pow(x,2)+ 0.5);
}

请注意,我添加了强制转换以避免编译器警告可能会导致精度损失。


当我尝试它时,它完全符合我的预期,并返回14400,所以它;依赖于系统。可能是你的系统上的浮点到整数转换导致这种情况:pow期望使用 double 值而不是整数,并返回 double value - 隐式转换为 int 以从方法返回它。也许,double的内部形式不完全是14400,而是14399.99999999999999999,当转换时它将被截断为14399.

请改为尝试:

 int squarefunction(int x)
{
return x * x;
}

它适用于所有系统。


pow 'promote'其 int 参数 double 1,执行计算并返回 double 结果。这样的(固有近似的) double 结果然后被转换(截断)到函数内的 int 。例如,请参阅 c ++ - Strange pow(x,y);行为 - 堆栈溢出 [ ^ ]。

#include <iostream>
#include <cmath>

using namespace std;

int squarefunction(int x)
{
    return pow(x,2);
}

int main()
{
    cout << squarefunction(120) << endl;
    return 0;
}



What I have tried:

Instead of returning 14400 it is returning 14399

解决方案

The std::pow - cppreference.com[^] function is a floating point function. So the first parameter is converted from int to double and the double result is then converted back to int when returning from your function.

The problem with floating point values is that most numbers can't be exactly represented resulting in very small offsets to the real value. This has happened here: The double returned from the pow() function is something like 14399.999999... .

When a double is converted to int, all digits after the decimal point are ignored (cut off). So you will get 14399 returned. A common solution to avoid such is adding an offset of +/- 0.5 when casting to int:

int squarefunction(int x)
{
    // We know that the result is never negative here so that the
    //  sign check (subtracting 0.5 then) can be omitted
    return static_cast<int>(pow(x, 2) + 0.5);
}

Note that I have added casting to avoid compiler warnings about possible loss of precision.


When I try it, it does exactly what I expect, and returns 14400, so it;'s system dependant. Probably, it's the floating-point-to-integer conversion on your system that causes this: pow expects to work with double values not integer, and returns a double value - which is implicitly cast to an int to return it from the method. Probably, the internal form of the double is not exactly 14400, but 14399.99999999999999999 which rets truncated to 14399 when it is converted.
Try this instead:

int squarefunction(int x)
{
    return x * x;
}

And it'll work on all systems.


The pow 'promotes' its int arguments to double ones, performs the computation and returns a double result. Such a (inherently approximated) double result is then casted (truncated) to an int inside your function. See, for instance c++ - Strange pow(x, y); behaviour - Stack Overflow[^].


这篇关于为什么这个程序没有给出120的正确平方?我是初学者。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆