Numeric data types

  • Thread starter Thread starter Miha Abrahamsberg
  • Start date Start date
M

Miha Abrahamsberg

Hi!

Is there a library or some ad-on/in I can use in VBA to increase the decimal
precision of the variant data type or maybe define a new numeric data type
that could "hold" a decimal precision of at least 15 (right of the decimal
point) ??

Miha
 
Try a variant of subtype Decimal.

This kind of thing:

Function TestDecimal()
TestDecimal = CDec(123456789) + CDec(0.111222333444555)
End Function
 
First of all thank you.

I'm not sure how to use this in my case (see sample code below). I need for
all the variables to be stored at least 15 decimal (fixed; not floating)
point precision and also I need to have all the results in the same
precision (and also all the calculations). So, no precision "can get lost"
during the calculation process (which happens when variables/number are held
as double for example).

--------
Private Sub cmdTest1_Click()
Dim X1 As Variant, Y1 As Variant, Z1 As Variant
Dim X As Variant, Y As Variant, Z As Variant
Dim AT As Variant, BT As Variant, dist As Variant

X = Me.X
X1 = Me.X1
Y = Me.Y
Y1 = Me.Y1
Z = Me.Z
Z1 = Me.Z1

AT = ((X - X1) ^ 2 + (Y - Y1) ^ 2 + (Z - Z1) ^ 2) ^ (0.5)
BT = AT ^(1/3)*386
dist = BT^2 + 3*AT*BT - 4*AT
 
The Decimal is a scalar number, so once you get a value into this type, it
should not be subject to rounding errors.

Therefore:
x = CDec(Me.X)
etc.

In the interest of clarity, use different names for variables and controls;
otherwise which is X referring to?

You may have to keep using CDec() at each stage; unfortunately, there's no
Decimal type, and so no way to force the result to that type implicitly. For
debugging purposes, it might help to ask VBA about the data type at points
where it seems to be playing up, e.g.:
Debug.Print TypeName(x)
 
OK.

Thank you. I'll try it this way.

Miha


Allen Browne said:
The Decimal is a scalar number, so once you get a value into this type, it
should not be subject to rounding errors.

Therefore:
x = CDec(Me.X)
etc.

In the interest of clarity, use different names for variables and
controls; otherwise which is X referring to?

You may have to keep using CDec() at each stage; unfortunately, there's no
Decimal type, and so no way to force the result to that type implicitly.
For debugging purposes, it might help to ask VBA about the data type at
points where it seems to be playing up, e.g.:
Debug.Print TypeName(x)
 
Back
Top