round
There is another function named round()
with 2 arguments (see here).
round, function
def pure const autodiff round(quantity: number): number
def pure const autodiff round(quantity: number, digits: number): number
Return quantity
rounded to digits
precision after the decimal point (default is zero). Values are rounded to the closest multiple of 10 to the power minus digits
. If two multiples are equally close, rounding is done toward the even choice.
Example
show table "" with
round(0.5)
round(0.51)
round(1.5)
round(4.5)
round(-0.5)
round(-0.51)
round(-1.5)
This outputs the following table:
round(0.5) | round(0.51) | round(1.5) | round(4.5) | round(-0.5) | round(-0.51) | round(-1.5) |
---|---|---|---|---|---|---|
0 | 1 | 2 | 4 | 0 | -1 | -2 |
show table "" with
round(4.33, 0)
round(4.33, 1)
round(4.33, 2)
This outputs the following table:
round(4.33, 0) | round(4.33, 1) | round(4.33, 2) |
---|---|---|
4 | 4.3 | 4.33 |
Remarks
Calling round(x)
or round(x, 0)
gives identical results.
Rounding method
The rounding method used here is called the bankers’ rounding (see wikipedia).
The conversion to Float32 numbers used in Envision (see here) can alter the rounding method. For instance, 0.05 is converted to 0.0500000007 in Float32, and thus round(0.05,1)
in Envision will return 0.1 when the expected result was 0.0.
Gradient
When used inside an Autodiff block, the gradient associated to round()
is 1, instead of 0, as the mathematical definition would suggest. The purpose of this irregular behavior is to facilate the design of discrete policies.
Recipes and Best Practices
If you want to round numbers displayed in tiles, you should not use the round()
function but rather stylecode [precision](/specifications/stylecode/properties/precision/)
or [minPrecision](/specifications/stylecode/properties/minprecision/)
.