VBA.VB6 – Rounding functions

VBA.VB6 - Rounding functionsThe mathematical function "round ()" is to turn a specific number in number of lesser precision. The most common case is the conversion of a decimal integer. The IEEE 754-2008 standard standardizes the formats and methods of calculating binary and decimal numbers expressed in floating point, in microprocessors and their environment. It defines five rounding user selectable methods. This standard is also a guide for language designers to ensure their portability, but it is not required, hence the variability of the results of the Round () function as languages.


Here are a dozen mathematical methods to round a number:VBA.VB6 - Rounding functions

Are also referred rounded symmetrical or asymmetrical (relative to zero).

Other names are often used for these rounded but most are vague or ambiguous and therefore, they are subject to different interpretations.

Examples

VBA.VB6 - Rounding functions

The rounded by

The rounding is as close as intuitive to rounding. For example the values ​​3.1, 3.2, 3.3, and 3.4 are rounded down to 3, while the values ​​3.6, 3.7, 3.8, and 3.9 are rounded, but both choices are 3.5 possible (3 or 4).

The different methods of rounding to nearest are distinguished by the way it selects the median value located exactly halfway between the upper and the number rounded number rounded bottom.

Rounding to the nearest even (or banker's rounding)

This method " round ties to Even"Is the moderounded down to the IEEE 754-2008. She should be used for numbers in binary format. With this method of rounding to the meadows even, the median value is rounded to the even number.

For example: 2.5 to 3.5 and 2 to 4.

This symmetrical rounding reduces the cumulative bias which may result from a rounded always in the same direction (cf. rounded towards positive infinity, towards negative infinity, toward zero, arithmetic), but a bias appears when use only even or odd numbers numbers.

Function

This is the function Round ()VB and C #, and the results may not seem surprising not come from a bug.

arithmetic rounding

The arithmetic rounding [symmetrically] " round ties away to" East recommended by the IEEE Standard 754-2008 for numbers in decimal format.

This method is the best known, is that we have learned in math class, the spreadsheet (Excel, Calc, ...). This is a rounding methods closer to where the median value is always rounded up.

For example: 3.5 to 4 and -3.5 to -4.

This method can introduce a cumulative bias but it limits the accumulation of errors during successive calculations.

function:

Public Function ArrondiArithmetique (ByVal Number, Optional ByVal Decimal = 0)'Function arithmetic Rounding [symmetrically]: the closer to 0.5 the infiniteArrondiArithmetique = Fix (Number * 10 ^ Decimal + Sgn (Number) * 1/2) / 10 ^ DecimalEnd Function

Rounded towards positive infinity

This method corresponds to " Round Toward Positive "Defined by the IEEE 754-2008 standard.

It is an asymmetrical rounded. In the case of a positive number, the result remains unchanged if the numbers to eliminate are all zero; if the last digit is rounded up. In the case of a negative number, the figures are simply remove deleted.

For example: 3.1 to 4 and -3.9 to -3.

This method results in a positive cumulative bias and more, it is rarely used in hardware implementations because its realization is more expensive.

function:

Public Function ArrondiVersPlusInfini (ByVal Number, Optional ByVal Decimal = 0)Rounding function to + infinityArrondiVersPlusInfini = (Int (Number * 10 ^ Decimal) _+ IIf (Number = Int (Number Decimal * 10 ^), 0, 1)) / 10 ^ DecimalEnd Function

Round toward negative infinity

This method corresponds to " Round Toward negative "Defined by the IEEE 754-2008 standard.

It is an asymmetrical rounded. In the case of a positive number, the digits to eliminate are simply deleted. In the case of a negative number, the result remains unchanged if the numbers to eliminate are all zero; if the last digit is rounded up.

For example: 3.9 to 3 and -3.1 to -4.

This method is simple to perform materially, resulting in a cumulative negative bias.

function:

Public Function ArrondiVersMoinsInfini (ByVal Number, Optional ByVal Decimal = 0)'Rounding Function to - infinityArrondiVersMoinsInfini = Int (Number * 10 ^ Decimal) / 10 ^ DecimalEnd Function

Rounded towards zero

This method, " Round Toward Zero "Defined by the IEEE 754-2008 standard is also known truncation : Figures are simply removed to eliminate, it is a symmetrical rounded.

Eg 3.9 to 3 and -3.9 to -3.

This method corresponds to the ROUNDDOWN function () spreadsheet (Excel, Calc, ...).

function:

Public Function ArrondiVersZero (ByVal Number, Optional ByVal Decimal = 0)Rounding function to zero; the lower figure (truncation)ArrondiVersZero = Fix (Number * 10 ^ Decimal) / 10 ^ DecimalEnd Function

asymmetric arithmetic rounding

This is one of the methodsrounded to the nearest for which the median value is always rounded towards positive infinity. It corresponds to the function Round the Math library Java.

For example 3.5 to 4 and -3.5 to -3.

This method results in a cumulative positive bias.

function:

Public Function ArrondiArithmetiqueAsymetrique (ByVal Number, Optional ByVal Decimal = 0)'Function asymmetric arithmetic rounding: the closer, 0.5 to + infinityArrondiArithmetiqueAsymetrique = Int (Number Decimal * 10 ^ + 1/2) / 10 ^ DecimalEnd Function

Rounding to nearest to negative infinity

This is one of the methodsrounded to the nearest asymmetric, for which the median value is always rounded towards negative infinity.

For example: 3.5 to 3 and -3.5 to -4.

This method results in a cumulative negative bias.

function:

Public Function ArrondiPlusPresVersMoinsInfini (ByVal Number, Optional ByVal Decimal = 0)'Rounding function as close to the bottom: as close, 0.5 to - infinityArrondiPlusPresVersMoinsInfini = (Fix (Number * 10 ^ Decimal + Sgn (Number) * 1/2) _- IIf (Number * 10 ^ Decimal - Fix (Number * 10 ^ Decimal) = 1/2, Sgn (Number), 0)) _/ 10 ^ DecimalEnd Function

Rounded to the nearest zero

This is one of the methodsrounded to the nearest symmetrical to which the median value is always rounded towards zero

Eg 3.5 to 3 and -3.5 to -3).

