# Thread: Single and Double precision

1. ## 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

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 show-stopper).

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 base-10, as you've shown. And, in this binary-to-base-10 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 base-10 (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 base-2. But like I said, not a show-stopper.

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.####E-40 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.2345E-40
Debug.Print Format\$(n, "0.#############################################"), SinglePrecision(n)
n = 1.2346E-40
Debug.Print Format\$(n, "0.#############################################"), SinglePrecision(n)
n = 1.2347E-40
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 show-stopper for those of us who typically deal with numbers in "normal" ranges.

Take Care, and Again Thanks,
Elroy

3. ## 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
•

Featured

Click Here to Expand Forum to Full Width