Question

I am using VariantCopyInd . The source contains 1111.199999999. However after VariantCopyInd the value gets rounded off in the destination as 1111.200000. I would like to retain the original value . how can this be achieved ?

Was it helpful?

Solution 2

I found a very good link from msdn

enter link description here The link clearly indicates any number whose length is greater than 15 will evaluate into incorrect results .

Take 2 cases

1) 101126.199999999 will store a correct value , since the length is 15 . No conversion or precision loss 2) 111.12345678912345 will store incorrect value since the length is 17 . Conversion will be done

OTHER TIPS

This has nothing to do with VariantCopyInd, but merely the fact that the literal as it exists in the code, has not exact representation in the floating point format used internally by COM Variants.

Therefore, there is no way to achieve what you want, except to use the CURRENCY type of variant. It will have limited precision, see MSDN:

CURRENCY types use a decimal representation internally, just like the code literal. You will still have to provide an indirect initialization (from string, not a float/double literal) in code, to prevent any unwanted representation effects.

A currency number stored as an 8-byte, two's complement integer, scaled by 10,000 to give a fixed-point number with 15 digits to the left of the decimal point and 4 digits to the right. This IDispatch::GetTypeInforesentation provides a range of 922337203685477.5807 to -922337203685477.5808.

The CURRENCY data type is useful for calculations involving money, or for any fixed-point calculation where accuracy is particularly important.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top