views:

483

answers:

7

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.

+4  A: 

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
a byte isn't guaranteed to be 8-bits
Evan Teran
j_random_hacker
What about the case when the size of the double is 16 byte, but the byte consist of 4 bits?
Mykola Golubyev
Evan Teran
or better yet "sizeof(double) * CHAR_BIT == 64"
Evan Teran
Mykola, a byte can never consist of 4 bits. it has to have at least 8 bits.
Johannes Schaub - litb
ok. I put this only as example to the j_random_hacker proposal
Mykola Golubyev
@Evan: I agree, "sizeof(double) * CHAR_BIT == 64" is a better solution (e.g. it handles Mykola's quirky 16 * 4 case) as well as being clearer.
j_random_hacker
+4  A: 

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

Mihai Limbășan
+6  A: 

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.
80 bit doubles are an Intel-ism, and true only if the FPU is actually in 80-bit mode (there's also 64-bit mode)...
DevSolar
I know, but nevertheless...
Edouard A.
And even 32-bit mode depending on how you initialize Direct3D.
Cecil Has a Name
+5  A: 

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
This incorrectly assumes that a char is as big as a byte.
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
So for example, on x86 CHAR_BIT is defined as 8. sizeof(double) return's 8. (8 * 8) == 64.
Evan Teran
Quoting the standard, 5.3.3p1: "The sizeof operator yields the number of bytes in the object representation of its operands." QED
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
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
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
I stand corrected.
A: 

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

plinth
+5  A: 

In C99, you can hust 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++.

Adam Rosenfield
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
+1  A: 

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