0

UPDATE1

So after converting "+49.984367875E-230" to double and converting back to string, how can i print it as "4.998436788E-229" ?

As i have to convert "+49.984367875E-230" to double to do comparison.


I got a problem when converting string to double and double to string. For example:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char a[40]={"+49.984367875E-230"};
    double d=atof(a);//d=4.9984367874999999e-229
    char b[40],c[40];
    sprintf(b,"%16.9E",d);
    grisu2(c,"%16.9E",d);//implementation of grisu2 algorithm
    printf("sprintf:%s\ngrisu2:%s\n",b,c);
    return 0;
}

The result like this:

sprintf:4.998436787E-229
grisu2:4.998436788E-229

So, why the stdlib's atof and sprintf got the wrong result? Since grisu2 is right, so i guess atof is good, but sprintf is wrong?

As i have grisu2, **so i want a faster and accurate atof, I have searched the Internet and stackoverflow, but ** this fast_atof is not good.

I want something like grisu2, is there any paper or good implementation?

user1024
  • 982
  • 4
  • 13
  • 26
  • 1
    see [this](http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) tell you why not accurate. And C++ STL has std::stod function. – Ron Tang Mar 12 '15 at 05:43
  • see is this one gives better result http://stackoverflow.com/questions/2302969/how-to-implement-char-ftoafloat-num-without-sprintf-library-function-i – Barmak Shemirani Mar 12 '15 at 05:45
  • 3
    Because `d` can't represent the value in `a` perfectly, it ends up with a slightly lesser value. It's quite legitimate for `sprintf()` to round based on the exact value in `d`, while some other library may try to guess whether it's a representation of a larger number that would round differently for display. Neither `atof` nor `sprintf` are broken - your expectations are unreasonable. In another circumstance, you might equally be complaining that `grisu2` incorrectly rounds up from `...49999...` – Tony Delroy Mar 12 '15 at 05:47
  • You do realize that the difference is something like 2 parts in 10 billion? Or a measurement error of 2 grams in something weighing 10,000 tonnes, right? That's 2 cubic centimetres in a body of water 100 meters wide, 100 meters long and 1 meter high. – enhzflep Mar 12 '15 at 06:41
  • So after converting "+49.984367875E-230" to double and converting back to string, how can i print it as "4.998436788E-229" ? As i have to convert "+49.984367875E-230" to double to do comparison. – user1024 Mar 12 '15 at 09:29
  • http://stackoverflow.com/questions/588004/is-floating-point-math-broken?rq=1 why do you need to print out the string to compare? Just do it in double. And note that comparison in floating-point types also need extra care. To print the number out with 16 digits of precision, use `%22.16f`, as you need more characters for the dot, minus sign and E – phuclv Mar 12 '15 at 09:42
  • Well, "+49.984367875E-230" is stored as string, i have to first read it, and convert it to double, and then do comparison, then write it to text file as "4.998436788E-229", how can i do? – user1024 Mar 12 '15 at 12:07

0 Answers0