round

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

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

Rounds a number to the nearest even number.

show table "" a1a3 with
  round(0.5) // 0
  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, pure function

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

Example:

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.