Re: Singles to Doubles

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



Michael C wrote:
"dpb" <none@xxxxxxx> wrote in message news:f7k4fe$vv8$1@xxxxxxxxxxx
If, after all this you still want more, do a google on "What Every Computer Scientist...Floating Point" and you'll be led quickly to a very fine paper on all the details of floating point. I thought I had a bookmark, but can't seem to find it at present and I'm plumb tuckered out, sorry... :)

What you say makes sense although I'm still trying to get my head fully around it. A binary number such as 1.11 should be converted to 1.1100 as these 2 values are equal. *If* we assume the number came from a base 10 number then we could provide some optimisation to the binary conversion to make some sort of guess as to what information was lost converting it to binary. But what we have is a binary number which could have been obtained from any source and might not have been a base 10 number at some stage. Therefore to assume base 10 could be faulty.

I'm confused by your nomenclature but let me see if I can paraphrase and add and get us closer...

A "binary number 1.11 should be converted to 1.1100 as they are equal".

If you're saying an internal representation of 1.11 (decimal) 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 (and I didn't check for sure). In that case it _will_ be preserved both in i/o and in promotion.

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. The VB i/o rtl is in a way a culprit in masking these problems because Print (and friends) _do_ incorporate logic in them to try their darnedest to make "ugly" values "pretty" because, just as in Karl's case, that's what is often wanted. Consequently, when the "problem" does appear, as this thread has amply demonstrated, it is more confusing than it might otherwise be in a more rigorous (in some sense) language such as C or Fortran where "what you see is what you get" makes the internal representations a little more visible.

The part about assuming whether the number came from base 10 or was possibly from somewhere else I don't understand at all. In the context of VB, all external representations are base 10 unless explicitly coded as Hex$ or Oct$ in which case the base is also known and explicit. There is no assumption of base ever required, the only "assumptions" in what I was trying to describe had to do with what one is to assume trailing digits are for added precision when there isn't any information
providing them.

I'm repeating myself, but that's the situation w/ promotion or translation. In both cases the course of action that is followed is to extend w/ zeros, but that leads to two different results, depending on whether one is extending zeros in the external or internal representation.

To see what is going on internally, find Tony's post and paste his code in and look at the actual bit patterns and it will probably become much more clear. Or, 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.

Again, I heartily recommend at least reading through Goldberg's paper even if you don't study it in depth...

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

--
.



Relevant Pages

  • Re: Singles to Doubles
    ... number then we could provide some optimisation to the binary conversion ... If you're saying an internal representation of 1.11 should be ... the external representation arises, ... whether one is extending zeros in the external or internal representation. ...
    (microsoft.public.vb.general.discussion)
  • 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: identifiers and modules
    ... "XML is touted as an external format for representing data. ... If one converts from an internal representation to the external representation and back again, the new internal representation should equal the old. ... It is not always self-describing, since the internal format corresponding to an external XML description depends crucially on the XML Schema that is used for validation (for instance, to tell whether data is an integer or a string). ...
    (comp.lang.scheme)
  • Re: Floating point difficulties... help
    ... The root of this problem is that a conversion needs to be done from base-10 floating point notation to the internal representation which is base-2. ... Any time you convert from/to a Double or Float you will run into this conversion error so you will need to make sure such float usage is 100% eliminated from your code. ... If you decide to use FixedPoints be aware that they are somewhat nasty: their internal value can be different from how they display. ...
    (comp.lang.smalltalk)
  • Re: converting single to integer
    ... If by "different value" you mean a differing internal representation for the same numerical value... ... and interpret it as a 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. ... 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, 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. ...
    (microsoft.public.vb.general.discussion)