My original code for this problem is as follows:
int main(void)
{
float c = 0; //total coins
float $ = get_float("Change: ");
while($ <= 0.0099999 && $ >= 0.00000001)
{
$ = get_float("Change: ");
}
while($ <= -0.00000001)
{
$ = get_float("Change: ");
}
while($ == 0)
{
printf("0");
}
float updated_$ = $;
while(updated_$ >= 0 || updated_$ >= -0.25)
{
if ($ >= 0.25 && updated_$ >= 0.25) {{
updated_$=updated_$ - 0.25;}
c++;
}
else if($ >= 0.10 && updated_$ >= 0.10) {{
updated_$=updated_$ - 0.10;}
c++;
}
else if($ >= 0.05 && updated_$ >= 0.05) {{
updated_$=updated_$ - 0.05;}
c++;<p>
}
else if($ >= 0.01 && updated_$ >= 0.01) {{
updated_$=updated_$ - 0.01;}
c++;
}
else if(updated_$ >= 0.0098 && updated_$ <= 0.01) {{
updated_$=updated_$ - 0.098;}
c++;
}
else if(updated_$ < 0.0098) {
printf("%f\n", c );
return 0;
}
}
}
}
This program is returning correct values for every number I tested so far except for 4.2, which it incorrectly computes as 23 coins instead of 18. When I run debug50, I can see that the input 4.2 is changed to something like 4.19999998 for variable $ while the program is running. In fact, for any number entered, $ is changed to some similar imprecise value, hence why I added certain parts to the code such as
else if(updated_$ >= 0.0098 && updated_$ <= 0.01) {{
updated_$=updated_$ - 0.098;}
c++;
I have searched around this site for various lines of code that would help me round the value to the nearest hundredth decimal place during the actual computation, and so far the codes I have found and tried to insert only round the result of the computation, giving me the same incorrect answer for 4.2 of 23, and when I run debug50,I can still see that the incorrect value of 4.19999..something is being used for the computation itself. I have tried to use roundf as follows:
int main(void)
{
{
float c = 0; //total coins
float $ = get_float("Change: ");
float $r = roundf($ * 100) / 100;
while($ <= 0.0099999 && $ >= 0.00000001)
{
$ = get_float("Change: ");
}
while($ <= -0.00000001)
{
$ = get_float("Change: ");
}
while($ == 0)
{
printf("0");
}
float updated_$ = $r;
while(updated_$ >= 0 || updated_$ >= -0.25)
{
if ($r >= 0.25 && updated_$ >= 0.25) {{
updated_$=updated_$ - 0.25;}
c++;
}
else if($r >= 0.10 && updated_$ >= 0.10) {{
updated_$=updated_$ - 0.10;}
c++;
}
else if($r >= 0.05 && updated_$ >= 0.05) {{
updated_$=updated_$ - 0.05;}
c++;
}
else if($r >= 0.01 && updated_$ >= 0.01) {{
updated_$=updated_$ - 0.01;}
c++;
}
else if(updated_$ >= 0.0098 && updated_$ <= 0.01) {{
updated_$=updated_$ - 0.098;}
c++;
}
else if(updated_$ < 0.0098) {
printf("%f\n", c );
return 0;
}
}
}
}
$r is still being changed to 4.1999... during the computation instead of 4.2 as I had hoped, and then the printf result is rounded instead. I also tried inserting
for(double a=120;a<=130;a+=1.0);
into my code and again had the same problem of the $ value being converted to an imprecise value once inserted and only the printf final result being rounded. Is there a way to round the value of $ for the computation itself, so that I will see it as 4.2 instead of 4.1999...etc when I run debug50?(ie, all the computations will be run with 4.2 exactly as entered, instead of being changed to 4.1999...) Or maybe there is another way around this? Perhaps I inserted roundf into the program in such a way that it didn't work as desired?