Round

From APL Wiki
Jump to navigation Jump to search

Round (, ) (respectively in TinyAPL and Uiua) is a monadic scalar function that rounds a number. While it doesn't appear in most APLs, its behavior is most commonly replicated with the idiom ⌊0.5+y.

Examples

      ⸠0.1‿2.9
⟨0 ⋄ 3⟩
Works in: TinyAPL

Variants

In traditional mathematics and programming, there are multiple definitions of rounding a number when its fractional part is one half.

The traditional APL expression always rounds halves up, and TinyAPL also chooses this behavior:

      ⸠0.5‿1.5‿¯0.5‿¯1.5
⟨1 ⋄ 2 ⋄ 0 ⋄ ¯1⟩
Works in: TinyAPL

Another way is to round away from zero, which is equivalent to rounding positive numbers above and negative numbers below. This is chosen by Uiua:

⁅0.5_1.5_¯0.5_¯1.5
## [1 2 ¯1 ¯2]
Works in: Uiua

A third way is rounding to the nearest even integer. This might be preferred because it reduces bias and accumulation of error in subsequent rounding[1]. This is the behavior used by Kap:

      math:round 0.5 1.5 ¯0.5 ¯1.5
0 2 0 -2
Works in: Kap

External links

Documentation

References


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector