---
title: Rounding modes
summary: null
url: https://www.fastly.com/documentation/reference/vcl/rounding-modes
---

Fastly VCL provides access to various _rounding modes_ by way of independent functions for rounding values. These functions have explicit rounding modes. There is no stateful interface to set a "current" rounding mode.

Fastly VCL does not provide interfaces to round values to a given number of significant figures, to a given multiple, or to a given power.

## Tie-breaking when rounding to nearest

The [roundoff errors](http://mathworld.wolfram.com/RoundoffError.html) introduced by rounding values to their nearest integers are symmetric, except for treatment of the exact midpoint between adjacent integers.

That is, for every value that gets rounded up (such as 3.77 rounding up to the nearest integer 4.0), there is a corresponding value (3.23) which is rounded down by the same amount. This can be seen visually:

![tie-breaking](/reference/vcl/img/tie-breaking.svg)

Rounding to the nearest integer requires a tie-breaking rule for when the fractional part of a value is exactly 0.5. There are several ways to break these ties, enumerated in the "to nearest" rounding modes below.

## Overview

Example values:

|   Input  | ceil | floor | trunc | round | roundeven | roundhalfup | roundhalfdown |
| :------: | :--: | :---: | :---: | :---: | :-------: | :---------: | :-----------: |
| **-1.8** | -1.0 |  -2.0 |  -1.0 |  -2.0 |    -2.0   |     -2.0    |      -2.0     |
| **-1.5** | -1.0 |  -2.0 |  -1.0 |  -2.0 |    -2.0   |     -1.0    |      -2.0     |
| **-1.2** | -1.0 |  -2.0 |  -1.0 |  -1.0 |    -1.0   |     -1.0    |      -1.0     |
| **-0.5** | -0.0 |  -1.0 |  -0.0 |  -1.0 |    -0.0   |     -0.0    |      -1.0     |
|  **0.5** |  1.0 |  0.0  |  0.0  |  1.0  |    0.0    |     1.0     |      0.0      |
|  **1.2** |  2.0 |  1.0  |  1.0  |  1.0  |    1.0    |     1.0     |      1.0      |
|  **1.5** |  2.0 |  1.0  |  1.0  |  2.0  |    2.0    |     2.0     |      1.0      |
|  **1.8** |  2.0 |  1.0  |  1.0  |  2.0  |    2.0    |     2.0     |      2.0      |

A visual representation of the same:

![rounding-modes](/reference/vcl/img/rounding-modes.svg)

## "Direct" rounding modes

All of the following modes always round towards a specific direction.

- **Round up** — `math.ceil`  
  Also known as _ceiling_, _round towards positive infinity_
  [IEEE 754](https://standards.ieee.org/ieee/754/6210/) roundTowardPositive

  Non-integer values are rounded up towards +∞. Negative results thus round toward zero.

- **Round down** — `math.floor`  
  Also known as _floor_, _round towards negative infinity_
  [IEEE 754](https://standards.ieee.org/ieee/754/6210/) roundTowardNegative

  Non-integer values are rounded down towards -∞. Negative results thus round away from zero.

- **Round towards zero** — `math.trunc`  
  Also known as _truncation_, _round away from infinity_
  [IEEE 754](https://standards.ieee.org/ieee/754/6210/) roundTowardZero

  Rounding is performed by removing the fractional part of a number, leaving the integral part unchanged.

  Note the FLOAT to INTEGER type conversion in Fastly VCL is not by truncation (as it is in many comparable languages). See discussion under _ties away from zero_.

- **Round away from zero**  
  Also known as _round towards infinity_

  Positive non-integer values are rounded up towards positive infinity. Negative non-integer values are rounded down towards negative infinity.

  Not provided in Fastly VCL.

## “To nearest” rounding modes

All of the following modes round non-tie values to their nearest integer. These modes differ only in their treatment of ties.

- **Round to nearest, ties away from zero** — `math.round`  
  Also known as _commercial rounding_
  [IEEE 754](https://standards.ieee.org/ieee/754/6210/) roundTiesToAway

  For positive values, ties are rounded up towards positive infinity. For negative values, ties are rounded down towards negative infinity.

  This is symmetric behaviour, avoiding bias to either positive or negative values. However, this mode does introduce bias away from zero.

  This rounding mode is used for implicit FLOAT to INTEGER type conversions in VCL. These behave as if by a call to `math.round`.

- **Round to nearest, ties to even** — `math.roundeven`  
  Also known as _half to even_, _convergent rounding_, _statistician's rounding_, _Dutch rounding_, _Gaussian rounding_, _odd–even rounding_, and _bankers' rounding_
  [IEEE 754](https://standards.ieee.org/ieee/754/6210/) roundTiesToEven

  Of the two nearest integer values, ties are rounded either up or down to whichever value is even.

  This rounding mode increases the probability of even numbers relative to odd numbers, but avoids bias to either positive or negative values, and also avoids bias towards or away from zero. The cumulative error is minimized when summing rounded values, especially when the values are predominantly positive or predominantly negative.

- **Round to nearest, ties towards positive infinity** — `math.roundhalfup`  
  Also known as _half up_

  This is asymmetric behavior, where ties for negative values are rounded towards zero, and ties for positive values are rounded away from zero.

> **WARNING:** Some languages use the term _half up_ to mean symmetric behaviour. For rounding functions in these languages, "up" is a value of larger absolute magnitude. That is, negative ties will be rounded away from zero, which differs from the behavior in VCL. Take care when porting code using this rounding mode to VCL.

- **Round to nearest, ties towards negative infinity** — `math.roundhalfdown`  
  Also known as _half down_

  This is asymmetric behavior, where ties for negative values are rounded away from zero, and ties for positive values are rounded towards zero.

> **WARNING:** Some languages use the term _half down_ to mean symmetric behavior. For rounding functions in these languages, "down" is a value of smaller absolute magnitude. That is, negative ties will be rounded towards zero, which differs from the behavior in VCL. Take care when porting code using this rounding mode to VCL.

- **Round to nearest** with other tie-breaking schemes  
  There are several other less common arrangements for tie-breaking. These include _ties to odd_ (in a similar manner as _ties to even_), random tie-breaking, and stochastic tie-breaking.

  These schemes are not provided in Fastly VCL.

Floating point numbers have have more computational nuances than are described by the cursory discussion of biases here. For more details, see [What every computer scientist should know about floating-point arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)
