Author Topic: char* to long double (non-destructivly)?  (Read 2321 times)

anphanax

  • Member
  • **
  • Posts: 197
  • Kudos: 11
    • http://june.tripod.com
char* to long double (non-destructivly)?
« on: 23 October 2005, 04:59 »
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

  • Member
  • **
  • Posts: 518
  • Kudos: 416
Re: char* to long double (non-destructivly)?
« Reply #1 on: 23 October 2005, 15:14 »
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

  • Member
  • **
  • Posts: 197
  • Kudos: 11
    • http://june.tripod.com
Re: char* to long double (non-destructivly)?
« Reply #2 on: 23 October 2005, 22:42 »
Thanks. sscanf for some reason wasn't in my thinking -_-.

Pathos

  • Member
  • **
  • Posts: 518
  • Kudos: 416
Re: char* to long double (non-destructivly)?
« Reply #3 on: 24 October 2005, 11:53 »
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

  • Member
  • **
  • Posts: 197
  • Kudos: 11
    • http://june.tripod.com
Re: char* to long double (non-destructivly)?
« Reply #4 on: 25 October 2005, 02:58 »
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);

Pathos

  • Member
  • **
  • Posts: 518
  • Kudos: 416
Re: char* to long double (non-destructivly)?
« Reply #5 on: 26 October 2005, 10:35 »
I Must Know
















:)

anphanax

  • Member
  • **
  • Posts: 197
  • Kudos: 11
    • http://june.tripod.com
Re: char* to long double (non-destructivly)?
« Reply #6 on: 5 November 2005, 03:29 »
I was writing some code because I was curious, there :|. I'm still kinda new to C++.

I didn't know about the long double to double truncation that might occur, thanks.