PDA

View Full Version : VBA: What can cause of truncation of significant digits double precision variable?



stanhilliard
06-10-2022, 11:05 AM
I read inputs to a VBA macro from a worksheet, calculate values with double precision variables. I display the result back on the worksheet - and they have only 7 significant digits instead of 14.

Question: What operations can cause a double precision variable to have only 7 significant digits?

Beethoven
06-10-2022, 03:50 PM
Make sure all of your intermediate variables and constants are double precision

Paul_Hossler
06-10-2022, 07:23 PM
Can you post example?

arnelgp
06-10-2022, 08:24 PM
you can convert the numbers as Currency or Decimal while performing calculation in VBA.
these two are more accurate than Double datatype.

stanhilliard
06-10-2022, 10:38 PM
you can convert the numbers as Currency or Decimal while performing calculation in VBA.
these two are more accurate than Double datatype.

Thanks, I will test those.

stanhilliard
06-10-2022, 11:30 PM
I don't understand the currency data type. I tested it and it seems to always round decimal fractions to 4 places. Perhaps there is a technique to get around that for general usage?

Aussiebear
06-11-2022, 12:39 AM
you can convert the numbers as Currency or Decimal while performing calculation in VBA.
these two are more accurate than Double datatype.

Sorry but I would have to disagree there when using my Macs. We had major problems converting Degrees/Minutes to Eastings/Northings for Vincenty formula until all values were converted using Double datatypes

stanhilliard
06-11-2022, 12:34 PM
I am tracing values through my VBA code which I suspend with "stop". Is it correct determine significant digits by counting the digits in the popup that occurs when I hold the cursor over a variable? Is there a better way?

snb
06-11-2022, 01:12 PM
Sub M_snb()
MsgBox 1 / 3 & vbLf & Len(CStr(1 / 3))
End Sub

Paul_Hossler
06-11-2022, 03:57 PM
Is there a better way?

Posting the code that causes the truncation

Are you sure that you're looking at the Display of the number and not the full precision?


29842

p45cal
06-11-2022, 10:41 PM
Is the column wide enough?

Paul_Hossler
06-12-2022, 09:55 AM
If the truncated cell is the output of a formula, you can display with full (all that is available) precision by selecting the cell (e.g. C1) click in the formula bar, and pressing F9


29843

29844

stanhilliard
06-12-2022, 03:15 PM
If the truncated cell is the output of a formula, you can display with full (all that is available) precision by selecting the cell (e.g. C1) click in the formula bar, and pressing F9


29843

29844

Thanks. That saved the work of widening the columns and using the arrows on the [Home] menu to expand the number until you start getting all zeros - and then guessing which zeros are significant.

By the way, can anyone explain why those arrows point in the wrong direction?

Aussiebear
06-12-2022, 03:44 PM
The arrows point in the direction that you wish to move the decimal point.

stanhilliard
06-12-2022, 04:02 PM
The arrows point in the direction that you wish to move the decimal point.

Thanks
OK, I see where an abstract programmer would think of where the decimal point goes. I think more visually, and see adding the digits at the end of a decimal fraction as expanding to the right. I often press the right arrow wrongly.:(

Paul_Hossler
06-12-2022, 04:39 PM
The arrows point in the direction that you wish to move the decimal point.

I never thought of it that way -- I always thought, "Damn I wanted the other one!" I learned something today

Aussiebear
06-12-2022, 05:03 PM
OMG... is this an Aussie thing?

SamT
06-13-2022, 08:48 AM
Math operations with Currency Types are limited to 4 decimal places to prevent rounding issues with "Smaller" Decimal values.

4 decimal places are sufficient for mathematics for all currencies that use 1/100 units. Ie: $1.12