且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有关float类型精度的一些问题

更新时间:2023-09-29 21:36:46

正如Pablo先前的解决方案正确显示的那样,问题出在
行中
As the previous solution by Pablo has correctly shown, the problem lies in the line

int cents = (amount - dollars) * 100; // here''s probably the prob



并且您的评论是正确的.发生的事情是(金额-美元)是一个浮点运算(与上面的注释之一相反),并提供0.849998或类似的值.这是由于以下事实:浮点表示不能正确存储所有有理数.相反,它使用可以由底数2的尾数表示的最接近的近似值.
在下一步中,将值乘以100(这也被强制为浮点数),您将得到:84.9998,然后通过强制转换为int将其截断为84.

那么出了什么问题呢?您不应该将值强制转换为int,而将四舍五入转换为int.这是一种实现方法:



and you were correct in your comment. What happens is that (amount - dollars) is a floating point operation (contrary what one of the comments above said) and delivers a value of 0.849998 or something the like. This is due to the fact that the floating point representation cannot correctly store all rational numbers. Instead it uses the nearest approximation that can be represented by a mantissa to the base 2.

In the next step that value is multiplied by 100 (which is also forced up to floating point) and you get: 84.9998, which is then truncated to 84 by the cast to int.

So what went wrong? You should not have cast the value to int but rounded it to int. Here is a way to do that:

double centsDlb = (amount - dollars) * 100.0;
int cents = floor (centsDlb + 0.5);



当然,您可以将两行代码压缩为一个代码.

Pablo提供的解决方案也可以帮助您,但请想象一下,如果有人指定79.849作为输入会发生什么.这就是为什么要使用舍入的原因.

使用floor函数时,请不要忘记包含"math.h".

而且,您可能想检查一下,如果有人向您的函数输入了负数,该怎么办.

希望对您有所帮助.



Of course you can contract both lines of code to a single one.

The solution given by Pablo will also get you there, but imagine what happens if someone specifies 79.849 as input. That''s why you want to use rounding.

Don''t forget to include "math.h" when you use the floor function.

And you might want to check what happens if someone enters a negative amount to your function.

Hope that helps.


如果金额为79.85,则美元(整数)将为79.
美分可能是85或84(浮点数不准确):您测试过吗?
如果cents为84,则c5将为84%10/5,即4/5,它是(作为整数)零.
如果美分是84,则可以尝试:
If amount is 79.85, dollars (which is an int) will be 79.
Cents may be 85, or 84 (floats are not accurate): did you test this?
If cents is 84, c5 will be 84 % 10 / 5 which is 4 / 5 which is (as an integer) zero.
If cents is 84, you may try:
int cents = (amount + 0.00001 - dollars) * 100.0;



希望这会有所帮助,
Pablo.



Hope this helps,
Pablo.


您已经在一个函数中混合了两个问题:

1.将浮点表示形式转换为定点表示形式
2.确定代表一定金额的最小硬币数量

我建议将两者分开.您可能还需要在其他地方进行该转换.显然,您目前还停留在转换上,但是解决方案1应该可以帮助您.在尝试解决2之前,请先查看它是否有效.

(即使这只是编程工作,将两个功能分离为不同的功能也不会受到伤害-无论如何,这都是您应该在实践中执行的操作)
You''ve mixed up two problems in one function:

1. converting a float representation into a fixed point representation
2. determining the minimum amount of coins to represent an amount of money

I suggest separating the two. You may need that conversion in other places too. Apparently you''re currently stuck with the conversion, but solution 1 should help there. See that it works before trying to solve 2.

(Even if this is just a programming assignment, it won''t hurt to separate the two functionalities in different functions - that''s what you should do in practice anyway)