UnitParser was always expected to deal with all the errors internally, an almost-forced decision on account of its somehow-prone-to-errors essence (i.e., a very flexible input system supporting a wide variety of configurations). Additionally and due to the huge variability of potential values together with the high precision and accuracy expectations, the big-number support was also included since almost the beginning.
Before going ahead with the analysis of this code, it is better to know about
. As its name indicates, this
was originally intended to be used internally when dealing with unit-related calculations. The close linkage between unit calculations (e.g., conversions) and numeric calculations (e.g., accounting for a unit prefix value) explains why that initial configuration quickly evolved into the definitive one also dealing with numeric values (i.e., the
I call "managed calculations" to the code dealing with the arithmetic operations between
variables, where all the errors are managed internally and the
) are taken into account. Most of this code is stored in Operations_Private_Managed.cs
, which includes the three following main methods:
NormaliseUnitInfo. It simplifies/normalises the complex reality of
UnitInfo values (i.e., formed by three independent variables with quite big ranges) in order to facilitate all the subsequent arithmetic operations. The goal of this method is to store as much information as possible from
BaseTenExponent. Note that
Prefix is always cleared, but
Value might eventually keep some information.
PerformManagedOperationUnits. This is the main method in charge of performing arithmetic operations between
UnitInfo variables. All the
operator overloads defined in the
UnitInfo class call this method to perform the corresponding operations.
This method is the safest way to carry out any operation because it accounts for all the possible scenarios; for example, non-normalised
UnitInfo variables or adding/subtracting two variables with different
PerformManagedOperationValues. Unlikely the previous method, this one doesn't perform any pre-check and that's why it should only be used under very specific conditions.
A typical situation where it might be preferred over
PerformManagedOperationUnits is an operation where only
Value needs to be considered, at least for one of the variables (e.g., multiplication between
double variables). Note that
PerformManagedOperationValues is quicker and returns a non-normalised variable (i.e., the ideal output under certain conditions).