This question already has answers here:
Closed 2 years ago.
Is floating point math broken?
(31个答案)
嗨,所以我在努力找出我的代码有什么问题。我真的是新手,所以请容忍我。评论可能有点混乱。
当我在Visual Studio上运行这段代码时,我得到了某些输入的正确输出,比如8.68,但是当我通过PuTTY为我的教授提交这个文件时,它会为我进行测试,并且在扣除一角钱后,我得到了一个不正确的余额(少了一分钱)。当我输入一个76.54这样的值时,扣除疯子后我就少了一分钱。输入一个76.76的值,我得到了正确的输出。我不知道到底发生了什么。我的教授看了看我的代码,说是因为类型的改变?建议我改用这个方法,例如
不过,我不知道如何使用这种方法。
编辑:我需要使用模数运算符和铸型来完成这个任务。当我提交油灰时使用gcc。
(31个答案)
嗨,所以我在努力找出我的代码有什么问题。我真的是新手,所以请容忍我。评论可能有点混乱。
#include <stdio.h>
int main(void)
{
double cost, gstCost, newCost; // initial cost, cost of gst by itself, new cost after gst
int loonies, quarters, dimes, nickels, pennies; // used for quantity of coins
float loonreq, quartreq, dimereq, nickreq, penreq; // cost required after deduction of the corresponding coin
printf("Please enter the amount to be paid: $");
scanf("%lf", &cost); // scanf allows to input an amount as a double ("%lf")
gstCost = cost * .13 + .005; // .13 for gst and add 0.005 to round up or down
printf("GST: %.2lf\n", gstCost);
newCost = cost + gstCost;
printf("Balance owing: $%.2lf\n", newCost); // original cost + gst cost
loonies = newCost; // loonies will output as integer when cost has decimals
printf("Loonies required: %d", loonies);
if (loonies == 0) // == used to compare equality
loonies = 1; // if loonies = 0, loonreq will be infinite causing code to crash and stop
loonreq = (float)((int)(100 * newCost) % (100 * loonies)) / 100; // casting int and * 100 on float values for modulo since it can only calculate for integer value
printf(", balance owing $%.2f\n", loonreq); // %.2f shows a float with 2 decimal places
quarters = 100 * loonreq / 25; // 100*loonreq allows the code to find how many quarters by dividing by 25
printf("Quarters required: %d", quarters);
if (quarters == 0)
quarters = 1;
quartreq = (float)((int)(100 * loonreq) % (int)(100 * (quarters * .25))) / 100;
printf(", balance owing $%.2f\n", quartreq);
dimes = 100 * quartreq / 10;
printf("Dimes required: %d", dimes);
if (dimes == 0)
dimes = 1;
dimereq = (float)((int)(100 * quartreq) % (int)(100 * (dimes * .10))) / 100;
printf(", balance owing $%.2f\n", dimereq);
nickels = 100 * dimereq / 5;
printf("Nickels required: %d", nickels);
if (nickels == 0)
nickels = 1;
nickreq = (float)((int)(100 * dimereq) % (int)(100 * (nickels * .05))) / 100;
printf(", balance owing $%.2f\n", nickreq);
pennies = 100 * nickreq / 1;
printf("Pennies required: %d", pennies);
if (pennies == 0)
pennies = 1;
penreq = (float)((int)(100 * nickreq) % (int)(100 * (pennies * .01))) / 100;
printf(", balance owing $%.2f\n", penreq);
return 0;
}
当我在Visual Studio上运行这段代码时,我得到了某些输入的正确输出,比如8.68,但是当我通过PuTTY为我的教授提交这个文件时,它会为我进行测试,并且在扣除一角钱后,我得到了一个不正确的余额(少了一分钱)。当我输入一个76.54这样的值时,扣除疯子后我就少了一分钱。输入一个76.76的值,我得到了正确的输出。我不知道到底发生了什么。我的教授看了看我的代码,说是因为类型的改变?建议我改用这个方法,例如
dimes = (balance / 100) / 0.1;
balance = (int)(balance) % 10;
不过,我不知道如何使用这种方法。
编辑:我需要使用模数运算符和铸型来完成这个任务。当我提交油灰时使用gcc。
最佳答案
舍入逻辑没有给出正确的答案。假设我们有一个输入123。商品及服务税应为15.99。但是你的代码会给出16.00。若要修复此问题,请尝试将gstCost = cost * .13 + .005;
替换为gstCost = (round(cost * .13 * 100) / 100);
关于c - 一分钱一分货,是编程新手吗?在C中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46438216/
10-15 14:56