Re: converting single to integer

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



David Kerber wrote:
In article <fcop1j$bin$2@xxxxxxxx>, none@xxxxxxx says...
David Kerber wrote:
In article <OitxKgW#HHA.980@xxxxxxxxxxxxxxxxxxxx>, rickNOSPAMnews@xxxxxxxxxxxxxxxxx says...
...

What is the difference between storing a Single in an Integer and "casting" a Single to an Integer... isn't the end result the same... an Integer?
They're both integers, but have different values.
If by "different value" you mean a differing internal representation for the same numerical value...

Are you sure about that? I don't know the internal representation of the various types of numbers, but if you take the bit pattern:

00011010 11100001 00001111 10101010

and interpret it as a Single (assuming it represents a valid Single), I sincerely doubt it would return the same value as that bit pattern interpreted as an Integer, which is what you are doing when you use memcopy to copy from a single to an integer. Or am I completely misintrepreting the line of this discussion? If you use a Cast, then you will get the same value, but a different internal bit pattern.

I think we're then talking past each other...yes, the internal bit pattern isn't the same between the same integer stored in Single as floating point as the same integer stored as a Long -- which is what I said (and I gather what you meant although it seemed unclear to me as written previously).

In IEEE-754 floating point, not all bit patterns are legal as floating point representations so some valid integers aren't valid floats. And, since there are only 23 bits reserved for the mantissa plus a "hidden" bit, any integer requiring over that many bits for representation will lose precision if cast to a Single. For a readable description of floating point in general and the IEEE standard, see

http://docs.sun.com/source/806-3568/ncg_goldberg.html

--




--


.



Relevant Pages

  • Re: Singles to Doubles
    ... If you're saying an internal representation of 1.11 should be displayed as 1.1100 externally under all circumstances, that can't be unless it is one of the set of reals than can be represented exactly. ... If otoh it can't be represented exactly as a Single, then the likelihood that it can in Double isn't zero, but the problem of translation to/from the external representation arises, as does that of promotion. ... google on "floating point conversion" or similar and amongst the zillion of hits pretty early on there will be sites that have floating point calculators that you can type numbers into and see their internal representations. ...
    (microsoft.public.vb.general.discussion)
  • Re: Trap representations
    ... The most likely symptom is some kind of machine exception (fault, trap, ... valid floating point numbers. ... to contain the trap representation when no undefined behavior ... invalid bit pattern into b. ...
    (comp.lang.c)
  • Re: Rfd: floating point truncation V1
    ... Round r1 to an integral value using the "round towards zero" rule, ... an internal representation of floating point negative zero, "-0E", which differs from the representation of floating point positive zero, "0E", should return r2 as floating point negative zero for r1 in the open interval. ...
    (comp.lang.forth)
  • Re: invalid floating point value
    ... >> I'm not aware of any bit pattern that wouldn't represent a valid value for ... >> on systems with no floating point hardware support. ... and an old ICL machine that had no ... Dan Pop ...
    (comp.lang.c)
  • Re: How many float values in range 0f - 1f inclusive?
    ... or a bit pattern outside the range I ... gave that corresponds to a floating point number in 0f through 1f. ... In Java float is 32 bites. ...
    (comp.lang.java.help)