--

--

--

--

--

--

--

The contents in this website aren't being updated anymore. For more information, go to https://varocarbas.com/.

UnitParser code (.NET/C#) >

Managed calculationsUnitParser 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

I call "managed calculations" to the code dealing with the arithmetic operations between

Before going ahead with the analysis of this code, it is better to know about

`UnitInfo`

. As its name indicates, this `private class`

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 `UnitP`

fields `Value`

, `BaseTenExponent`

and `UnitPrefix`

).I call "managed calculations" to the code dealing with the arithmetic operations between

`UnitInfo`

variables, where all the errors are managed internally and the `UnitP`

peculiarities (e.g., `BaseTenExponent`

or `UnitPrefix`

) 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`Value`

and`Prefix`

into`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 overload`

s 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`BaseTenExponent`

values.`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`UnitP`

and`double`

variables). Note that`PerformManagedOperationValues`

is quicker and returns a non-normalised variable (i.e., the ideal output under certain conditions).