Posts

Showing posts from May, 2019

Tricky Situation: Atof function converting strings to inf or nan

Image
This blog post says about a tricky situation when using atof function. The atof function converts a string argument to a floating point number. If the string to be converted starts with "inf" or "nan", the atof function converts the string to inf/nan instead of converting to 0. inf represents "Positive Infinity" and nan represents a value which is "Not a Number". For eg: The following strings will convert to inf when using atof function. Information -> inf Infection -> inf Influenza -> inf etc... The following strings will convert to nan when using atof function. Nanotechnology -> nan Nanogram -> nan etc... This issue can cause downstream impacts if you are using the converted values to be saved to database or other application. The following C++ program shows the above issue: This issue can be corrected by using a simple which checks if the string starts with inf and if so