gpt4 book ai didi

c - While 循环忽略比较两个浮点变量的 bool 表达式

转载 作者:行者123 更新时间:2023-11-30 14:47:26 25 4
gpt4 key购买 nike

编辑:
我解决了这个问题,首先将浮点值乘以 100,然后使用 roundf() 函数对其进行舍入,然后将其转换为要存储在整数中的整数多变的。从那时起,我用整数值进行了剩余的操作,一切正常。尽管 @JacobBoertjes 提供的解决方案实际上有效,但我的作业要求我使用 cs50.h 库中的 get_float() ,所以我没有实现它。这是最终的代码:

// Get user input as a positive float value
float f_change;
do {
printf("Change owed: ");
f_change = get_float();
} while(f_change < 0);

// Round & cast
int int_change = (int) roundf(f_change * 100);
<小时/>

我的程序接受一定数量的金钱,比如 4.20 美元,并计算出可以代表该值(value)的最少硬币数量。例如,以 4.20 美元为输入的程序所需的输出为:16 个硬币 ($4.00),2 个硬币 ($0.20)。

我的程序成功计算了硬币数,但未能计算出在努力工作的同时。导致此失败的原因是代码中的第二个 for 循环0.10 >= 0.10 计算结果不为 true,因此循环的最后一次迭代永远不会发生。我究竟做错了什么?

这是代码。我提供了测试打印语句,并将其输出写为注释。

#include <stdio.h>
#include <cs50.h>

int main(void) {

// Fake user input
float owed_coin = 4.2f;

// Initialize coin variables
int coin_count = 0;

float quarters = 0.25f,
dimes = 0.10f;

// Calculate quarters
while(owed_coin >= quarters) {
owed_coin -= quarters;
coin_count += 1;
}
printf("owed_coin: %.2f\ncoin_count: %d\n\n", owed_coin, coin_count);
// Prints owed_coin: 0.20
// coin_count: 16

// Calculate dimes
while(owed_coin >= dimes) {
owed_coin -= dimes;
coin_count += 1;
}
printf("owed_coin: %.2f\ncoin_count: %d\n\n", owed_coin, coin_count);
// Prints owed_coin: 0.10
// coin_count: 17

}

最佳答案

浮点比较通常是一个坏主意,因为 float 通常变得不精确,因此不会完全相等。正如 @bruceg 提到的,一个好的解决方案是将您的货币值(value)保持为美分,这样您就可以避免使用 float 。

您可以将 floatowed_coin = 4.2f; 替换为 intowed_coin = 420;

在收集用户对此数字的输入方面,我建议使用 scanf

int n1, n2;

printf("Please enter amount:");
scanf("%d.%2d", &n1, &n2);

owed_coin = n1*100 + n2;
<小时/>

另一种解决方案允许您将变量保留为 float ,并仅比较两者之间非常小的差异。可以在这里找到:What's wrong with using == to compare floats in Java?

它使用 Java Math 库,但类似的解决方案在 C 中可能如下所示:

while((owed_coin - dimes) >= -0.001) {
owed_coin -= dimes;
coin_count += 1;
}

如果您想了解更多关于为什么 float 会出现小错误的信息,请查看:https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems

关于c - While 循环忽略比较两个浮点变量的 bool 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51251821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com