Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double_t in C99

Tags:

double

c99

I just read that C99 has double_t which should be at least as wide as double. Does this imply that it gives more precision digits after the decimal place? More than the usual 15 digits for double?.

Secondly, how to use it: Is only including

#include <float.h> 

enough? I read that one has to set the FLT_EVAL_METHOD to 2 for long double. How to do this? As I work with numerical methods, I would like maximum precision without using an arbitrary precision library.

Thanks a lot...

like image 460
yCalleecharan Avatar asked Apr 16 '10 18:04

yCalleecharan


2 Answers

No. double_t is at least as wide as double; i.e., it might be the same as double. Footnote 190 in the C99 standard makes the intent clear:

The types float_t and double_t are intended to be the implementation’s most efficient types at least as wide as float and double, respectively.

As Michael Burr noted, you can't set FLT_EVAL_METHOD.

If you want the widest floating-point type on any system available using only C99, use long double. Just be aware that on some platforms it will be the same as double (and could even be the same as float).

Also, if you "work with numerical methods", you should be aware that for many (most even) numerical methods, the approximation error of the method is vastly larger than the rounding error of double precision, so there's often no benefit to using wider types. Exceptions exist, of course. What type of numerical methods are you working on, specifically?

Edit: seriously, either (a) just use long double and call it a day or (b) take a few weeks to learn about how floating-point is actually implemented on the platforms that you're targeting, and what the actual accuracy requirements are for the algorithms that you're implementing.

like image 125
Stephen Canon Avatar answered Oct 17 '22 07:10

Stephen Canon


Note that you don't get to set FLT_EVAL_METHOD - it is set by the compiler's headers to let you determine how the library does certain things with floating point.

If your code is very sensitive to exactly how floating point operations are performed, you can use the value of that macro to conditionally compile code to handle those differences that might be important to you.

So for example, in general you know that double_t will be at least a double in all cases. If you want your code to do something different if double_t is a long double then your code can test if FLT_EVAL_METHOD == 2 and act accordingly.

Note that if FLT_EVAL_METHOD is something other than 0, 1, or 2 you'll need to look at the compiler's documentation to know exactly what type double_t is.

like image 25
Michael Burr Avatar answered Oct 17 '22 09:10

Michael Burr