0

I have this macro definition in one file (we'll call it file1.c):

#define VAL_NOT_SET -9999.9

In another file, I include file1, then initialize a different variable to VAL_NOT_SET:

decimal value;
value = VAL_NOT_SET;

Later on, I try to compare the two values:

if(value == VAL_NOT_SET){
...
}

But that if statement fails. When I print out the contents of value and VAL_NOT_SET, I get:

value == -9999.900000
VAL_NOT_SET == 759.867067

However, if I set my macro to just -9999, not -9999.9, there is no problem, and VAL_NOT_SET keeps its value.

Also, this is code that I am trying to transition from an AIX machine to Linux, and there was no problem with any of this on AIX. Does anyone have any idea why the macro loses its value, but only when it's a float?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
kschmitt
  • 3
  • 1
  • 3
    There is no way that the macro loses it's value at runtime, because a macro doesn't exist at runtime. I am afraid that you need to explain/post more, for instance what is `decimal`? is it a `typedef`? And do you declare a variable with the `VAL_NOT_SET` name? instead of using the macro defined in the other file? – Iharob Al Asimi Apr 09 '15 at 15:56
  • 1
    Also, you're comparing floating-point types with ==, which is always a bad idea. – Lee Daniel Crocker Apr 09 '15 at 16:06
  • @LeeDanielCrocker: It's bad idea if you are comparing results of calculations; I don't see anything particularly wrong with a `==` comparison here. – M Oehm Apr 09 '15 at 17:44
  • He doesn't show where `value` is coming from. He might be doing something silly like adding 9999.0 to 0.9 and expecting it to equal 9999.9. – Lee Daniel Crocker Apr 09 '15 at 17:46
  • The value comes from `value = VAL_NOT_SET`. (Okay, he doesn't show what happens to that value in between the assignment and comparison, but I guess it isn't touched.) – M Oehm Apr 09 '15 at 17:49
  • How do you print the values? A value of `-9999.9` in `printf`s variaic argument list is a double, a value of `-9999` is an integer. You might get a wrong value, because the variadic arguments are misinterpreted. – M Oehm Apr 09 '15 at 17:54

0 Answers0