random1

round


The round function converts a number or 1-dimensional array of numbers to the nearest integer.

Syntax

rounded_numbers = round( numbers )

bullet.gif    where numbers is a 1-dimensional array of numbers to be rounded.

bullet.gif    rounded_numbers is the table or number after rounding.

Remarks

The number x is rounded to integer n if (n – 0.5) <= x < (n + 0.5). Half integers n+0.5 are rounded up or down in the standard way to avoid statistical bias.

Example

The following script lists the rounded values of several numbers in the range -3 through +3. Particular cases involving the odd/even rounding bias correction are notes as comments.

n = {}

-- create a table to hold the values

n[1] = -3

 

n[2] = -2.50000001

 

n[3] = -2.5

-- should round up to -2

n[4] = -2.49999999

 

n[5] = -2

 

n[6] = -1.50000001

 

n[7] = -1.5

-- should round down to -2

n[8] = -1.49999999

 

n[9] = -1

 

n[10] = -0.5000001

 

n[11] = -0.5

-- should round up to 0

n[12] = -0.4999999

 

n[13] = 0

 

n[14] = 0.4999999

 

n[15] = 0.5

-- should round down to 0

n[16] = 0.5000001

 

n[17] = 1

 

n[18] = 1.4999999

 

n[19] = 1.5

-- should round up to 2

n[20] = 1.5000001

 

n[21] = 2

 

n[22] = 2.4999999

 

n[23] = 2.5

-- should round down to 2

n[24] = 2.5000001

 

n[25] = 3

 

 

 

for i = 1, table.getn(n) do

-- list all table array elements

  Printf("%.10lg --> %lg\n", n[i], Round( n[i] ) )

end

 

Related Topics

Contents

Table and Array Functions

 


Mira Pro x64 Script User's Guide, v.8.73 Copyright Ⓒ 2024 Mirametrics, Inc. All Rights Reserved.