Solved

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

Posted on 2014-11-14
3
115 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 69

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 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