# round

The rounding used in the following is called the bankers’ rounding.

## round(a: number) 🡒 number, const autodiff pure function

Rounds a number to the nearest integer. If the fractional part is 0.5, the number is rounded to the nearest even integer.

show table "" a1a3 with
round(0.5)  // 0
round(0.51) // 1
round(1.5)  // 2
round(4.5)  // 4


Calling round(x) or round(x, 0) (see below) gives identical results.

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.

## round(a: number, b : number) 🡒 number, const pure function

Rounds a number to the nearest integer, considering the number of digits that are intended to be kept, passed as a second argument.

show table "" a1a3 with
round(4.33, 0) // 4
round(4.33, 1) // 4.3
round(4.33, 2) // 4.33


The second argument must be an integer between 0 and 15 (inclusive), otherwise the function will fail.

User Contributed Notes
0 notes + add a note