0

After tinkering with the syntax and perusing the over blog related to floating number imprecision I thought I had cracked the nut with my attempt to round my numbers. But when I enter 0.41 in my terminal I get a runtime error instead of the the 4 coins it should take to get to 41p.

Is my syntax/formula correct for rounding floats correct?

//Prompt user for an amount of change
do
{
    dollars = get_float("change owed: ");
}
while (dollars < 0);

//convert float (dollars) to integer (cents) and then round
int cents = round(dollars * 100);

int coins = 0;
while (cents >= 25)
{
    coins++;
    cents-= 25;
}

while (cents >= 10)
{
    coins++;
    cents -=10;
}

while (cents >= 5)
{
    coins++;
    cents +=5;
}

while (cents >= 1)
{
    coins++;
    cents -=1;
}

printf("%i\n", coins);

}

i am getting this error message - runtime error: signed integer overflow: 2147483646 + 5 cannot be represented in type 'int' 429496731

nickel
  • 1
  • 2
  • 1
    Exactly what "run time error" do you get? Show more code, too. What data type is `dollars`? How are you reading it from the input? Did you confirm that you read it correctly (*e.g.*, by printing it back out with `printf`)? Did you look up the manual page for `round` and did you know that it takes `double` argument and it returns a `double`? – lurker Jun 19 '18 at 16:39
  • 2
    This code can't trigger runtime error as long as `round` is the standard `round` function.. – Eugene Sh. Jun 19 '18 at 16:42
  • 2
    Please post a [mcve]. – R Sahu Jun 19 '18 at 16:44
  • thanks for responding. – nickel Jun 19 '18 at 16:52
  • Please [edit](https://stackoverflow.com/posts/50933139/edit) your question and add this information. – Eugene Sh. Jun 19 '18 at 16:54
  • What is the exact error message you are getting? I still don't see anything that can cause a runtime error. – Eugene Sh. Jun 19 '18 at 16:59
  • 1
    Don't use floating point types to represent money. Integral types are better suited for this. https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency#3730040 – Christian Gibbons Jun 19 '18 at 17:00
  • @ChristianGibbons Using integer types for money is a good starting point for learners yet not so great once interest/tax rates, mill rates, compounding, overflow detections and commodity definitions like 3 for-a-dollar come in. All forms of data representing money have their pitfalls and benefits. Integers types are not **the** answer. – chux - Reinstate Monica Jun 19 '18 at 18:14

1 Answers1

3

Change

cents +=5;

to

cents -=5;

That said - don't use float for a problem like this. Read the input as a string and convert directly to an int or unsigned int

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Why would it cause any runtime error? – Eugene Sh. Jun 19 '18 at 17:05
  • 2
    @EugeneSh. Because the loop will continue until there is a overflow. I assume OP has some special system for detecting that. See last lines of the question. In any case - as `cents` is an `int` it will end with UB. The loop is intended to **decrement** - not **increment** – Support Ukraine Jun 19 '18 at 17:07
  • it was a simple notation error. Yes, i have been acquainting myself on the floating point numbers - exponents and mantissa's. thanks everybody! – nickel Jun 19 '18 at 17:08
  • 1
    I wouldn't necessarily use an unsigned type for currency. Being able to represent debt would be reasonable. – Christian Gibbons Jun 19 '18 at 17:12
  • 1
    @nickel this is a good catch. But a division (number of coins) and a modulus (money remaining) is more efficient than repeated subtraction - especially if a large amount was entered. The final loop for cents does not need to be a `while` because that many cents are all that remains. – Weather Vane Jun 19 '18 at 19:24
  • ... such as the input `20000000` dollars, `2000000000` cents , which needs `80000000` subtractions to find the number of 25c coins as opposed to a single `2000000000 / 25` calculation. – Weather Vane Jun 19 '18 at 19:31