tags:

views:

147

answers:

5

I was wondering, how long in number of characters would the longest a double printed using fprintf be? My guess is wrong.

Thanks in advance.

+7  A: 

Twelve would be a bit of an underestimate. On my machine, the following results in a 317 character long string:

#include <limits>
#include <cstdio>
#include <cstring>

int main() 
{
    double d = -std::numeric_limits<double>::max();
    char str[2048] = "";

    std::sprintf(str, "%f", d);

    std::size_t length = std::strlen(str);
}

Using %e results in a 14 character long string.

James McNellis
Note that this does *not* reflect the precision in the double value itself.
Seth Johnson
At *least* use `snprintf`.
strager
@strager: Well, while I'd never, ever use or recommend use of `sprintf` like this in production code, for a quick example... eh...
James McNellis
A: 

you can decide it by yourself..

double a=1.1111111111111111111111111111111111111111111111111;
printf("%1.15lf\n", a);  
return 0;

./a.out
1.111111111111111

you can print more than 12 characters..

Frost
+6  A: 

Who knows. The Standard doesn't say how many digits of precision a double provides other than saying it (3.9.1.8) "provides at least as much precision as float," so you don't really know how many characters you'll need to sprintf an arbitrary value. Even if you did know how many digits your implementation provided, there's still the question of exponential formatting, etc.

But there's a MUCH bigger question here. Why the heck would you care? I'm guessing it's because you're trying to write something like this:

double d = ...;
int MAGIC_NUMBER = ...;
char buffer[MAGIC_NUMBER];
sprintf(buffer, "%f", d);

This is a bad way to do this, precisely because you don't know how big MAGIC_NUMBER should be. You can pick something that should be big enough, like 14 or 128k, but then the number you picked is arbitrary, not based on anything but a guess that it will be big enough. Numbers like MAGIC_NUMBER are, not suprisingly, called Magic Numbers. Stay away from them. They will make you cry one day.

Instead, there's a lot of ways to do this string formatting without having to care about buffer sizes, digits of precision, etc, that let you just get on with the buisness of programming. Streams is one:

#include <sstream>

double d = ...;
stringstream ss;
ss << d;
string s = ss.str();
cout << s;

...Boost.Format is another:

#include <boost\format\format.hpp>

double d = ... ;
string s = (boost::format("%1%") % d).str();
cout << s;
John Dibling
Actually, if you're using `%f`, it doesn't matter what the actual precision of the `double` is, because `%f` will always round it down to whatever precision you specified in the formatting string (default is 6 if omitted). So you always know the number of digits after the decimal point. And those before it? You can always use `FLT_MAX` (or `numeric_limits`) for that.
Pavel Minaev
+2  A: 

Its defined in limits:

 std::cout << std::numeric_limits<double>::digits << "\n";
 std::cout << std::numeric_limits<double>::digits10 << "\n";

Definition:

digits:    number of digits (in radix base) in the mantissa
           Equivalent to FLT_MANT_DIG, DBL_MANT_DIG or LDBL_MANT_DIG.
digits10:  Number of digits (in decimal base) that can be represented without change.
           Equivalent to FLT_DIG, DBL_DIG or LDBL_DIG for floating types.

See: http://www.cplusplus.com/reference/std/limits/numeric_limits/

Of course when you print stuff to a stream you can use the stream manipulators to limit the size of the output.

Martin York
Note that `digits10` is the number of digits _of precision_. A number such as `1.23e+100` only has 3 digits of precision, but if printed in full without exponential notation, would have 101 digits in the output. Of course, the question doesn't specify the format in which the number is to be printed... but then if it's exponential, then you also have to account for variable-size exponent.
Pavel Minaev
A: 

If your machine uses IEEE754 doubles (which is fairly widespread now), then the binary precision is 53 bits; The decimal equivalent is approximately 15.95 (calculated via logarithmic conversion), so you can usually rely on 15 decimal digits of precision.

Consult Double precision floating-point format for a brief discussion.

For a much more in-depth study, the canonical paper is What Every Computer Scientist Should Know About Floating-Point Arithmetic. It gets cited here whenever binary floating point discussions pop up, and is worth a weekend of careful reading.

Don Wakefield