CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Check if a given 8 byte is a valid double

1. Member
Join Date
Nov 2010
Posts
54

## Check if a given 8 byte is a valid double

Hi,

Is there a function in C/C++ that can check if a given 8-byte data block is a valid double value in the valid range?

Thanks

Alan

2. Banned
Join Date
Aug 2013
Posts
55

## Re: Check if a given 8 byte is a valid double

You could check that the 8 bytes (held in a double) is neither std::subnormal, std::infinite nor std::NaN.

Then it should represent some number in the "numeric" range.
Last edited by zizz; May 29th, 2014 at 11:13 PM.

3. Senior Member
Join Date
Oct 2008
Posts
1,456

## Re: Check if a given 8 byte is a valid double

denormals are in "the valid range" so why exclude them ? moreover, infinities and nans are also "valid" in a sense and given that the OP asked for converting an "8-byte data block" to a double, I think he's probably dealing with something like a (portable?) double binary de/serialization problem, that is a different story ... ( hence, byte ordering, binary rep, etc... should be taken into account in this case ... )

4. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: Check if a given 8 byte is a valid double

1) floating points don't have byte ordering issues, the byte ordering is a specific trait of the type.
2) 8byte floating points (double) don't have denormals since there is an implied 1 bit in the mantissa, hence it's always normalized. (the same is true for 4byte float)
This is assuming the IEEE 8byte float representation.

to the OP: I guess it depends of your definition of "valid double value" and "in the valid range".

5. Banned
Join Date
Aug 2013
Posts
55

## Re: Check if a given 8 byte is a valid double

My comprehension is that any 64-bit bitpattern is a valid double really.

But the OP wanted to know if a certain bitpattern counted as a valid "number" and in that case the tests I suggested would assure that.

6. Senior Member
Join Date
Oct 2008
Posts
1,456

## Re: Check if a given 8 byte is a valid double

Originally Posted by OReubens
1) floating points don't have byte ordering issues, the byte ordering is a specific trait of the type.
what do you mean ? note that the OP asked for testing an arbitrary 8-byte chunck of which we don't know where it comes from ...

Originally Posted by OReubens
2) 8byte floating points (double) don't have denormals since there is an implied 1 bit in the mantissa, hence it's always normalized. (the same is true for 4byte float)
This is assuming the IEEE 8byte float representation.
I also don't get this. the point of my post was not to claim that denormals existed or not assuming a binary representation, but whether they do represent a valid numrical value or not.

7. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: Check if a given 8 byte is a valid double

Originally Posted by zizz
My comprehension is that any 64-bit bitpattern is a valid double really.

But the OP wanted to know if a certain bitpattern counted as a valid "number" and in that case the tests I suggested would assure that.
there are invalid 64bit patterns for a double. (not a number or NAN's)

a 64bit floating point variable may be a double, it might be a custom floating point type that is not the same as a double and has different layout. Hence why I refered to IEEE format.

8. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: Check if a given 8 byte is a valid double

Originally Posted by superbonzo
what do you mean ? note that the OP asked for testing an arbitrary 8-byte chunck of which we don't know where it comes from ...
if it's an IEEE type, then the byte ordering is inate of the type, it doesn't matter if it's on a intel machine or a Motorola 68000
only integers have "endianness".

I also don't get this. the point of my post was not to claim that denormals existed or not assuming a binary representation, but whether they do represent a valid numrical value or not.
you mentioned denormals
I mentioned denormals can't happen in a double.
nothing more.

denormals may be valid or not, depending on your definition of "valid".
allowing denormals means there may be multiple representations of the same value, that may be "invalid" for some tests.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•