Site Tools


Rounding Numbers

Developer: RhinoScript
Summary: Discusses number rounding in RhinoScript

You need to round when you want to convert a number of greater precision into a number of lesser precision. The most common case is when you need to convert a floating-point number into an integer.

Rounding down

The simplest form of rounding is truncation. Any digits after the desired precision are simply ignored. The Fix function is an example of truncation. For example:

 Rhino.Print Fix( 3.5) '  3
 Rhino.Print Fix(-3.5) ' -3

The Int function rounds down to the highest integer less than the value. Both Int and Fix act the same way with positive numbers - truncating - but give different results for negative numbers:

 Rhino.Print Int( 3.5) '  3
 Rhino.Print Int(-3.5) ' -4

The Fix function is an example of symmetrical rounding because it affects the magnitude (absolute value) of positive and negative numbers in the same way. The Int function is an example of asymmetrical rounding because it affects the magnitude of positive and negative numbers differently.

RhinoScript has a Floor function that truncates positive values, but it does not work with negative numbers.

 Rhino.Print Rhino.Floor( 3.5) '  3
 Rhino.Print Rhino.Floor(-3.5) ' -4

Rounding up

RhinoScript has a Ceil function which always rounds fraction values up (more positive) to the next value.

   Rhino.Print Rhino.Ceil( 3.5) '  4
   Rhino.Print Rhino.Ceil(-3.5) ' -3

VBScript does not have a corresponding round-up function. However, for negative numbers, both Fix and Int can be used to round upward, in different ways.

Fix rounds towards 0 (up in the absolute sense, but down in absolute magnitude).

 Rhino.Print Fix( 3.5) '  3
 Rhino.Print Fix(-3.5) ' -3

Int rounds away from 0 (up in terms of absolute magnitude, but down in the absolute sense).

 Rhino.Print Int( 3.5) '  3
 Rhino.Print Int(-3.5) ' -4

Arithmetic rounding

When you always round one direction, the resulting number is not necessarily the closest to the original number. For example, if you round 1.9 down to 1, the difference is a lot larger than if you round it up to 2. It is easy to see that numbers from 1.6 to 2.4 should be rounded to 2.

But what about 1.5, which is equidistant between 1 and 2? By convention, the half-way number is rounded up.

You can implement rounding half-way numbers in a symmetrical fashion, where -.5 is rounded down to -1, or in an asymmetrical fashion, where -.5 is rounded up to 0.

VBScript does not have any functions that do arithmetic rounding.

Banker's rounding

When you add rounded values together, always rounding .5 in the same direction results in a bias that grows the more numbers you add together. One way to minimize the bias is with banker's rounding.

Banker's rounding sometimes rounds .5 up and sometimes rounds .5 down. The convention is to round to the nearest even number, so that both 1.5 and 2.5 round to 2, and 3.5 and 4.5 both round to 4. Banker's rounding is symmetrical.

In VBScript, the following numeric functions perform banker's rounding: CByte, CInt, CLng, CCur, and Round.

Random rounding

Even banker's rounding can bias totals. You can take an extra step to remove bias by rounding .5 up or down in a truly random fashion. This way, even if the data is deliberately biased, bias might be minimized. Still, using random rounding with randomly distributed data might result in a larger bias than banker's rounding. Random rounding could result in two different totals on the same data.

VBScript does not have any functions that do random rounding.

Alternate rounding

Alternate rounding is rounding between .5 up and .5 down on successive calls.

VBScript does not have any functions that do alternate rounding.

Sample user-defined rounding functions

The following code provides sample implementations for each of the rounding types.

All these functions take two arguments: the number to be rounded and a factor. If the factor is 1, then the functions return an integer created by one of the above methods. If the factor is something other than 1, the number is scaled by the factor to create different rounding effects. For example AsymArith (2.55, 10) produces 2.6. That is, it rounds to 1/factor = 1/10 = 0.1.

Note: A factor of 0 generates a run-time error, 1/factor = 1/0.

 ' Asymmetrically rounds numbers down - similar to Int().
 ' Negative numbers get more negative.
 Function AsymDown(X, Factor)
   AsymDown = Int(X * Factor) / Factor
 End Function
 
 ' Symmetrically rounds numbers down - similar to Fix().
 ' Truncates all numbers toward 0.
 ' Same as AsymDown for positive numbers.
 Function SymDown(X, Factor)
   SymDown = Fix(X * Factor) / Factor
 End Function
 
 ' Asymmetrically rounds numbers fractions up.
 ' Same as SymDown for negative numbers.
 ' Similar to Rhino.Ceil().
 Function AsymUp(X, Factor)
   Dim Temp
   Temp = Int(X * Factor)
   AsymUp = (Temp + IIf(X = Temp, 0, 1)) / Factor
 End Function
 
 ' Symmetrically rounds fractions up - that is, away from 0.
 ' Same as AsymUp for positive numbers.
 ' Same as AsymDown for negative numbers.
 Function SymUp(X, Factor)
   Dim Temp
   Temp = Fix(X * Factor)
   SymUp = (Temp + IIf(X = Temp, 0, Sgn(X))) / Factor
 End Function
 
 ' Asymmetric arithmetic rounding - rounds .5 up always.
 Function AsymArith(X, Factor)
   AsymArith = Int(X * Factor + 0.5) / Factor
 End Function
 
 ' Symmetric arithmetic rounding - rounds .5 away from 0.
 ' Same as AsymArith for positive numbers.
 Function SymArith(X, Factor)
   SymArith = Fix(X * Factor + 0.5 * Sgn(X)) / Factor
 End Function
 
 ' Banker's rounding.
 ' Rounds .5 up or down to achieve an even number.
 ' Symmetrical by definition.
 Function BRound(X, Factor)
   Dim Temp, FixTemp
   Temp = X * Factor
   FixTemp = Fix(Temp + 0.5 * Sgn(X))
   If Temp - Int(Temp) = 0.5 Then
     If FixTemp / 2 <> Int(FixTemp / 2) Then
       FixTemp = FixTemp - Sgn(X)
     End If
   End If
   BRound = FixTemp / Factor
 End Function
 
 ' Random rounding.
 ' Rounds .5 up or down in a random fashion.
 ' (Execute Randomize statement somewhere prior to calling.)
 Function RandRound(X, Factor)
   Dim Temp, FixTemp
   Temp = X * Factor
   FixTemp = Fix(Temp + 0.5 * Sgn(X))
   If Temp - Int(Temp) = 0.5 Then
     FixTemp = FixTemp - Int(Rnd * 2) * Sgn(X)
   End If
   RandRound = FixTemp / Factor
 End Function
 
 ' Alternating rounding.
 ' Alternates between rounding .5 up or down.
 Public fReduce
 Function AltRound(X, Factor)
   Dim Temp, FixTemp
   If IsEmpty(fReduce) Then fReduce = False
   Temp = X * Factor
   FixTemp = Fix(Temp + 0.5 * Sgn(X))
   If Temp - Int(Temp) = 0.5 Then
     If (fReduce And Sgn(X) = 1) Or (Not fReduce And Sgn(X) = -1) Then
       FixTemp = FixTemp - Sgn(X)
     End If
     fReduce = Not fReduce
   End If
   AltRound = FixTemp / Factor
 End Function

Note: Many of these sample functions require the following utility function.

 ' VBScript equivalent to VB's Immediate If function.
 Function IIf(expr, true_val, false_val)
   If expr Then
     IIf = true_val
   Else 
     IIf = false_val
   End If
 End Function

The following table shows the effects of various factors:

ExpressionResultComment
AsymArith(2.5)3Rounds up to next integer.
BRound(2.18, 20)2.2Rounds to the nearest 1/20.
SymDown(25, .1)20Rounds down to an even multiple of 10.


Floating point limitations

All the rounding implementations presented here use the double data type, which can represent approximately 15 decimal digits.

Since not all fractional values can be expressed exactly, you might get unexpected results because the display value does not match the stored value.

For example, the number 2.25 might be stored internally as 2.2499999…, which would round down with arithmetic rounding, instead of up as you might expect. Also, the more calculations a number is put through, the greater possibility that the stored binary value will deviate from the ideal decimal value.

Dropping precision as a shortcut in rounding

As taught in school, rounding is usually arithmetic rounding using positive numbers. With this type of rounding, you only need to know the number to 1 digit past where you are rounding to. You ignore digits past the first decimal place. In other words, precision is dropped as a shortcut to rounding the value.

For example, both 2.5 and 2.51 round up to 3, while both 2.4 and 2.49 round down to 2.

When you use banker's rounding (or other methods that round .5 either up or down) or when you round negative numbers using asymmetrical arithmetic rounding, dropping precision can lead to incorrect results where you might not round to the nearest number.

For example, with banker's rounding, 2.5 rounds down to 2 and 2.51 rounds up to 3.

With asymmetrical arithmetic rounding, -2.5 rounds up to -2 while -2.51 rounds down to -3.

The user-defined functions in this article take the number's full precision into account when performing rounding.

developer/scriptsamples/rounding.txt · Last modified: 2015/11/25 by sandy