Solved

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

Posted on 2014-11-14
3
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 40442572
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 75

Expert Comment

by:käµfm³d 👽
ID: 40442611
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
ID: 40442788
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GUID's in SQL Server 4 31
sql server connection string in config file 4 41
MessageBox Appear behind a form in Runtime 1 31
vb.net code 9 28
A short article about problems I had with the new location API and permissions in Marshmallow
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 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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

734 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