Solved

Single/Double type mixing: Is this unbelievable VB bug or what?

Posted on 2014-11-14
3
110 Views
Last Modified: 2014-11-28
I have found that the following example code, produces very unexpected result

Private Sub Command1_Click()
    Dim Sng As Single
    Dim Dbl As Double

    Sng = 1.1
    Dbl = sng
   
    Debug.Print Dbl
End Sub

The result was 1.10000002384186

Now, I’m of course aware of floating point data tape limitations, and that when working with, for instance double precision variables, some fluctuating error at the 1.100000000000xy positions might be expected, since the floating types in general case can only be approximately represented.

However, this behavior in example is unexpected and strange to me. Why would assigning a Single value do a Double introduce such enormous error? This can have particularly dire consequences, as in a case that happened to me: you work extensively with Doubles, in order to increase the precision of calculations, and then, at only one place, you use a Single for a variable, and multiplies a Double, and ruin all pression calculation you’ve done so far!

I’ve checked, and this happens in Vb6, VB.Net, QuickBasic. I suspect, this might be just the way processors handles Singles/Doubles, in visual basic or other languages?
Why? Why in the previous example, the result for Dbl is not simply 1.1 or at least 1.100000000000xy

It seems as if processor/compiler/whatever for 8 bytes of Dbl, assigns for first 4 bytes the 4 bytes from single, and gets 1.1000000, and then for the rests, it uses the values it found in memory in the following 4 bytes…
0
Comment
Question by:npaun
3 Comments
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
Comment Utility
The explanation is in https://randomascii.wordpress.com/2012/06/26/doubles-are-not-floats-so-dont-compare-them/ .
1.1 cannot be represented exactly in binary, as you probably know. The added precision for double cannot be used if comming from float (single precision), and so the error is maximized instead of minimized. If converting from single to double, you just see that error without any possible compensation.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
and then for the rests, it uses the values it found in memory in the following 4 bytes…
It assuredly does *not* do that  = )
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 250 total points
Comment Utility
If you really want to understand floating point numbers at the machine level, check out these two Wiki articles:
Single Precision Floating Numbers
Double Precision Floating Numbers

As Qlemo points out, when you placed a value in Sng, you didn't place 1.1 in there, you placed (approximately) 1.10000002 into Sng.  

When you copied that value to Dbl, at the machine level, the computer simply added zeros to the fraction (and modified the exponent to keep it at the same value, but with more digits).

As a hex value, your Sng was 0x3f8ccccd.
As a hex value, your Dbl was 0x3ff199990a000000

As bits, broke down into the sign | exponent | fraction
0 | 01111111 | 00011001100110011001101
0 | 01111111111 | 0001100110011001100110100000000000000000000000000000

So the computer did not simply take what ever values were randomly located in memory.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now