27

In my C++ program, I need to pull a 64 bit float from an external byte sequence. Is there some way to ensure, at compile-time, that doubles are 64 bits? Is there some other type I should use to store the data instead?

Edit: If you're reading this and actually looking for a way to ensure storage in the IEEE 754 format, have a look at Adam Rosenfield's answer below.

Whatsit
  • 10,227
  • 11
  • 42
  • 41
  • Exactly what are you doing? Are you looking for a completely portable way to take eight bytes and interpret them as an IEEE-standard 64-bit floating-point number? – David Thornley Apr 15 '09 at 16:19
  • 2
    @David: Yes, that's exactly what I'm doing. I'd found something somewhere that said C++ floats and doubles were in the IEEE-754 format. I wasn't sure whether doubles always used the same precision and wanted to add a check for it. – Whatsit Apr 15 '09 at 16:37
  • Now I'm not sure that my original information was correct. What's the convention here? Should I delete this question and add another asking about conversion from IEEE-754? – Whatsit Apr 15 '09 at 16:40
  • IIUC: 1) IEEE 754 is not guaranteed by the standard (otherwise, I fail to see the point for std::numeric_limits::is_iec559) 2) is_iec559 == true does not guarantee a binary representation. – Éric Malenfant Apr 15 '09 at 16:53
  • Eric: numeric_limits::is_iec559 exists because numeric_limits is designed to be extensible, and, for example, there might be a numeric_limits specialization for a user-defined BigFloat type for which is_iec559==false. However, you're right that built-in floats might not be IEEE 754. –  Apr 15 '09 at 18:39

8 Answers8

38

In C99, you can just check if the preprocessor symbol __STDC_IEC_559__ is defined. If it is, then you are guaranteed that a double will be an 8-byte value represented with IEEE 754 (also known as IEC 60559) format. See the C99 standard, Annex F. I'm not sure if this symbol is available in C++, though.

#ifndef __STDC_IEC_559__
#error "Requires IEEE 754 floating point!"
#endif

Alternatively, you can check the predefined constants __DBL_DIG__ (should be 15), __DBL_MANT_DIG__ (should be 53), __DBL_MAX_10_EXP__ (should be 308), __DBL_MAX_EXP__ (should be 1024), __DBL_MIN_10_EXP__ (should be -307), and __DBL_MIN_EXP__ (should be -1021). These should be available in all flavors of C and C++.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 1
    This is an even better solution than I'd hoped for. Accepted Evan's answer, though, since it answers the question as it exists. I just asked the wrong question. – Whatsit Apr 15 '09 at 22:37
  • This is inconsistent between compilers and platforms. – lightxbulb Aug 31 '22 at 14:50
15

An improvement on the other answers (which assume a char is 8-bits, the standard does not guarantee this..). Would be like this:

char a[sizeof(double) * CHAR_BIT == 64];

or

BOOST_STATIC_ASSERT(sizeof(double) * CHAR_BIT == 64);

You can find CHAR_BIT defined in <limits.h> or <climits>.

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • 6
    um, no, it does not at all. sizeof returns in units of chars. CHAR_BIT is defined as the number of bits per char. multiply sizeof(x) by CHAR_BIT and you have *exactly* how many bits x is. – Evan Teran Apr 16 '09 at 09:52
  • So for example, on x86 CHAR_BIT is defined as 8. sizeof(double) return's 8. (8 * 8) == 64. – Evan Teran Apr 16 '09 at 09:54
  • Quoting the standard, 5.3.3p1: "The sizeof operator yields the number of bytes in the object representation of its operands." QED –  Apr 16 '09 at 11:24
  • 2
    Also, since CHAR_BIT is inhertied from c, the rules of c apply: to quote the C99 draft standard (section 6.2.6.1p3): "Values stored in objects of any other object type consist of n x CHAR_BIT bits, where n is the size of an object of that type, in bytes." – Evan Teran Apr 16 '09 at 11:50
  • 3
    Finally, from that SAME paragraph you quoted: "sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1;" if sizeof(char) == 1 and sizeof by definition returns in units of bytes. then that means that a char *is* the same size as a byte. However, a byte need not be 8-bits. – Evan Teran Apr 16 '09 at 11:55
  • So yes, your original comment was correct that I assumed that a char is the same size as a byte. But you are incorrect in thinking they are different. The standard clearly shows that they are the same. However, it makes no claims that a byte is 8-bits. – Evan Teran Apr 16 '09 at 12:02
14

Check std::numeric_limits< double >::is_iec559 if you need to know whether your C++ implementation supports standard doubles. This guarantees not only that the total number of bits is 64, but also the size and position of all fields inside the double.

MSalters
  • 173,980
  • 10
  • 155
  • 350
6

I don't think you should focus on the "raw size" of your double (which is generally 80 bit, not 64 bit), but rather on its precision.

Thanks to numeric_limits::digits10 this is fairly easy.

Edouard A.
  • 6,100
  • 26
  • 31
5

The solution without boost is to define the array like so

char a[ 8 == sizeof(double) ];

If the double is not 64 bits then the code will looks like

char a[0];

which is an compile time error. Just put the appropriate comment near this instruction.

Mykola Golubyev
  • 57,943
  • 15
  • 89
  • 102
5

You can use the Boost static assertions to do this. Look at the Use at namespace scope example.

Mihai Limbășan
  • 64,368
  • 4
  • 48
  • 59
1

See this post for a similar problem and a non-boost compile time assertion called CCASSERT.

plinth
  • 48,267
  • 11
  • 78
  • 120
0

For compilers supporting C11 or C++11, I use something similar to @EvanTeran's:

#include <assert.h> // defines macro static_assert in C11 

static_assert(sizeof(double) * CHAR_BIT == 64, "64-bit double is assumed.");

In C++11 or later, static_assert is a keyword.

OTheDev
  • 2,916
  • 2
  • 4
  • 20