如何在C ++中编写良好的round_double函数?

| 我正在尝试编写良好的round_double函数,它将以指定的精度四舍五入: 1。
double round_double(double num, int prec)
{
    for (int i = 0; i < abs(prec); ++i)
        if(prec > 0)
            num *= 10.0;
        else
            num /= 10.0;
    double result = (long long)floor(num + 0.5);
    for (int i = 0; i < abs(prec); ++i)
        if(prec > 0)
            result /= 10.0;
        else
            result *= 10.0;
    return result;
}
2。
double round_double(double num, int prec)
{
    double tmp = pow(10.0, prec);
    double result = (long long)floor(num * tmp + 0.5);
    result /= tmp;
    return result;
}
此功能无法完成我想做的事,但我认为它们还不够好。因为从精度= 13-14开始,它们将返回不良结果。 我确定可以写出更好的double_round的原因是,仅以指定的精度(例如18)通过
cout
打印数字,比我的函数结果打印的结果更好。 例如这部分代码:
int prec = 18;
double num = 10.123456789987654321;
cout << setiosflags(ios::showpoint | ios::fixed)
<< setprecision(prec) << \"round_double(\" << num << \", \" 
<< prec << \") = \" << round_double(num, prec) << endl;
第一个first5ѭ打印
round_double(10.123456789987655000, 18) = -9.223372036854776500
,第二个second4打印。 如何在C ++中编写良好的round_double函数?还是已经存在?     
已邀请:
不要强制转换为cast7ѭ,这会强制转换为范围有限的整数,超出了10 ^ 13的要求(64位没有整数部分,则为19)。只需叫
floor
就足够了。
double round_double(double num, int prec)
{
    double tmp = pow(10.0, prec);
    double result = floor(num * tmp + 0.5);
    result /= tmp;
    return result;
}
请注意,迈克也是正确的,您只能在两倍范围内代表有限的范围。如果您需要干净的十进制响应,则不是很好。但是
long long
是导致您数字古怪的原因。     
问题是浮点表示。二进制表示形式不能完全表示所有十进制数字,而只能具有有限的精度。 “ 11”通常表示IEEE754指定的64位二进制表示形式,具有52位小数部分。这样可以得到大约16位十进制数字的精度。 如果您需要的精度更高,那么最好的选择可能是使用任意精度的算术库,例如GMP。您的编译器可能提供或未提供比
double
更高精度的
long double
类型。 编辑:对不起,我没有注意到您得到的是完全不正确的结果。另一个答案是,这是由于转换为“ 7”溢出所致。     
另一种方法是基于精度的二进制数舍入。下面的示例实现-不确定它是否对您有用,但是由于您让我参与其中,所以我认为我会把它扔在那里。 笔记: 它使用Linux系统上常见的ieee754.h标头:可以轻松地将其移植到Windows,但这无疑是一点点黑客行为,是否适合在任何给定的生产代码中使用都是逐案的。 您可以近似近似十进制的十进制数,例如将所需的十进制精度乘以10并除以3(基于2 ^ 10〜= 10 ^ 3)。 1位精度的输入数字(10.1234 ...)为8;与2等于10等。 另外,恕我直言,十进制舍入最好在输出时或使用具有十进制能力的表示形式时进行(例如,存储storing15的尾数和10的幂)。
#include <ieee754.h>
#include <iostream>
#include <iomanip>

double round_double(double d, int precision)
{
    ieee754_double* p = reinterpret_cast<ieee754_double*>(&d);
    std::cout << \"mantissa  0:\" << std::hex << p->ieee.mantissa0
        << \", 1:\" << p->ieee.mantissa1 << \'\\n\';
    unsigned mask0 = precision < 20 ? 0x000FFFFF << (20 - precision) :
                                      0x000FFFFF;
    unsigned mask1 = precision <  20 ? 0 :
                     precision == 53 ? 0xFFFFFFFF :
                                       0xFFFFFFFE << (32 + 20 - precision);
    std::cout << \"masks     0:\" << mask0 << \", 1: \" << mask1 << \'\\n\';
    p->ieee.mantissa0 &= mask0;
    p->ieee.mantissa1 &= mask1;
    std::cout << \"mantissa\' 0:\" << p->ieee.mantissa0
        << \", 1:\" << p->ieee.mantissa1 << \'\\n\';
    return d;
}

int main()
{
    double num = 10.123456789987654321;

    for (int prec = 1; prec <= 53; ++prec)
        std::cout << std::setiosflags(std::ios::showpoint | std::ios::fixed)
            << std::setprecision(60)
            << \"round_double(\" << num << \", \"  << prec << \") = \"
            << round_double(num, prec) << std::endl;
}
输出...
mantissa  0:43f35, 1:ba76eea7
masks     0:fff80000, 1: 0
mantissa\' 0:0, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 1) = 8.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:fffc0000, 1: 0
mantissa\' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 2) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:fffe0000, 1: 0
mantissa\' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 3) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:ffff0000, 1: 0
mantissa\' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 4) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:ffff8000, 1: 0
mantissa\' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 5) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:ffffc000, 1: 0
mantissa\' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 6) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:ffffe000, 1: 0
mantissa\' 0:42000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 7) = 10.062500000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:fffff000, 1: 0
mantissa\' 0:43000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 8) = 10.093750000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:7ffff800, 1: 0
mantissa\' 0:43800, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 9) = 10.109375000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:3ffffc00, 1: 0
mantissa\' 0:43c00, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, a) = 10.117187500000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:1ffffe00, 1: 0
mantissa\' 0:43e00, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, b) = 10.121093750000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:fffff00, 1: 0
mantissa\' 0:43f00, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, c) = 10.123046875000000000000000000000000000000000000000000000000000

etc....
    

要回复问题请先登录注册