This method is sometimes called "rounded Gaussian" because it provides a symmetrical distribution with respect to its average value.

function:

Public Function ArrondiPlusPresVersZero (ByVal Number, Optional ByVal Decimal = 0)'Rounding function as close to zero: as close, 0.5 to zeroArrondiPlusPresVersZero = (Fix (Number * 10 ^ Decimal + Sgn (Number) * 1/2) _- IIF (Number * 10 ^ Decimal - Fix (Number * 10 ^ Decimal) = _Sgn (Number) * 1/2, sgn (Number), 0)) / 10 ^ DecimalEnd Function

Rounded to the nearest odd

This is one of the methodsrounded to the nearest very rarely used symmetrical, for which the median value is always rounded to the odd number.

For example: 3.5 to 3 and 4.5 to 5

Its advantages are theoretically equivalent to those of the rounding to the meadow together, but rounding to the nearest odd presents the disadvantage of never rounded to zero.

function:

Public Function ArrondiPlusPresImpair (ByVal Number, Optional ByVal Decimal = 0)'Rounding function in the closer odd: the closer to 0.5 the oddArrondiPlusPresImpair = (Fix (Number * 10 ^ Decimal + Sgn (Number) * 1/2) _- IIF (Number * 10 ^ Decimal - Int (Number * 10 ^ Decimal) <> 1/2, 0, _IIf (Fix (Number * 10 ^ Decimal + Sgn (Number) * 1/2) / 2 = _Int (Fix (Number * 10 ^ Decimal + Sgn (Number) * 1/2) / 2), Sgn (Number), 0))) _/ 10 ^ DecimalEnd Function

Rounded to the infinite

This is a symmetrical rounding method. The result remains unchanged if the numbers to eliminate are all zero; if the last digit is rounded up.

For example: 3.1 to 4 and -3.1 to -4.

This method corresponds to the ROUNDUP function () spreadsheet (Excel, Calc, ...)

function:

Public Function ArrondiVersInfinis (ByVal Number, Optional ByVal Decimal = 0)'Rounding function to the infinite: the higher figureArrondiVersInfinis = (Fix (Number * 10 ^ Decimal) _+ IIf (Number = Fix (Number Decimal * 10 ^), 0, Sgn (Number))) / 10 ^ DecimalEnd Function

stochastic rounding

This is one of the methodsrounded to the nearest, used in statistics, for which the median value is rounded randomly or pseudo-randomly to positive or negative infinity infinity.

For example: 3.5 randomly rounded to 3 or 4

The stochastic rounding reduces the cumulative bias that may result from rounding other methods, but the randomness makes it almost impossible replication of results.

function:

Public Function ArrondiStochastique (ByVal Number, Optional ByVal Decimal = 0)'Before calling the function, execute Randomize in the calling code.'Function stochastic rounding: closer, 0.5 random up or downArrondiStochastique = (Fix (Number * 10 ^ Decimal + Sgn (Number) * 1/2) _- IIF (Number * 10 ^ Decimal - Int (Number * 10 ^ Decimal) <> 1/2, 0, _Int (Rnd * 2) * Sgn (Number))) / 10 ^ DecimalEnd Function

alternate rounding

This is one of the methodsrounded to the nearest, used in statistics, for which the median value is rounded alternately to positive infinity then to negative infinity (or vice versa).

For example: 3.5 to 4 and 2 to 2.5 or 2.5 to 3 and 3.5 to 3

The alternating rounding reduces the cumulative bias that may result from other rounding methods, unlike the stochastic rounding, replication of results is often possible.

function:

Public Function ArrondiAlterne (ByVal Number, Optional ByVal Decimal = 0)Static flgDown As Boolean'Function Alternate Rounding: closer, 0.5 alternated up and downIf Number * 10 ^ Decimal - Int (Number * 10 ^ Decimal) = 1/2 ThenArrondiAlterne = IIf (flgDown Xor Sgn (number) = -1, Sgn (Number), 0)flgDown = Not flgDownEnd IfArrondiAlterne = (Fix (Number * 10 ^ Decimal + Sgn (Number) * 1/2) - ArrondiAlterne) _/ 10 ^ DecimalEnd Function

almost full text of Patrice33470

Keywords: rounded Round Round () VB bug
VBA.VB6 - Rounding functionsDownload this article (PDF)
Original article published by. Translated by lermite222. This document entitled " VBA.VB6 - Rounding functions "coming fromHow it works (Www.commentcamarche.net)is made available under the Creative Commons license.You can copy, modify copies of this page, under the conditions stipulated by the license,as this note appears clearly.download this article (PDF