初学者问题:浮点运算的精度...... [英] Beginner question: Precision of floating point arithmetic...

查看:62
本文介绍了初学者问题:浮点运算的精度......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我道歉,因为我确信这可能已经在以前很难...

但是我正在练习实用C编程 ;而且由于浮动问题,b
点算术并且我正在寻找解决问题的方法,因此我无法让它完美地工作。看下面的代码

...

给定一定的变化(低于1.00美元),程序会告诉你每个有多少硬币你需要得到这个数额。

计划是正在工作的因为逻辑看起来是正确的,并且它对某些数字有效,但对于其他数字,它不是。问题出现

就像我看到的那样是0.01,没有在内存中表现出来。

我在线搜索了一下,我确信这是一个常见的

问题,但我似乎无法找到解决方法...


感谢您的帮助...


Shawn


#include< stdio.h>


int main()

{

char输入[100];

float total_input,running_total;

int quarters,nickels,dimes,pennies;


/ *将所有变量清零* /

quarters = nickels = dimes = pennies = 0;

total_input = running_total = 0;


/ *获取必要的更改金额* /

printf(输入总金额,低于$ 1.00:);

fgets(输入,sizeof(输入),stdin);

sscanf(输入,"%f",& total_input);


/ *循环直到我们得到一个理智的金额* /

而(total_input> = 1.00 || total_input< = 0.00){

printf(总金额不在$ 0.00和$ 1.00.\ n之间);

printf("输入总金额改变,小于

$ 1.00:");

fgets(输入,sizeof(输入),stdin);

sscanf(输入, "%f"& total_input);

}


/ *存储在另一个变量中,以便我们可以使用它* /

running_total = total_input;


while(running_total> = 0.25){

++ quarters;

running_total - = 0.25;

}

而(running_total> = 0.10){

++角钱;

running_total - = 0.10;

}

而(running_total> = 0.05){

++镍币;

running_total - = 0.05;

}

while(running_total> = 0.01){

++便士;

running_total - = 0.01;

}


printf("为了得到$%。2f更改,你需要:\ n" ,

tot al_input);

printf("%d quarters,\ n",quarters);

printf("%d dimes,\ n",dimes) ;

printf("%d nickels,\ n",nickels);

printf("%d pennies\\\
",pennies);


返回(0);

}

解决方案

1.00)程序将告诉

你需要多少钱来获得这个金额。

计划是正在工作的因为逻辑看起来是正确的,并且它对某些数字有效,但对于其他数字,它不是。问题出现

就像我看到的那样是0.01,没有在内存中表现出来。

我在线搜索了一下,我确信这是一个常见的

问题,但我似乎无法找到解决方法...


感谢您的帮助...


Shawn


#include< stdio.h>


int main()

{

char输入[100];

float total_input,running_total;

int quarters,nickels,dimes,pennies;


/ *将所有变量清零* /

quarters = nickels = dimes = pennies = 0;

total_input = running_total = 0;


/ *获取必要的更改金额* /

printf("输入总变化量,小于


1.00:");

fgets(输入,sizeof(输入),stdin);

sscanf(输入,"%f",& total_input );


/ *循环直到我们得到一个合理的金额* /

而(total_input> = 1.00 || total_input< = 0.00){

printf("总计不在


0.00和

之间<​​/ p>

Hello all,
I apologize as I am sure this has probably been dealth with before...
but I am doing an exercise from "Practical C Programming" and I have
been unable to get it to work perfectly due to problems with floating
point arithmetic and I am looking for a way to solve it. See the code
below...
Given a certain amount of change (below $1.00) the program will tell
you how many of each coin you will need to get that amount. The
program is "working" in that the logic appears correct and it DOES
work for some numbers, but for others, it is not. The problem appears
to be that 0.01 as I see it, is not being represented in memory.
I have done some searching online and I am sure this is a common
problem but I just can''t seem to find the workaround...

Thank you for all your help...

Shawn

#include <stdio.h>

int main()
{
char input[100];
float total_input, running_total;
int quarters, nickels, dimes, pennies;

/* Zero out all the variables */
quarters = nickels = dimes = pennies = 0;
total_input = running_total = 0;

/* Get the necessary amount of change */
printf("Enter the total amount of change, less than $1.00: ");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &total_input);

/* Loop until we get a sane amount */
while (total_input >= 1.00 || total_input <= 0.00) {
printf("Total is not between $0.00 and $1.00.\n");
printf("Enter the total amount of change, less than
$1.00: ");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &total_input);
}

/* Store in another variable so we can use it */
running_total = total_input;

while (running_total >= 0.25) {
++quarters;
running_total -= 0.25;
}
while (running_total >= 0.10) {
++dimes;
running_total -= 0.10;
}
while (running_total >= 0.05) {
++nickels;
running_total -= 0.05;
}
while (running_total >= 0.01) {
++pennies;
running_total -= 0.01;
}

printf("In order to get $%.2f in change, you will need:\n",
total_input);
printf("%d quarters,\n", quarters);
printf("%d dimes,\n", dimes);
printf("%d nickels,\n", nickels);
printf("%d pennies\n", pennies);

return(0);
}

解决方案

1.00) the program will tell
you how many of each coin you will need to get that amount. The
program is "working" in that the logic appears correct and it DOES
work for some numbers, but for others, it is not. The problem appears
to be that 0.01 as I see it, is not being represented in memory.
I have done some searching online and I am sure this is a common
problem but I just can''t seem to find the workaround...

Thank you for all your help...

Shawn

#include <stdio.h>

int main()
{
char input[100];
float total_input, running_total;
int quarters, nickels, dimes, pennies;

/* Zero out all the variables */
quarters = nickels = dimes = pennies = 0;
total_input = running_total = 0;

/* Get the necessary amount of change */
printf("Enter the total amount of change, less than


1.00: ");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &total_input);

/* Loop until we get a sane amount */
while (total_input >= 1.00 || total_input <= 0.00) {
printf("Total is not between


0.00 and


这篇关于初学者问题:浮点运算的精度......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