C program not adding float correctly

可紊 提交于 2019-12-02 05:52:36

Finite precision of float.

A typical float can only represent about 232 different numbers. 117,810,016.0 and 1.0 are two of them. 117,810,017.0 is not. So the C sum of 117810016.0 + 1.0 results in the "best" answer of 117810016.0.

Using a higher precision type like double often will extend the range of +1 exact math, but even that will not be exact with large enough values (typically about 9.0*10e15 or 253).

If code is to retain using float, suggest limiting organism[i] to values to the inclusive range or ±8,388,608.0 (223).

Perhaps can code simply use integer types for this task like long long.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!