在一些用C n次方根precision错误++ [英] precision error in nth root of a number in C++

查看:127
本文介绍了在一些用C n次方根precision错误++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,从previous线程关于这个主题,使用float运算导致precision异常。但有趣的是我观察到了同样的功能表现两种不同的方式。照片使用COUT输出 4 的,但如果我保存结果到一个变量,那么结果为 3

I know from previous threads on this topic that using float arithmetic causes precision anomalies. But Interestingly I observed that the same function is behaving in two different ways.
Using COUT output is 4 but if I am saving the result into a variable, then result is 3!

#include <iostream>
#include <cmath>
using namespace std;
#define mod 1000000007
long long int fastPower(long long int a, int n){
    long long int res = 1;
    while (n) {
            if (n & 1) res = (res * a) % mod;
            n >>= 1; a = (a * a) % mod;
    }
    return res;
}
int main() {
    int j = 3;
    cout << pow(64, (double)1.0/(double)j) << endl; // Outputs 4
    int root = pow(64, (double)1.0/(double)j);
    cout << root << endl;                           // Outputs 3
    /* As said by "pts", i tried including this condition in my code but including this line in my code resulted in TimeLimitExceeded(TLE). */
    if (fastPower(root+1,j) <= 64) root++;
    cout << root << endl;                           // Outputs 4 :)
    return 0;
}

code输出Ideone.com
现在,我们怎样才能避免在编程大赛这样的错误。 我不想用'圆'功能,因为我需要root的唯一整数值​​。即
63 (1/6) = 1,20 (1/2) = 4,等等
我应该如何修改我的code,使正确的结果保存在根变量。

Code output on Ideone.com
Now, how can we avoid such errors in a programing contest. I do not want to use 'round' function because I need only integer value of root. i.e
63(1/6) = 1, 20(1/2) = 4, etc...
How should I modify my code so that correct result is stored in the root variable.

推荐答案

据我所知,在C和C没有单行的答案++用于获取 A B >次方根向下取整。

As far as I know, there is no single-line answer in C and C++ for getting the ath root of b rounded down.

作为一个快速的解决方法,你可以这样做:

As a quick workaround, you can do something like:

int root(int a, int b) {
  return floor(pow(b, 1.0 / a) + 0.001);
}

这不适合每个值,但通过调整工作的常数( 0.001 ),你可能会得到幸运,它会工作,为测试输入。

This doesn't work for every value, but by adjusting the constant (0.001), you may get lucky and it would work for the test input.

作为一种变通方法,使用你已经在使用它,如果它返回研究,然后尝试 POW 的R - 1 研究 R + 1 乘以它背面(使用整数的快速指数)。这将工作的大部分时间。

As a workaround, use pow as you use it already, and if it returns r, then try r - 1, r and r + 1 by multiplying it back (using fast exponentiation of integers). This will work most of the time.

如果你需要一个解决方案,工程时间的100%,那么就不要使用浮点数。使用例如与幂二进制搜索。有更快的算法(如牛顿迭代),但是如果你使用它们的整数,那么你需要,尽快为他们停止会聚编写自定义的逻辑来找到确切的解决方案。

If you need a solution which works 100% of the time, then don't use floating point numbers. Use for example binary search with exponentiation. There are faster algorithms (such as Newton iteration), but if you use them on integers then you need to write custom logic to find the exact solution as soon as they stop converging.

这篇关于在一些用C n次方根precision错误++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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