Skip to main content

Knowledge Base

Multi-precision code

The main advantage of using single precision in compare to double, is to halve the memory usage and to double the speed of execution. The major new feature of MtxVec v6 is the capability to select algorithm precision at runtime. When a vector or matrix variable is declared and then sized for the first time:

a.Size(10, TMtxFloatPrecision.mvDouble);

You can optionally specify the storage precision. This storage precision can also be set/read with a property:

a.FloatPrecision

When working with the vector/matrix in expressions, the precision is preserved:

b.Sin(a);

This capability allows:

  • User specified computational precision from the UI of your application.
  • Write initial algorithm in any precision and decide on what is really needed after profiling.

Internally MtxVec will not strictly use only single precision, when the source data is in single precision. However, floating precision conversion is an expensive operation and it will try to avoid anything that might proove to be costly. At the same time, it will not unneccessarily drop the computational precision from double to single, when there is no performance or storage size benefit to be expected. This gives the user the best of both worlds.

For the source code users, it is possible to specify individual external libraries which are to be linked in. These libraries are available separately for single and for double precision floating point computation. If either precision is not used or needed, they can be left out from the distribution of the final application reducing the final package size.

When writing code, which can work with either single or double precision, consider that:

  • The default array property or the indexer for an object a[i], will work with double or single precision of "a" Vector, but there will be some conversion cost, when the storage precision will be single. This coding pattern is meaningfull, when code simplicity helps, but does not affect the performance too much, because it is not used within tight loops.
  • When tight loops are used and performance matters, it is recommend to split the branch between double and single (float) precision by checking the value of:

    a.IsDouble

    and access:

    a.Values[i]

    for double precision and

    a.SValues[i]

    for single precision.

 

Of course, the corresponding properties/values are available also for the complex number flavors:

a.CValues[i]

for double precision and

a.SCValues[i]

for single precision.