
May 14th, 2018, 11:58 AM
#1
Thread Starter
Junior Member
Single and Double precision
I know everybody knows the limits of the Single and Double data, and the precision of each of them, 7 characteristic numbers for the Single and 15 characteristic numbers for the Double
That means for example the following numbers
1.1111111111111111111111111111111
if stored in a Single will become 1.11111 > Precision 0.000001
if stored in a Double will become 1.1111111111111 > Precision 0.00000000000001
111111.111111111111111111111111111
if stored in a Single will become 111111.1 > Precision 0.1
if stored in a Double will become 111111.111111111 > Precision 0.000000001
The thing is I did not find a code that with a given number it returns to me the precision of this number to estimate numerical errors of my algorithms. I know it is quite a easy and "stupid" algorithm but it may help somebody in the future
Code:
' calculate the log base 10
Public Function Log10(ByVal ival As Double) As Double
Log10 = Log(ival) / (Log(10))
End Function
Private Function SinglePrecision(iSng As Single) As Single
SinglePrecision = 10 ^ (Int(Log10(iSng))  6)
End Function
Private Function DoublePrecision(iDbl As Double) As Double
DoublePrecision = 10 ^ (Int(Log10(iDbl))  14)
End Function
That little code will solve problems like
Code:
Sub Test()
Dim a As Single, b As Single
a = 1.1111112
b = 1.1111111
Debug.Print a ' 1.111111
Debug.Print b ' 1.111111
Debug.Print a = b ' False
Debug.Print Abs((a  b)) < SinglePrecision(a) ' True
End Sub
The a=b returns False but the precision of the Single is not able to difference them and in some cases that can lead to errors. In that cases comparing the rest with the precision should solve the problem
HTH
Nico

May 14th, 2018, 05:50 PM
#2
Re: Single and Double precision
Hi Nico,
Thanks for the functions. They're a nice idea, and absolutely have the possibility of solving some problems. I suppose I see only two caveats when we're using them (and neither is a showstopper).
The first, we should realize that, when dealing with IEEE numbers (either Singles or Doubles), computers are always "thinking" in binary. However, they typically report to us in base10, as you've shown. And, in this binarytobase10 conversion, there are all kinds of traps. Your example (printing 1.111111 for 1.1111112) is a great example. However, if we have no intention of reporting in base10 (which is probably rare), we must recognize that the precision is somewhat greater than your functions are saying it is. That's why your Debug.Print a = b line says they're different. They actually are different in base2. But like I said, not a showstopper.
The second problem I see may be a bit more serious for some. When we get down near the limits (in the smallness direction, positive or negative, sign doesn't matter), your functions will quit working. Specifically, when we get down into IEEE subnormal numbers. For instance, Single precision can still easily handle five digits of precision at the 1.####E40 level. For instance, if we use your functions and apply the following code ...
Code:
Option Explicit
Private Sub Form_Click()
Dim n As Single
n = 1.2345E40
Debug.Print Format$(n, "0.#############################################"), SinglePrecision(n)
n = 1.2346E40
Debug.Print Format$(n, "0.#############################################"), SinglePrecision(n)
n = 1.2347E40
Debug.Print Format$(n, "0.#############################################"), SinglePrecision(n)
End Sub
We'll see that the numbers report correctly. However, your function states that we have no precision remaining. With a bit of work, that could be fixed. When we get down into the IEEE subnormal area, we do sacrifice precision to gain even smaller numbers, until we get down to a very small number with one digit of precision. However, all of this is well documented.
The place I typically go to when dealing with this IEEE precision stuff is Wikipedia. In the Double precision page, they have a nice discussion about the subnormal area.
Wikipedia Single
Wikipedia Double
Even this second point isn't a showstopper for those of us who typically deal with numbers in "normal" ranges.
Take Care, and Again Thanks,
Elroy
Last edited by Elroy; May 14th, 2018 at 05:56 PM.
Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will reattach those licenses and/or attributions. To all, peace and happiness.

May 16th, 2018, 07:50 AM
#3
Thread Starter
Junior Member
Re: Single and Double precision
Thank you for your point Elroy and for taking the time to answer
Extremely useful and well explained (as usual)
Nico
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
