Re: Singles to Doubles
- From: "Rick Rothstein \(MVP - VB\)" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx>
- Date: Mon, 16 Jul 2007 16:54:50 -0400
Here is my guess... and it is only a GUESS as I have never been one to "get under the hood" of VB. I'm guessing that VB calculates its Singles and Doubles using some kind of power series expansion (I'm guessing that is what is at the heart of FFT which I think is what is used)... for Singles, it stops iterating the series after X number of loops whereas for Doubles it continues iterating past X until Y loops have completed. This would explain why Doubles are more accurate than Singles (more iterations of the series expansion). Now, to continue my guessing, I think 15 (maybe 16) digits were calculated for the Single and stored; but, because VB knows it is a Single, it only reports 6 of 7 digits when asked. My further guess is that CDbl simply reveals the underlying 15 or 16 digits that were generated when the Single was calculated; hence, a not so accurate value. Now, for the CStr conversion, I think VB is grabbing only the 6 or 7 digits it would normally use for display (I'm thinking the same "text engine" is underneath the displayer as the string converter because, well, text is text, so why should they be different). These 6 or 7 digits are not burdened by the inaccurate excess digits that Single calculated, and so are more nearly equal to the original inputted value. Now, that nearly pure number is being fed into the CDbl series expansion engine (just like if you typed it in) producing a near perfect conversion. Anyway, that is my guess at what is going on.
Rick
"Karl E. Peterson" <karl@xxxxxxxx> wrote in message news:uldbmN%23xHHA.4652@xxxxxxxxxxxxxxxxxxxxxxx
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
--
.NET: It's About Trust!
http://vfred.mvps.org
.
- Follow-Ups:
- Re: Singles to Doubles
- From: dpb
- Re: Singles to Doubles
- References:
- Singles to Doubles
- From: Karl E. Peterson
- Re: Singles to Doubles
- From: Bob Butler
- Re: Singles to Doubles
- From: Karl E. Peterson
- Singles to Doubles
- Prev by Date: Re: Singles to Doubles
- Next by Date: Re: VB6 - why isnt a straight Win32 DLL offered as a project type?
- Previous by thread: Re: Singles to Doubles
- Next by thread: Re: Singles to Doubles
- Index(es):
Relevant Pages
|
Loading