how to detect if long double is of extended precision or not at compile time
Asked Answered
C

4

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?

Cotillion answered 5/1, 2012 at 22:56 Comment(5)
You could try sizeof(double) > 8. Although not portable either, it'll probably still work in most cases.Bridgettbridgette
Can't you test sizeof(double) < sizeof(long double) or am I missing something?Bucolic
The preprocessor doesn't recognize sizeof.Anastos
Just curious, how are you going to use this information?Anastos
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.Cotillion
D
7

You could compare DBL_MANT_DIG and LDBL_MANT_DIG from float.h.

Damiendamietta answered 5/1, 2012 at 23:0 Comment(0)
B
2

You can test e.g.

#if DBL_MANT_DIG < LDBL_MANT_DIG

or similar values defined in float.h

Bemuse answered 5/1, 2012 at 23:1 Comment(0)
I
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.

Irrawaddy answered 8/1, 2012 at 20:9 Comment(0)
D
-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).

Disproportion answered 25/2, 2022 at 17:50 Comment(3)
...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)./Knobkerrie
True. I'd put it in the first unit test. Or in a test program run from CMake.Disproportion
@Knobkerrie Edited my answer to reflect your objection. Thanks.Disproportion

© 2022 - 2024 — McMap. All rights reserved.