I have a method that needs to do a different thing when given an unset float than a float with the value of 0. Basically, I need to check whether or not a variable has been, counting it as set if it has a value of 0.
So, what placeholder should I use as an unset value (nil, NULL, NO, etc) and how can test to see if a variable is unset without returning true for a value of 0?
You can initialize your floats to NaN (e.g. by calling
nan()ornanf()) and then test withisnan()if they have been changed to hold a number. (Note that testingmyvalue == nan()will not work.)This is both rather simple (you will probably include
math.hin any case) and conceptually sensible: Any value that is not set to a number is “not a number”…