Ç - 求和双 - precision [英] C - Summation with Double - Precision

查看:69
本文介绍了Ç - 求和双 - precision的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有双格式

样品例如:

double K=0, L=0, M=0;
scanf("%lf %lf %lf", &K, &L, &M);
if((K+L) <= M) printf("Incorrect input");
else printf("Right, K=%f, L=%f, M=%f", K, L, M);


我的测试输入:

K = 0.1,L = 0.2,M = 0.3 - >条件,但去的'其他'语句

K = 0.1, L = 0.2, M = 0.3 -> Condition but goes to 'else' statement.

我怎样才能纠正这种差异?还有什么其他的方法来求和?

How I can correct this difference? Is there any other method to summation?

推荐答案

在双precision IEEE 754二进制浮点格式的世界 0.1(英特尔等处理器使用的那些) + 0.2 == 0.30000000000000004 :-)和 0.30000000000000004!= 0.3 (请注意,在的奇妙的世界双 S,0.1,0.2和0.3不存在为精确的数量。有一些双数字是非常接近他们,但如果你以饱满precision打印他们,他们也不会是0.1,0.2和0.3)

In the world of Double Precision IEEE 754 binary floating-point format (the ones used on Intel and other processors) 0.1 + 0.2 == 0.30000000000000004 :-) And 0.30000000000000004 != 0.3 (and note that in the marvelous world of doubles, 0.1, 0.2 and 0.3 don't exist as "exact" quantities. There are some double numbers that are very near them, but if you printed them with full precision, they wouldn't be 0.1, 0.2 and 0.3)

要笑了一下,试试这个: HTTP://pages.cs.wisc埃杜/〜rkennedy /确切浮动

To laugh a little, try this: http://pages.cs.wisc.edu/~rkennedy/exact-float

将一个十进制数,并期待在第二排和第三排,它显示了如何将数是真正重新在内存psented $ P $。这是德尔福,但双击是德尔福和可能所有的C编译器针对英特尔处理器相同(他们被称为双击浮动在C)

Insert a decimal number and look at the second and third row, it shows how the number is really represented in memory. It's for Delp but Double and Single are the same for Delphi and for probably all the C compilers for Intel processors (they are called double and float in C)

如果你想尝试自己,看看这个 http://ideone.com/WEL7h

And if you want to try for yourself, look at this http://ideone.com/WEL7h

#include <stdio.h>

int main()
{
    double d1 = (0.1 + 0.2);
    double d2 = 0.3;

    printf("%.20e\n%.20e", d1, d2);

    return 0;
}

输出:结果
3.00000000000000044409e-01结果
2.99999999999999988898e-01

output:
3.00000000000000044409e-01
2.99999999999999988898e-01

(注意输出是编译器相关的。根据选项, 0.1 + 0.2 可以编译并四舍五入至0.3)

(be aware that the output is compiler dependant. Depending on the options, 0.1 + 0.2 could be compiled and rounded to 0.3)

这篇关于Ç - 求和双 - precision的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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