Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-11-14
3
Medium Priority
?
119 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 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 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 1000 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

704 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