Here I would like to introduce a piece of simple code to show precision loss when involving large number calculation:
//----- code begins -----
Vars: X(8121.2), Y(8339.3);
value1 = Power((X - Y), 4);
value2 = power(X, 4) - 4 * power(X, 3) * Y + 6 * power(X, 2) * Power(Y, 2) - 4 * X * Power(Y, 3) + Power(Y, 4);
once cleardebug;
once print(value1:1:20, NewLine, value2:1:20);
once (value1 - value2 <> 0) print((value1 - value2):1:20);
//----- end of code -----
the output is:
2262677521.11207720000000000000
2262677528.00000000000000000000
-6.88792276382446290000
apparently there is a discrepancy between value1 and value2 and the difference is -6.88792276382446290000. But they should be the same, since the formula of value2 is just an expansion of that of value1.
For some reasons I need to use formulae like that of value2 in my studies.
Has anybody encountered this problem? How do you avoid/solve it?
Thanks for your time.
Is there any way to ensure calculation precision? [SOLVED]
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: Is there any way to ensure calculation precision? [SOLVED]
Hello trazom,
You have reached the limit of precision for double type values in C++ which is 15 digits. (MSDN).
You can use MultiCharts .NET where decimal type that provides 28 digit precision can be used. (MSDN)
You have reached the limit of precision for double type values in C++ which is 15 digits. (MSDN).
You can use MultiCharts .NET where decimal type that provides 28 digit precision can be used. (MSDN)