花车是不精确

编程入门 行业动态 更新时间:2024-10-21 18:47:42
本文介绍了花车是不精确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我百思不得其解。我也没有解释为什么本次测试使用双击数据类型时通过,但使用浮动数据类型时失败。考虑code的片断。

I am puzzled. I have no explanation to why this test passes when using the double data type but fails when using the float data type. Consider the following snippet of code.

float total = 0.00; for ( int i = 0; i < 100; i++ ) total += 0.01;

照理总为1.00,但它是等于0.99。为什么会出现这样的情况?我用GCC和铿锵编译,编译器都具有相同的结果。

One would anticipate total to be 1.00, however it is equal to 0.99. Why is this the case? I compiled with both GCC and clang, both compilers have the same result.

推荐答案

在十进制0.01的值是pssed作为该系列的前$ P $:A1 *(1/2)+ A2 *(1/2)^ 2 + A3 *(1/2)^ 4 +等,其中在是0或1。

The value for 0.01 in decimal is expressed as the series: a1*(1/2) + a2*(1/2)^2 + a3*(1/2)^4 + etc. where aN is a zero or one.

我让你找出A1,A2和多少小数位(在)的要求的具体数值。在某些情况下,小数部分不能被重新用有限系列(1/2)^ n个值的psented $ P $

I leave it to you to figure out the specific values of a1, a2 and how many fractional bits (aN) are required. In some cases a decimal fraction cannot be represented by a finite series of (1/2)^n values.

有关这一系列来总结,以小数0.01需要一个超越(存储在一个浮的位数比特的全字减去比特为一个数签署和指数)。但由于双有更多的位,然后0.01小数可以/也许/也许(你们做计算)是precisely定义。

For this series to sum to 0.01 in decimal requires that aN go beyond the number of bits stored in a float (full word of bits minus the number of bits for a sign and exponent). But since double has more bits then 0.01 decimal can/might/maybe (you do the calculation) be precisely defined.

更多推荐

花车是不精确

本文发布于:2023-11-27 04:23:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:花车   不精确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!