can NAN be assigned?

H

hurcan solter

consider the simplifed code fragment;

enum Endianness
{
BigEndian, ///<BigEndian
LittleEndian,///< LittleEndian
#if defined(BOOST_BIG_ENDIAN)
HostEndianness = BigEndian ///< Current host's Endiannnes.
#elif defined(BOOST_LITTLE_ENDIAN)
HostEndianness = LittleEndian
#else
#error we only supports big and little endian systems.
#endif
};
class ByteWrapper{
public:
template<typename T>
void put(T& t)
{
verify(sizeof t);
memcpy(&_buffer[_pos],reinterpret_cast< unsigned char*>(&t),sizeof
t);
_pos+=sizeof t;


}
template<typename T>
T get()
{
verify(sizeof(T));
T retval;
memcpy(&retval,&_buffer[_pos],sizeof(T));
_pos+=sizeof(T);
return retval;

}
private:
vector<unsigned char> _buffer;
size_t _pos;

};
template<typename T,Endianness Endian=LittleEndian>
class HLAgeneric
{
public:
void encode(ByteWrapper& byteWrapper)
{
if (HostEndianness!=Endian)
{
BYTESWAP(_value);
byteWrapper.put<T>(_value);
BYTESWAP(_value);
return;

}
byteWrapper.put<T>(_value);

}
void decode(ByteWrapper& byteWrapper)
{
_value=byteWrapper.get<T>();
if (HostEndianness!=Endian)
{
BYTESWAP(_value);
}

}
private:
T _value;
};
Well, Bytewrapper class is responsible for extracting and inserting
data from a network buffer, the thing is
for some floating point values, the data extracted from the buffer
could be NAN on the host machine(1.QNAN on my platform) because of the
Endianness.HLAgeneric can sort out the Endianness issue.However when i
get<T>() from ByteWrapper the memory representation of the returned
value changes.(it only changes in one bit but that's enough for
malfunction). I think it undergoes to some conversion, but i expected
it to be copied bitwise since the types are the same. What is the
expected behaviour here? Can I assign NAN to another variable ? if
not, how can i approach the problem?. I thought of specializing get
and creating retval on the heap but it won't fit or look good with the
rest of design.

Any pointers would be greatly appreciated...
 
C

Carl Barron

hurcan solter said:
Well, Bytewrapper class is responsible for extracting and inserting
data from a network buffer, the thing is
for some floating point values, the data extracted from the buffer
could be NAN on the host machine(1.QNAN on my platform) because of the
Endianness.HLAgeneric can sort out the Endianness issue.However when i
get<T>() from ByteWrapper the memory representation of the returned
value changes.(it only changes in one bit but that's enough for
malfunction). I think it undergoes to some conversion, but i expected
it to be copied bitwise since the types are the same. What is the
expected behaviour here? Can I assign NAN to another variable ? if
not, how can i approach the problem?. I thought of specializing get
and creating retval on the heap but it won't fit or look good with the
rest of design.

Any pointers would be greatly appreciated...
My first suggestion is the format provided by C99's %[aA] format
specifier, a lossless hexidecimal representation of the floating point
number. If you require the client to write in this format and you can
read it then the problem of endianness, non IEE754 format etc are
eliminated at the source.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top