Re: real numbers

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: benben (benhongh_at_yahoo.com.au)
Date: 12/18/04


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]
>
>



Relevant Pages

  • Re: real numbers
    ... > of course baring the risk to access violation. ... I don't see any unknown memory access in Peter's example. ... > alignment issues till your posts, ...
    (microsoft.public.vc.language)
  • Re: Not Entirely OT: Grammar
    ... inconsiderate or illiterate. ... You run that risk only from pedants and the anal-compulsive. ... I'd say the use of quotes for all but the last one is incorrect. ...
    (comp.sys.mac.system)
  • Re: PPM for Stourbridge Branch
    ... So are you saying that the statement by Ross Hamilton in an earlier ... Stourbridge Town Car. ... Full Stop" is incorrect? ... playing field on rules and regulations and are very much based on a risk ...
    (uk.railway)
  • Re: OOA to the Internet
    ... Exchange always sees it as a new sender. ... we are at risk for mail looping. ... >That is incorrect, its once per sender. ... >Exchange MVP ...
    (microsoft.public.exchange.admin)
  • Re: Weired DeviceIoControl Behavior...Can anyone Explain?
    ... First of all, when you work with dumps of structures, make sure that your ... Incorrect: ... are again padding between this field and the StartingOffset field; ... OS returns this structure with the default eight byte alignment, ...
    (microsoft.public.win32.programmer.kernel)