My function returns a float value from an object. If the function cannot find a proper float value an error should be returned so i can deal with it properly in the code.
My question is how to return an error.
The option of adding an extra parameter and using it to set an error marker is not preferred.
I could return a magic value, is that a valid option? My float values in the program never exceed very large numbers ( never over 10^12) so returning an FLT_MAX to check an error might be an option.
Is there a better ( portable ) way?
If you #include <math.h>, you can return NAN in case of error. This is the only float f for which f == f is FALSE.
Returning a magic value is the only option, except for setting a global (which is even worse).
The best solution would be to modify the prototype, to something like:
bool findAFloat(float *result);
That is clear, easy to use, and rather obvious.
You could also go for:
float findAFloat(float ifNotFound);
and have the caller decide what to return if no value is found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With