Support Board
Date/Time: Wed, 15 Jan 2025 11:46:34 +0000
Question About Float Precision in Sierra Chart API(ACSIL)
View Count: 400
[2024-08-06 00:59:48] |
User856649 - Posts: 5 |
I would like to bring up a common issue with floating-point precision in C++/SC study programming. Here's a quick C++ code snippet: --------------------------------------------------------- float y = 0.30f + 0.40f; if (y == 0.70f) cout << "True" <<endl; else cout << "False" << endl; ------------------------------------------------- output: False ---------------------------------------------------- I expected y to be 0.70f, so I thought output could be "True". However, due to how floating-point math works, y may not exactly equal 0.70f, which can lead to the output being "False" instead. this issue is quite common in programming, so that C# has a build-in type--decimal to avoid this issue. I've noticed that the Sierra Chart API(ACSIL) mainly uses float and double types for numbers/prices. Given the importance of precision in trading software, I'm curious if the Sierra Chart API accounts for float precision issues. Are there any built-in features or best practices to help developers avoid problems related to floating-point calculations? Thanks for your help. |
[2024-08-06 08:20:40] |
User431178 - Posts: 568 |
These functions are availble for comparison of floating point values. sc.FormattedEvaluate() sc.FormattedEvaluateUsingDoubles() |
[2024-08-13 20:36:59] |
User856649 - Posts: 5 |
These functions are availble for comparison of floating point values. sc.FormattedEvaluate() sc.FormattedEvaluateUsingDoubles() i define two binary floating-point numbers: float fnumber = 10.12; double dnumber = 10.12; in memory showed by debug window, they look like below: fnumber is 10.1199999 dnumber is 10.119999999999999 and as far as i know, the internal arrays in SC are either float or double, such as sc.Subgraph[].Data[] / sc.Subgraph[][] (Read/Write. Array of float variables (SCFloatArray)). maybe for most cases, this inaccuracy will not cause any issue, but for some cases, it causes issues (sometimes the issue will be unpredictable). that is why i bring this topic out for discussion. I'll take a wild guess that when SC was first being developed (1996), the authors probably didn't realize that using binary floating-point numbers could cause issues, or at that time, the whole finance industry hadn't really come to a consensus on using decimal floating-point numbers. So, it would be good for SC support/engineering to share their thoughts on this problem. thanks. |
[2024-08-13 20:48:37] |
John - SC Support - Posts: 37226 |
There is nothing for us to state regarding this. What you are referring to is a well known issue with floating point numbers and programmers have been working around it for many years. This is just something you need to deal with.
For the most reliable, advanced, and zero cost futures order routing, use the Teton service: Sierra Chart Teton Futures Order Routing |
[2024-08-13 22:03:26] |
User856649 - Posts: 5 |
There is nothing for us to state regarding this. What you are referring to is a well known issue with floating point numbers and programmers have been working around it for many years. This is just something you need to deal with. for example, i use the price data (i assume they are 100% accuracy), and convert them to decimal floating-point numbers and perform calculation to ensure the precision on calculation stage. once i get the calculation result, i pass the number(s) to e.g sc.Subgraph[].Data[] / sc.Subgraph[][]. based on the doc, this is a Array of float variables (SCFloatArray). my question here is what type float it is? native (binary)float? or a customized decimal float? I don't know. any guidance on your website? as far as i know -- no. if it is a native float, then once i pass the number to the array, it losses precision immediately. if it is not, then what kind of customized decimal floating-point type you are using, how can i convert my decimal to meet your decimal? in the example of sc.Subgraph[].Data[] / sc.Subgraph[][] part, it only shows this "sc.Subgraph[2][sc.Index] = 12.5f; " |
[2024-08-14 12:59:59] |
Sierra_Chart Engineering - Posts: 17953 |
The type of float is this: https://en.wikipedia.org/wiki/Single-precision_floating-point_format Sierra Chart Support - Engineering Level Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy: https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation For the most reliable, advanced, and zero cost futures order routing, use the Teton service: Sierra Chart Teton Futures Order Routing |
[2024-08-14 20:22:46] |
User856649 - Posts: 5 |
excellent, it is just native float in C, C++, C#, which is not Decimal Floating Point numbers defined in IEEE 754 and ISO/IEC DTR 24733. Now I can accurately evaluate the data processing and storage accuracy of SC platform.
|
To post a message in this thread, you need to log in with your Sierra Chart account: