This topic depicts issues, related with using real numbers, while manipulating FIX messages, as well as solutions for the issues, are offered by the FIX Engine.
FIX Protocol supposes FIX messages that exchange using 'tag=value' presentation. Except special cases, field value is a text-based, so regular presentation of real numbers with decimal point is used:
Interface of the OnixS::FIX::Message class that encapsulates primary operations over FIX fields for a FIX message, offers a few overloads, allowing users to setup field values of native language types like int, long and double. The field value setup function-member converts the value of a primitive type into its string presentation using natural transformation.
However, for native types like float and double there's no unambiguous transformation available. That's because double values usually have no finite presentation due to limitation of data type.
In particular, assume the following initialization of a variable of double type is executed:
In spite of exact constant definition, the real value, hold by a defined variable, will look like 100.099999999999998, but not 100.1.
This is because each double value is expressed as X * 2 ^ Y, where X is mantissa and Y is 2-basis exponent.
As a result, each time value of a double type is assigned as a FIX field value, this issue might happen when transformation into string gives different text presentation:
in compare to expected one:
As a common solution for this case, OnixS::FIX::FieldSet::set and OnixS::FIX::FieldSet::setV overloads expose additional parameter which defines rounding precision for a double to string transformation.
As a result, transformation rounds value is being set to the given number of digits after the decimal point:
Using rounding precision usually avoids appearance of infinite approximated values in FIX messages, because the being set value is rounded to a strictly defined precision.
In accordance with the FIX specification, trailing zeros in decimal string presentation make no sense. In other words, the following strings are the same: "23.0" = "23.0000" = "23". Therefore, OnixS::FIX::Message class discards trailing zeros in the string presentation of decimal values. However, there may be some venues, which require trailing zeros for decimal values of some fields (e.g. Price <44> field). In such cases, you can use the OnixS::FIX::FieldSet::set method for string values and set such decimal values as string values:
Additionally, you can use e.g. std::sprintf function and assign the field value as a pre-formatted string rather than a double value:
Since the version 3.0 FIX Engine exposes OnixS::FIX::Decimal class for better operability over real numbers like prices and quantities are.
In contrast to a double type, OnixS::FIX::Decimal presents real numbers use 10-basis exponent. Therefore, each number looks like X * 10 ^ Y, where X is mantissa and Y is an exponent.
Also, being C++ class, OnixS::FIX::Decimal offers constructing from 64-bit integer mantissa and 32-bit integer exponent. In this case, it eliminates unambiguity and approximation during the defining value:
Parsing OnixS::FIX::Decimal from the text presentation is also free of approximation issues in bounds of values, operated in FIX messages.
Thus, operating prices and quantities as OnixS::FIX::Decimal, instead of double types, eliminates issues that are caused by an inexact value presentation, as well as gives performance benefits.