VBA rounding problem

前端 未结 2 1089
轮回少年
轮回少年 2020-12-02 01:58

I have this obscure rounding problem in VBA.

a = 61048.4599674847
b = 154553063.208822
c = a + b   
debug.print c
Result: 
154614111.66879 
<
相关标签:
2条回答
  • 2020-12-02 02:25

    Its not obscure, but its not necessarily obvious.

    I think you've sort of answered it - but the basic problem is one of the "size" of the values that is how much data can be stored in a variable of a given type.

    If (and this is very crude) you count the number of digits in each of the numbers in your first example you will see that you have 15 so whilst the range of values that a float (the default type) can represent is huge the precision is limited to 15 digits (I'm sure someone will be along to correct this, I'll tick the wiki box...)

    So when you add the two numbers together it loses the least significant values in order to remain within the allowable precision for a flow.

    By doing a cDec you're converting to a different type of variable (decimal) that is capable of greater precision

    0 讨论(0)
  • 2020-12-02 02:28

    The reason is the limited precission that can be stored in a floating point variable.
    For a complete explanation you shoud read the paper What Every Computer Scientist Should Know About Floating-Point Arithmetic, by David Goldberg, published in the March, 1991 issue of Computing Surveys.

    Link to paper

    In VBA the default floating point type is Double which is a IEEE 64-bit (8-byte) floating-point number.

    There is another type available: Decimal which is a 96-bit (12-byte) signed integers scaled by a variable power of 10
    Put simply, this provides floating point numbers to 28 digit precission.

    To use in your example:

    a = CDec(61048.4599674847)
    b = CDec(154553063.208822)
    c = a + b   
    debug.print c
    Result: 
    154614111.6687894847 
    
    0 讨论(0)
提交回复
热议问题