8

On few systems double is same as long double. How can I detect if long double is of extended precision than double at compile time and use it to conditional compile.

I see there are predefined macros present in libgcc SIZEOF_DOUBLE and SIZEOF_LONG_DOUBLE But there are not portable across different toolchains.

Is there C way to do this?

5
  • You could try sizeof(double) > 8. Although not portable either, it'll probably still work in most cases. Commented Jan 5, 2012 at 22:58
  • Can't you test sizeof(double) < sizeof(long double) or am I missing something? Commented Jan 5, 2012 at 22:58
  • 3
    The preprocessor doesn't recognize sizeof. Commented Jan 5, 2012 at 23:00
  • 1
    Just curious, how are you going to use this information? Commented Jan 5, 2012 at 23:03
  • I'm implementing few double precision functions, to verify them I would need long double but long double is not always of extended precision. If long double is same as double I will use mpfr libray for reference. Commented Jan 10, 2012 at 12:45

4 Answers 4

8

You could compare DBL_MANT_DIG and LDBL_MANT_DIG from float.h.

Sign up to request clarification or add additional context in comments.

Comments

2

You can test e.g.

#if DBL_MANT_DIG < LDBL_MANT_DIG

or similar values defined in float.h

Comments

0

The "correct" solution to this problem (as used by many projects) is to create a configure script.

The configure script runs various tests that include compiling and running small programs to determine compiler and system properties. The script then writes out it's findings as a header file, or a makefile, or both. Of course, yours can do anything you like.

There are tools some tools to do this sort of thing semi-automatically, but they're probably overkill for you. If you'd like to take a look the names are autoconf and automake. Beware, they're not simple to learn, but they generate configure scripts and makefiles that should work on just about any platform as long as it has a unix-style shell, and GNU make.

Comments

-1

Why do you want long double? For precision. So go straight to the core of the issue and test for precision, specified by EPSILON:

#include <float.h>
printf("Info: long double epsilon = %10.4Lg\n", LDBL_EPSILON);
if (LDBL_EPSILON > 1.2e-19) {
    printf("Insufficient precision of long double type\n");
    return 1;
}

This is a test at run time though, not at configure or compile time.

Put it either in a unit test, or in a little test program to be run from CMake (as proposed in the answer by ams).

3 Comments

...at compile time and if (LDBL_EPSILON > 1.2e-19) is a runtime expression and you can't compare floats at compile time (in preprocessor)./
True. I'd put it in the first unit test. Or in a test program run from CMake.
@KamilCuk Edited my answer to reflect your objection. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.