Miscellaneous > Programming & Networking
char* to long double (non-destructivly)?
anphanax:
atof() from cstdlib returns double, not long double, hence my problem.
I thought about this:
(long double)atof(cstr)
But I don't like that, because atof returns double (I need long double, and casting a double to a long double isn't a damn solution, since I might end up loosing some decimal points in my original long double). It bothers me I can use sprintf to place a long double in a string, yet I can't seem to do the opposite. Perhaps there's some trick i'm not aware of.
Looked on various sites, and didn't see anything about solving this problem :|.
Pathos:
some versions of the standard libraries actually have equivalent long double functions (although its not standard) so browse that.
char buf[100]= "3.141512394872394876293847293487923847";
long double ld;
sscanf(buf, "%Lf", ld);
maybe...not sure about the %Lf bit. Why do you need such high accuracy? Doubles are very accurate but there are many numbers long double still can't represent anyway (0.3). I dislike using long doubles because you never know if the're gonna be truncated or not, any other functions you use will probably only accept doubles anyway.
anphanax:
Thanks. sscanf for some reason wasn't in my thinking -_-.
Pathos:
I wouldn't be surprised if it just computed a double value and discarded the rest tho.
What do you need an long double for?
anphanax:
Why do people need to know why I wanna do something? I just do :P.
Honestly, I just wanted to know how to do it. In the process of all of this, I discovered typeinfo.h and typeid(), which i'm quite happy about :).
Amazingly simple to do too:
long double ldVar; sscanf(szInput, "%fL", &ldVar);
long double ldVar; swscanf(wszInput, L"%fL", &ldVar);
Navigation
[0] Message Index
[#] Next page
Go to full version