Re: real numbers
From: benben (benhongh_at_yahoo.com.au)
Date: 12/18/04
- Next message: Agoston Bejo: "boost::enable_if for class templates"
- Previous message: Bob Milton: "Re: Tracing which .h files are included"
- In reply to: Peter Koch Larsen: "Re: real numbers"
- Next in thread: Filip Konvicka: "Re: real numbers"
- Reply: Filip Konvicka: "Re: real numbers"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 18 Dec 2004 08:32:08 +0800
Thanks for the correction. The example you gave is accessing unknow memory,
of course baring the risk to access violation. I was not aware of the
alignment issues till your posts, to be honest. But I was indeed assuming
that "a sequence conforming IEEE floating point rules" is created and
located with a float somewhere in the program. :)
Thanks
ben
>
> "benben" <benhongh@yahoo.com.au> skrev i en meddelelse
> news:%23AOXiQ%234EHA.1188@tk2msftngp13.phx.gbl...
> > desn't seem to me incorrect in my tests
> >
> > i used the followings:
> >
> > float original = 3.1415;
> > void* pBitSequence = reinterpret_cast<void*>(&original);
> > float realNumber = *(reinterpret_cast<float*>(pBitSequence));
> >
> > assert (original == realNumber);
> >
> > ben
> As Filip said, your test is most unconvincing. Try:
>
> char buf[1 + sizeof(float)];
> float original = 3.1415;
> // copy to possibly unaligned storage
> memcpy(buf + 1,&original,sizeof(float));
> float crash = *(reinterpret_cast<float*>(buf + 1));
>
> The last line has the potential to crash and/or give incorrect result on
> some architectures (including Intel CPU's).
>
> /Peter
>
> >
> >>
> >> "benben" <benhongh@yahoo.com.au> skrev i en meddelelse
> >> news:OkTzIT14EHA.1292@TK2MSFTNGP10.phx.gbl...
> >> > assuming your bit sequence conforms IEEE floating point rules and has
> > the
> >> > same size as a float in C/C++ you can do:
> >> >
> >> > void* pBitSequence = ...; // pointer to your bit sequence
> >> > float realNumber = *(reinterpret_cast<float*>(pBitSequence));
> >>
> >> This is simply not correct as there might be alignment problems.
> >>
> >> /Peter
> >> >
> >> > benben
> [snip]
>
>
- Next message: Agoston Bejo: "boost::enable_if for class templates"
- Previous message: Bob Milton: "Re: Tracing which .h files are included"
- In reply to: Peter Koch Larsen: "Re: real numbers"
- Next in thread: Filip Konvicka: "Re: real numbers"
- Reply: Filip Konvicka: "Re: real numbers"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|