Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 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

20 Experts available now in Live!

Get 1:1 Help Now