为什么我会看到一个双变量初始化为21.4,如21.399999618530273? [英] Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?

查看:85
本文介绍了为什么我会看到一个双变量初始化为21.4,如21.399999618530273?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  double r = 11.631; 
double theta = 21.4;

在调试器中,这些显示为 11.631000000000000 21.399999618530273



如何避免这种情况? h2_lin>解决方案

这些准确性问题是由于浮点数的内部表示,避免这种情况的可能性不大。 / p>

顺便说一下,在运行时打印这些值通常仍会导致正确的结果,至少使用现代C ++编译器。对于大多数操作,这不是一个问题。


double r = 11.631;
double theta = 21.4;

In the debugger, these are shown as 11.631000000000000 and 21.399999618530273.

How can I avoid this?

解决方案

These accuracy problems are due to the internal representation of floating point numbers and there's not much you can do to avoid it.

By the way, printing these values at run-time often still leads to the correct results, at least using modern C++ compilers. For most operations, this isn't much of an issue.

这篇关于为什么我会看到一个双变量初始化为21.4,如21.399999618530273?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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