0

for greedy I'm kinda confused. this is what i have in main.

int VAL;
int quart;
int dime;
int nick;
int penny;
printf("O hai! How much change is owed?\n");
float x = GetFloat();
while (x<0)
{
    printf("How much change is owed?\n");
    x = GetFloat();
}
VAL = 100*(x);
quart = VAL/25;
VAL = VAL-(25*quart);
dime = VAL/10;
VAL = VAL-(10*dime);
nick = VAL/5;
VAL = VAL-(5*nick);
penny = VAL;
VAL = penny+nick+dime+quart;
printf("%i\n",VAL);

on line 17 for every value inputted(x) I got the correct value(VAL) for example even if I inputted 3.3492 I'll got VAL == 334 but for some reson if I input 4.2 as x I somehow got VAL = 419 how is that possible? I've tried printf my x and I got 4.20000016 so how can 4.2000016*100=419?

2

That's a good question. Respectfully, your result of 4.20000016 is highly suspicious to me because in every environment I've seen this program tested in, 4.2 is always stored as something slightly smaller. In the IDE, it's being stored as 4.199999809265. Given that stored value, when you multiply by 100 and store in an int, the decimal portion is always truncated, resulting in 419. If, instead, you apply a rounding function after multiplying by 100, the problem goes away.

I would be interested to know what environment you are running in and the exact code that is storing something slightly larger than 4.2, or whether it is an anomaly related to the exact code that you are using to print that value.

IF this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

[EDIT: additional comments]

It's been suggested that using a double instead of a float would solve the problem. Not really. In some cases, it would work, but there's no guarantee. It would just be dependent on whether the number was stored as slightly larger or slightly smaller than the actual number. For proof, look at how 4.2 and 4.1 are stored. Here's a small program that will demonstrate the issue.

#include <stdio.h>

int main(void)
{
    double y = 4.2;
    float x = 4.2;
    printf("Default printout:  float x = %f, double y = %f\n", x, y);
    printf("High precision printout:\n    float x = %1.60f\n   double y = %1.60f\n\n\n", x, y);

    y = 4.1;
    x = 4.1;
    printf("Default printout:  float x = %f, double y = %f\n", x, y);
    printf("High precision printout:\n    float x = %1.60f\n   double y = %1.60f\n", x, y);
}

I'll leave it to the reader to run the program and see the result. ;-)

2
  • turns out I missed something when checking it was 4.200000 and 16 which is another value. I checked again and got about 4.1999. Thanks anyway :) Jan 20 '16 at 14:22
  • Similar Problem. I multiply by 100 and store in an int c, then I round as follows double round(double c); but the problem still occurs. I believe that I am not using the rounded number properly. My question is, how do I use a double as I use an int? Feb 13 '16 at 5:38
0

This problem occurs due to the floating precisions. Using double data type rather than float could fix the problem.

2
  • 1
    Actually, no. The interesting thing is that while it would produce a correct result for 4.20, it would fail for 4.10. The underlying problem would still exist. See my edited answer. But I applaud trying to explore something that might possible work.
    – Cliff B
    Jan 27 '17 at 3:53
  • @CliffB Thx for your answer. You're right.
    – shuaibird
    Jan 27 '17 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .