0

This is a simple problem of calculating the min number of coins needed to give the change, given a N value. The division 0.04/0.01 gives 3, why?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int MinQtdCoins(float N, float coins[], int qtdCoins)
{
    int i, qtdMinCoins = 0;
    int numCoins=0;
    for(i=0; i<qtdCoins; i++)
    {
        if(N > coins[i] || fabs(N - coins[i]) < 0.0000000001)  // N >= coins[i]
        {
            numCoins = (int)(N/coins[i]);
            printf("Number of Coins: %f divided by %f = %d, ",N,coins[i],numCoins);
            qtdMinCoins += numCoins;
            N = N - numCoins*coins[i];
            printf("remains: %f\n",N);

        }
    }
    return qtdMinCoins;
}

int main()
{
    int qtdCoins = 5;
    float coins[] = {0.50, 0.25, 0.10, 0.05, 0.01};
    float N=9.79;

    printf("\n\n%d\n",MinQtdCoins(N, coins, qtdCoins));
    return 0;
}
Roni Castro
  • 1,968
  • 21
  • 40
  • At a guess I'd say you're performing a floating point division that gives an answer close to, but less than 4, and truncating it to an integer to get 3. Try multiplying your target and the values of your coins by 100 so that the entire calculation can be done in integers –  Sep 29 '14 at 02:35
  • Related, if not duplicate: http://stackoverflow.com/questions/24665459/best-practices-for-floating-point-arithmetics – R Sahu Sep 29 '14 at 03:11

2 Answers2

5

The division 0.04/0.01 gives 3, why?

numCoins = (int)(N/coins[i]);

Casting to int just truncates fractional part. So if 0.04/0.01 == 3.999.. (due to rounding), the result is 3.

Community
  • 1
  • 1
AlexD
  • 32,156
  • 3
  • 71
  • 65
  • Don't use an integer? – Martin James Sep 29 '14 at 02:48
  • 1
    @MartinJames: No, don't use floating point to represent money. Use integer numbers of cents. – R.. GitHub STOP HELPING ICE Sep 29 '14 at 02:49
  • If you want to round to the nearest `int`, you may try `(N / coins[i] + 0.5)`. (Assuming `N` and `coins[i]` are positive.)[This is just an answer to the question "how to round", I didn't check the logic of the algorithm. Regarding the algorithm itself, see the advice of @R.] – AlexD Sep 29 '14 at 02:55
  • What do you mean by "Use integer numbers of cents"? represent everything as integers, even the N(total value to be divided)? – Roni Castro Sep 29 '14 at 03:12
  • This is a question to @R., but I think this is exactly what was meant. Such kind of tasks ask for precise number representation. – AlexD Sep 29 '14 at 03:23
  • 1
    @RoniCastro: Yes. All your numbers are exact whole numbers of cents. On the other hand, 0.01 and 0.04 are not floating point values. When the compiler translates the program, it converts them to the closest representable floating point values. FYI, use of floating point is generally forbidden in the financial industry, presumably since innocent-looking floating point errors can be used to embezzle huge amounts of money (when dealing with the scale of numbers they're working with). – R.. GitHub STOP HELPING ICE Sep 29 '14 at 15:24
  • @RoniCastro I am guessing you are looking for the %f format for printf – dvhh Sep 30 '14 at 07:35
  • @dvhh Which `printf` do you mean? As far as I see, `int`s are printed with `%d` and floats with `%f`. – AlexD Sep 30 '14 at 19:13
0

you are doing floating point division and keeping the value in an integer...because of this value is truncated to 3

µtex
  • 900
  • 5
  • 12