Re: Singles to Doubles



Karl E. Peterson wrote:
Hi Bob --

What the heck? Seems silly to have to convert it to a String
first, then to a Double. But, it is more accurate. Anyone know of
a less ugly way to accurately replicate a Single with a Double, or
this the best it gets?

Isn't it mainly that the Single isn't representing the value as
accurately?

I guess, yeah. But how is it that a *String* seems to do so even
more accurately?
For discussion purposes, I modified your example just a bit:

Public Sub Main()
Dim s As Single
Dim d1 As Double
Dim d2 As Double
s = 0.3026!
d1 = s
d2 = CDbl(CStr(s))
Debug.Print s, d1, d2
Debug.Print s = d1, s = d2
Debug.Print s - d1, s - d2
End Sub

Which gives me these results:

0.3026 0.302599996328354 0.3026
True True
0 -3.67164609826887E-09

And, of course, the value I'm after *is* 0.3026 -- not the one that's
off
0.00000000367... Seems like some sort of register garbage that I'm
picking up in
the conversion?

Thanks... Karl


The best representation of a value as a single can use all 24 bits of mantissa, and still not be exact. The double representation gets a lot closer, using 53 bits of mantissa. But there's no way to guess what those new bits are supposed to be.

During a conversion from single to double, all the extra bits of mantissa are (must be) set to 0. That makes for a significantly different representation than the best one for your value. Notice that the error term is down in the range of 10^-8, which is close to 2^-24, where the break to all zeros occurs.

In the string domain the value is exact, and can be converted to a double using all 53 bits of precision.

So yes, you get the best result if you pass through a domain where the value is exact. In this case you could also have transitioned through a Currency value and gotten the same good results.

--
Jim Mack
MicroDexterity Inc
www.microdexterity.com
.



Relevant Pages

  • Re: The spinoza papers: design of the extra-precision number object 2
    ... doesnt terminate, which cause practical problems, e.g in representation ... that we have an exact number. ... Limiting the precision doesn't change this claim. ... including quantum uncertainty represented as ...
    (comp.programming)
  • Re: Why cant I xor strings?
    ... a representation that does have parts and order etc. ... It's a matter of interpretation. ... I said they do represent _exact_ values, ... >who operates with "floats represent a value and have an error range around ...
    (comp.lang.python)
  • Re: Converting from Microsoft Binary Format floats to Python Float
    ... >floating point numbers, In a previous thread, Bengt Richter posted ... >that for one of the examples, the conversion isn't exact. ... not seeing a full representation of the two values. ...
    (comp.lang.python)
  • Re: Messagebox displaying without being called
    ... This is AFAIK the exact text you'll have if you ... Dim o As Object=dt.DefaultView' Get the first DataRowView ... formatting happens) but for now I would say rather user code as what you ... exception is thrown. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: ifstream bug in VS2005 Beta2 & Platform SDK
    ... So 3/25 and 7/40 are exact. ... Just about all languages use IEEE as the representation. ... What you have is a binary approximation, ... But converting the other way is inexact. ...
    (microsoft.public.vc.stl)