圆形误差从double到字符串转换,而无需使用%F [英] Round error converting from double to string without using %f

查看:135
本文介绍了圆形误差从double到字符串转换,而无需使用%F的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下这个问题:

如果你没有用C%F,如何写一个C程序打印十进制数高达2位小数,而不%F?

这是我的尝试:

#include <stdio.h>
#include <math.h>

void dbl2str(char *s, double number, int decimals)
{
    double integral, fractional;
    int n, i;

    fractional = modf(number, &integral);
    n = sprintf(s, "%d%c", (int)integral, decimals ? '.' : 0);
    for (i = 0; i < decimals; i++) {
        fractional *= 10;
        s[n + i] = '0' + (int)fractional;
        fractional = modf(fractional, &integral);
    }
    s[n + i] = '\0';
}

int main(void)
{
    char s[32];

    dbl2str(s, 3.1416, 4);
    printf("%s\n", s);
    dbl2str(s, 3.159, 4);
    printf("%s\n", s);
    dbl2str(s, 3.04, 2);
    printf("%s\n", s);
    return 0;
}

输出:

3.1415
3.1589
3.04

正如你可以看到有圆的错误,有没有一种方式来获得正确的输出?

As you can see there are round errors, is there a way to get the correct output?

推荐答案

我会做这样的事情。

void dbl2str(char *s, double number, int decimals)
{
    double integral, fractional;
    int n, i;

    /* use an epsilon and add the correct rounding value */
    double eps= 1e-9;
    double round = 0.5*pow(10,-decimals);

    fractional = modf(number+round+eps, &integral);
    n = sprintf(s, "%d%c", (int)integral, decimals ? '.' : 0);
    for (i = 0; i < decimals; i++) {
        fractional *= 10;
        s[n + i] = '0' + (int)fractional;
        fractional = modf(fractional, &integral);
    }
    s[n + i] = '\0';
}

这样的话,我想补充0.5 * 10 ^ -decimals获得(INT)分数来正确地舍入到小量。

This way, I add 0.5*10^-decimals to get the (int) fractional to round correctly up to an epsilon.

它适用于您的输入:

3.1416
3.1590
3.04

当然,你可能需要调整ε,根据不同的数字,再想想,你已经用尽你的16位precision的情况。总的来说,这是很难做到的权利。

Of course, you may need to adjust the epsilon, depending on the digits, and then think about the case where you have exhausted your 16 digit precision. Overall, this is very hard to do right.

这篇关于圆形误差从double到字符串转换,而无需使用%F的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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