ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm wondering if I can get help here. I need to implement the following function in C.
Code:
double stringToDouble(const char *str);
The parameter, str, can be assumed to contain either a valid floating point number (e.g. 0.25, -3.44, etc) or one of the "non-number" entities; i.e. "Inf", "+Inf", "-Inf" or "NaN". The return value shall contain the converted value.
It's a piece of cake with GCC, which has atof() that does exactly that. For instance, atof("Inf") returns the double value equivalent of Inf (positive infinity). The problem is, as usual, Visual C++, where atof("Inf") returns 0.0.
Here is my rough draft. The question is, how can I assign values like +/-Inf and NaN to a double variable?
They are not included in <math.h> for Visual Studio .NET. Do you suggest that I take those values off of the GCC <math.h> and stick them into the Windows code? I could try that and see what happens.
[Update] Looks like I can use HUGE_VAL and -HUGE_VAL with Visual Studio .NET to represent +/-Inf. Now I need to figure out what can be used for NaN.
Do you get an error or did you just not find the #defines?
For NAN, you can use 0.0/0.0 (not sure about that one).
There are a good few things which are missing from windows math.h and has caused me problems before in doing cross platform code. I did have a link(can't see to find it at the mo) which shows the parts of c and c++ standard which are not in some headers.
Do you get an error or did you just not find the #defines?
For NAN, you can use 0.0/0.0 (not sure about that one).
I just didn't find the defined macros. I searched both vc7/include and vc7/PlatformSDK/include, but to no avail.
Thanks for the suggestion on NaN. I will see if that works. If not, we may need to resort to a readme.txt saying you cannot specify NaN for float/double data due to the OS limitation.
Thanks for the info. I actually found the macros that defined those values inside vc7/PlatformSDK/include/AlphaOps.h. However, if I throw in the varible that holds, say, "+INF double", to _fpclass(), it does not return _FPCLASS_PINF as it should. Also, if I try to print out the content of the varible, it will print out what looks like a valid double value instead of +Inf.
Well, if you do this:
double d = 5;
you will expect d to be 5.0, won't you? Instead try
Code:
unsigned long long raw = 0x7ff0000000000000;
double d = *( double* )&raw
By the way, I just noticed that the negative values are not different from the corresponding positive ones. This a mistake; change the '7' in the negative values to 'f'.
Well, if you do this:
double d = 5;
you will expect d to be 5.0, won't you? Instead try
Code:
unsigned long long raw = 0x7ff0000000000000;
double d = *( double* )&raw
By the way, I just noticed that the negative values are not different from the corresponding positive ones. This a mistake; change the '7' in the negative values to 'f'.
DOH! My bad... thanks for the help. I've implemented +/-Inf and NaN that way and the code works on all platforms now!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.