1

What precision does Excel support? I tried to calculate 0.800000000000001 / 2 and received incorrect result of 0.40000000000000000000, where it should be 0.4000000000000005

Max Koretskyi
  • 733
  • 1
  • 5
  • 10
  • 1
    https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel – Máté Juhász Aug 15 '16 at 08:41
  • thanks, what tool can I use to calculate higher accuracy? – Max Koretskyi Aug 15 '16 at 08:42
  • 3
    Possible duplicate of [Adding more than 15 digits in Excel](http://superuser.com/questions/373997/adding-more-than-15-digits-in-excel) – DavidPostill Aug 15 '16 at 09:20
  • 1
    Asking for tools is off-topic. However see the duplicate for some ideas. – DavidPostill Aug 15 '16 at 09:20
  • How much precision do you need? The VBA Decimal data type supports much higher (25-30 decimals) precision, depending on the magnitude of the value, and you could write the math routines you need. – Ron Rosenfeld Aug 16 '16 at 02:12
  • @Maximus I provided the answer you were looking for. – ejbytes Aug 16 '16 at 22:24
  • @RonRosenfeld I gave your idea a shot, but VBa isn't capable at surpassing the Floating Point limits either. – ejbytes Aug 16 '16 at 22:49
  • @ejbytes You are probably not implementing the Decimal data type properly; or perhaps you are not outputting the results properly. But I can certainly obtain the OP's desired results using the values he provides. – Ron Rosenfeld Aug 17 '16 at 00:44
  • @ejbytes I just posted an example of one way to use the Decimal data type to do simple math. If one's precision is satisfied by the Decimal data type limits, it is useful. For higher precision, one can try something like xNumbers, but it has not been supported for the latest versions of Excel, and there may be some issues. – Ron Rosenfeld Aug 17 '16 at 01:53
  • @RonRosenfeld Ah, I wasn't aware of the `CDec(), VBa library function`. That's all it was eh? Good to know. – ejbytes Aug 17 '16 at 02:20

2 Answers2

3

Excel supports Floating Point arithmetic, which has a limit of 15 decimal places. Here is the investigation and reference. enter image description here

In researching this subject, it is found that only 15 digits are stored in arithmetic quantities, due to Floating Point Notation as suspected.

After searching for this suspected culprit, the reference is found here: https://support.microsoft.com/en-us/kb/78113 and here https://support.microsoft.com/en-us/kb/214118

So, 15 decimal places is the maximum carry over in floating point. Here is the same example in 14 decimal places. Note the 5 in the 14th decimal place; presents further demonstration of the Floating Point limits. enter image description here

To test a theory, commented by a member in this forum, I decided to add this addendum. No, not even in VBa can the Double variable surpass the Floating Point limitations. I used Double precision and returned the value as String to demonstrate even further here: enter image description here

An Exception to the Rule!

Thank you, Ron Rosenfeld. With regards to the last image, regarding VBa. If you are using VBa code, then utilizing the VBa library function CDec() does indeed capture the precision not covered in the limits as discussed above. However, with one caveat, you must return the data as a String for display purposes. A reference can be found here: http://www.techonthenet.com/excel/formulas/cdec.php

ejbytes
  • 2,014
  • 3
  • 13
  • 25
  • 1
    @fixer1234 I've updated my answer and gave reason why precision is lost at 15 decimal places; Floating Point limitations. – ejbytes Aug 16 '16 at 20:20
  • It's [a bit more complicated](http://floating-point-gui.de/) than "15 digits." Floating point arithmetic is not the same thing as [BCD](https://en.wikipedia.org/wiki/Binary-coded_decimal) or other decimal-oriented encodings. – Warren Young Aug 17 '16 at 01:53
  • @WarrenYoung Yeah, I know. It's been a while since I had to write anything in Floating Point Notation. After all I studied computer science in college, they don't hand out those degrees too easily, you have to work hard for them. It's a simplified "take away" for seeker of special answers to an Excel question about precision. I didn't write a book, but I referenced a good source. – ejbytes Aug 17 '16 at 01:58
2

Here is an example of how to use the VBA Decimal data type in simple math operations, to obtain higher precision than Excel's 15 decimal digit precision.

Obviously, you can expand this to use for other types of operations. However, if 28 decimal precision, or if the allowable number range (see Help) is not sufficient, you will need a different tool.

The below example is only for Addition, Multiplication and Division. But you can easily extend it to other operations, or implement it differently, according to what you really need.

Note that the results are returned as a string (text). Otherwise Excel will not be able to display the results. If you were using it solely within VBA, you could return the result as a Variant/Decimal.

Function HiPrecMath(sOp As String, ParamArray Factors() As Variant) As String
    Dim V As Variant
        V = Factors
    If sOp Like "Add*" Then
        HiPrecMath = vbADD(V)
    ElseIf sOp Like "Mult*" Or sOp Like "Prod*" Then
        HiPrecMath = vbMULT(V)
    ElseIf sOp Like "Div*" Then
        HiPrecMath = vbDIV(V)
    End If
End Function

Private Function vbADD(F) As String
    Dim vRes As Variant
    For I = 0 To UBound(F)
        vRes = vRes + CDec(F(I))
    Next I
vbADD = vRes
End Function

Private Function vbMULT(F) As String
    Dim vRes As Variant
    vRes = F(0)
    For I = 1 To UBound(F)
        vRes = vRes * CDec(F(I))
    Next I
vbMULT = vRes
End Function

Private Function vbDIV(F) As String
    Dim vRes As Variant
    vRes = F(0)
    For I = 1 To UBound(F)
        vRes = vRes / CDec(F(I))
    Next I
vbDIV = vRes
End Function

Here is an example using your data:

enter image description here

ejbytes
  • 2,014
  • 3
  • 13
  • 25
Ron Rosenfeld
  • 8,198
  • 3
  • 13
  • 17