-2

In the below code when I enter input as 0.41 then I get result as 4, which is expected but when I enter input as 0.15 then I get result as 3, but it should have been 2.

I know the reason because my very first if condition is not becoming true, and this has to do with -0.0 and +0.0. I am not able to understand how this all is happening. This whole difference is with input as 0.41 and 0.15.

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

void performMath(float);
int count = 0;

int main(void){
    printf("Enter dollars\n");
    float userInput = FloatInput(); //FloatInput() is just a method to get user input.
    float* ptr = malloc(sizeof(float));
    float cents = modff(userInput, ptr);
    performMath(cents);
}

void performMath(float f1){
        printf("performMath : %f\n", f1);
    if(f1 <= 0.0){  //It fails in this condition when user input is 0.15.
        printf("Change is: %i\n", count);   
    } else{
        if((f1 - 0.25) >= 0.0){
            f1 = f1 - 0.25;
            count++;
        } else if((f1 - 0.10) >= 0.0){
            f1 = f1 - 0.10;
            count++;
        } else if((f1 - 0.05) >= 0.0){
            f1 = f1 - 0.05;
            count++;
        } else { // Even if I user "else if((f1 - 0.01) >= 0.0)" and f1 is 0.01 then also flow doesn't enter this condition.
            f1 = f1 - 0.01;
            count++;
        }
        performMath(f1);
    }   
}
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • 2
    What makes you think the problem is related to negative zero? – John Bollinger Dec 28 '15 at 20:36
  • @JohnBollinger I printf'ed the values and in case of `0.15` I see `0.0000` and first IF doesn't get executed while when I enter `0.41` then I see `-0.000` and first IF gets executed. – hagrawal7777 Dec 28 '15 at 20:38
  • 1
    Please post the full code. There is no `FloatInput()` – Weather Vane Dec 28 '15 at 20:40
  • @WeatherVane: FloatInput() is just a method to get user input. I have edited question to make it clear. – hagrawal7777 Dec 28 '15 at 20:42
  • 2
    How do you know where the error is, or isn't, since you asked a question? Please post the [MCVE](http://stackoverflow.com/help/mcve) – Weather Vane Dec 28 '15 at 20:43
  • `if(f1 <= 0.0)` have you read [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken)? It is better to work in `int cents` than `float dollars`. – Weather Vane Dec 28 '15 at 20:49
  • You could circumvent the questions about `FloatInput()` by either providing a cut-down version (`float FloatInput(void) { float f = 0.0; scanf("%f", &f); return f; }` would do, and testing the return from `scanf()` isn't necessary in this case, though it is sensible and even necessary in general), or by using `scanf()` directly in `main()`. Either would make your code into an MCVE. – Jonathan Leffler Dec 28 '15 at 20:52
  • Your use of `if((f1 - 0.25) > 0.0){` is reminiscent of Fortran II (which is not a compliment). Is there a solid reason for not writing `if (f1 > 0.25)` etc? – Jonathan Leffler Dec 28 '15 at 20:54
  • Your problem is not `+0.0` and `-0.0`. Your problem is small nonzero values that print as “0.000000’ and “-0.000000” because 1) you assume that you can compute decimal fractions exactly with binary floating-point and 2) you assume that if `f1` prints as x.xxxxxx it is equal to or the `double` nearest xxxxxxx/1000000. – Pascal Cuoq Dec 28 '15 at 21:04
  • @WeatherVane I have highlighted where the error is. Please see my latest edits. I cannot work in cents because I am converting `xx.yy` into `0.yy` using `modf`, now if I will try to convert it into int then I will always get `1` or `0`. For example, value as `33.41`. My concern is fractional part. – hagrawal7777 Dec 29 '15 at 22:41
  • @JonathanLeffler You are right, I could write that way, however it wouldn't make any difference or answers my question. – hagrawal7777 Dec 29 '15 at 22:42
  • @RSahu: I tried using float representation but it wouldn't help me. – hagrawal7777 Dec 29 '15 at 22:43
  • @PascalCuoq You are right but that doesn't answer or solve the problem. Even if I use float representation as mentioned in answer of duplicate question, issue wouldn't solve. – hagrawal7777 Dec 29 '15 at 22:44
  • @hagrawal the reason has already been commented. If you change `if(f1 <= 0.0)` to `if(f1 < 0.00001)` which is less than the smallest denomination, the program will work correctly. [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Weather Vane Dec 30 '15 at 09:19

1 Answers1

1

You fail increment count when counting out pennies.

For example:

    } else {
        f1 = f1 - 0.01;
        count++;   // count pennies
    }
dbush
  • 205,898
  • 23
  • 218
  • 273