finos / common-domain-model

The CDM is a model for financial products, trades in those products, and the lifecycle events of those trades. It is an open source standard that aligns data, systems and processes and is available as code in multiple languages for easy implementation across technologies.
Other
142 stars 60 forks source link

RoundToSignificantFigures function required to round to specified number of significant figures #3154

Open hugohills-regnosys opened 2 months ago

hugohills-regnosys commented 2 months ago

Background

Currently the CDM supports rounding functionality in functions cdm.base.math.RoundToPrecision (see https://github.com/finos/common-domain-model/issues/2915) and cdm.base.math.RoundToNearest, however this does not cover the use-case for rounding to a number of significant figures.

This particular use-case for this is from DRR where prices that are expressed as percentages are required to be reported in the format: “up to 11 numeric characters including up to 10 decimals if price is expressed as percentage”.

Proposal

Add function cdm.base.math.RoundToSignificantFigures. For the DRR use-case, the price should be rounded by the RoundToPrecision function (with precision = 10), then rounded by the RoundToSignificantFigures function (with significant figures = 11).

func RoundToSignificantFigures: <"Round a number to the supplied significant figures, using the supplied rounding direction.">
    inputs:
        value number (1..1) <"The original (unrounded) number.">
        significantFigures int (1..1) <"The number of significant figures.">
        roundingMode RoundingDirectionEnum (1..1) <"The method of rounding (up/down/nearest).">
    output:
        roundedValue number (1..1) <"The value to the desired number of significant figures.">

    condition NonZeroSignificantFigures: <"The number of significant figures should be greater than zero.">
        significantFigures > 0

The following examples show the function behaviour:

RoundToSignificantFigures(1023.123456789, 5, RoundingDirectionEnum -> NEAREST)

RoundToSignificantFigures(1023.123456789, 5, RoundingDirectionEnum -> UP)

RoundToSignificantFigures(1023.123456789, 5, RoundingDirectionEnum -> DOWN)

RoundToSignificantFigures(1023.123456789, 1, RoundingDirectionEnum -> NEAREST)

RoundToSignificantFigures(1023.1, 7, RoundingDirectionEnum -> NEAREST)

Compatibility

This would be a new function, so there would be no compatibility issues.

manel-martos commented 1 month ago

Hi Hugo, this looks great and we think it'll be enough for DRR requeriments. I don't see in which cases applying RoundToPrecision(10)&RoundToSignificantFigures(11) in sequence makes a difference than just appliying the later, specifically for DRR. Could you clarify if you found any edge case? Thanks!