views:

232

answers:

4

In C++ you can use std::numeric_limits<FloatingPointType>::digits10 to find out exactly how many places of precision you can get before the decimal place. How do I find out how many decimal places I can get following it, if I know the number will always be fixed-point between 0 and 1 inclusive?

Edit: The reason I ask is that I have an external RNG generating doubles and piping them through to a second program using standard input and output, so if the randomly generated numbers aren't output correctly to standard output, the second program can't grab their full values through standard input because they get truncated by the output of the first program, then rounded to representable values by the second program. I'd like to pipe the values between programs fully intact. Thanks

+4  A: 

The maximum number of significant digits you can have is given by std::numeric_limits<...>::digits. This is typically 24 and 53 for IEEE floats and doubles, respectively.

John Feminella
+4  A: 
sth
A: 

The C++ standard doesn't guarantee you anything. If you have to strictly control the number of places after the decimal point, you have to write your own code to do so (fixed-format output for instance). Ultimately it depends on the compiler you are using. You would get better luck by using C and printf's formatting controls if you need control over the number of decimal places.

Russ Auld
A: 

If you will always need a certain number of digits you'd be well advised to look into boost. For example the format library, or the numeric conversion library. If you haven't already, you also should read the Goldberg article about floating point numbers

Liz Albin