Negate: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(Created page with ":''This page describes the monadic arithmetic function. For logical negation of a single argument, see Not. For dyadic subtraction (minus), see Subtract.'' {{Built-in...")
 
m (Text replacement - "<source" to "<syntaxhighlight")
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
:''This page describes the monadic arithmetic function. For logical negation of a single argument, see [[Not]]. For dyadic subtraction (minus), see [[Subtract]].''
:''This page describes the monadic arithmetic function. For logical negation of a single argument, see [[Not]]. For dyadic subtraction (minus), see [[Subtract]]. For the negative sign of a number, see [[High minus]].''


{{Built-in|Negate|-}}, or '''Minus''', is a [[monadic]] [[scalar function]] which returns the [[wikipedia:additive inverse|additive inverse]] of its argument. It shares a [[glyph]] <source lang=apl inline>-</source> with [[Subtract]], which may also be called Minus, and may be considered a case of Subtract with a default left argument of zero.
{{Built-in|Negate|-}}, or '''Minus''', is a [[monadic]] [[scalar function]] which returns the [[wikipedia:additive inverse|additive inverse]] of its argument. It shares a [[glyph]] <syntaxhighlight lang=apl inline>-</syntaxhighlight> with [[Subtract]], which may also be called Minus, and may be considered a case of Subtract with a default left argument of zero.


The [[function]] Negate is distinguished from the syntactic [[negative]] sign, which is not a function but rather part of [[numeric literal]] notation. APL uses the [[high minus]] <source lang=apl inline>¯</source> for the negative sign, but [[K]] uses the same symbol <source lang=apl inline>-</source> as Minus, treating a <source lang=apl inline>-</source> immediately preceding a numeric literal with no spaces as a negative sign.
The [[function]] Negate is distinguished from the syntactic [[negative]] sign, which is not a function but rather part of [[numeric literal]] notation. APL uses the [[high minus]] <syntaxhighlight lang=apl inline>¯</syntaxhighlight> for the negative sign, but [[K]] uses the same symbol <syntaxhighlight lang=apl inline>-</syntaxhighlight> as Minus, treating a <syntaxhighlight lang=apl inline>-</syntaxhighlight> immediately preceding a numeric literal with no spaces as a negative sign.


== Examples ==
== Examples ==


Negating a vector of positive numbers. Note that the result array is displayed with a [[high minus]] on each element rather than a single Negate. The high minus applies to individual [[element]]s while Negate can only negate an entire array.
Negating a vector of positive numbers. Note that the result array is displayed with a [[high minus]] on each element rather than a single Negate. The high minus applies to individual [[element]]s while Negate can only negate an entire array.
<source lang=apl>
<syntaxhighlight lang=apl>
       - 1 2 3
       - 1 2 3
¯1 ¯2 ¯3
¯1 ¯2 ¯3
</source>
</syntaxhighlight>
Negate works on every type of number present, including [[complex number]]s.
Negate works on every type of number present, including [[complex number]]s.
<source>
<syntaxhighlight lang=apl>
       - ¯2.5 1e20 3j¯4
       - ¯2.5 1e20 3j¯4
2.5 ¯1E20 ¯3J4
2.5 ¯1E20 ¯3J4
</source>
</syntaxhighlight>
 
== Precision ==
 
Unlike [[Subtract]], Negate cannot lose precision in floating-point calculations. This is because floating-point representations have a sign bit indicating whether a value is positive or negative; negating a number only changes the sign bit. Floating-point formats always use a sign bit because they distribute numbers exponentially. But only positive numbers can be represented as the result of an exponent, so an additional sign bit is needed.
 
For integers, which are always stored in [[wikipedia:two's complement|two's complement]] format on modern computers, Negate forces a conversion to a higher type when passed the smallest possible value: for example, the range of 1-byte integers is from <syntaxhighlight lang=apl inline>¯128</syntaxhighlight> to <syntaxhighlight lang=apl inline>127</syntaxhighlight> inclusive, so <syntaxhighlight lang=apl inline>-¯128</syntaxhighlight> requires a 2-byte integer to store. Similarly, negating a [[Boolean]] array (that is, 1-bit unsigned values) forces it to be converted to an integer array. These conversions cannot cause a loss of precision in any normal APL because the result is a power of two, and is exactly representable in floating-point format. In [[K]], which wraps on integer overflow, there is also no possibility of such a loss of precision because the minimum 4-byte integer is used as a null value, making the integer range symmetric.
 
== See also ==
* [[Signum]]
* [[Conjugate]]
* [[Reciprocal]]


== External links ==
== External links ==
Line 22: Line 33:
=== Documentation ===
=== Documentation ===


* [http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Negative.htm Dyalog]
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Negative.htm Dyalog]
* [http://microapl.com/apl_help/ch_020_020_030.htm APLX]
* [http://microapl.com/apl_help/ch_020_020_030.htm APLX]
* J [https://www.jsoftware.com/help/dictionary/d120.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/minus NuVoc]
* J [https://www.jsoftware.com/help/dictionary/d120.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/minus NuVoc]
* [https://mlochbaum.github.io/BQN/doc/arithmetic.html#basic-arithmetic BQN]


{{APL built-ins}}
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar monadic functions]]

Latest revision as of 22:05, 10 September 2022

This page describes the monadic arithmetic function. For logical negation of a single argument, see Not. For dyadic subtraction (minus), see Subtract. For the negative sign of a number, see High minus.
-

Negate (-), or Minus, is a monadic scalar function which returns the additive inverse of its argument. It shares a glyph - with Subtract, which may also be called Minus, and may be considered a case of Subtract with a default left argument of zero.

The function Negate is distinguished from the syntactic negative sign, which is not a function but rather part of numeric literal notation. APL uses the high minus ¯ for the negative sign, but K uses the same symbol - as Minus, treating a - immediately preceding a numeric literal with no spaces as a negative sign.

Examples

Negating a vector of positive numbers. Note that the result array is displayed with a high minus on each element rather than a single Negate. The high minus applies to individual elements while Negate can only negate an entire array.

      - 1 2 3
¯1 ¯2 ¯3

Negate works on every type of number present, including complex numbers.

      - ¯2.5 1e20 3j¯4
2.5 ¯1E20 ¯3J4

Precision

Unlike Subtract, Negate cannot lose precision in floating-point calculations. This is because floating-point representations have a sign bit indicating whether a value is positive or negative; negating a number only changes the sign bit. Floating-point formats always use a sign bit because they distribute numbers exponentially. But only positive numbers can be represented as the result of an exponent, so an additional sign bit is needed.

For integers, which are always stored in two's complement format on modern computers, Negate forces a conversion to a higher type when passed the smallest possible value: for example, the range of 1-byte integers is from ¯128 to 127 inclusive, so -¯128 requires a 2-byte integer to store. Similarly, negating a Boolean array (that is, 1-bit unsigned values) forces it to be converted to an integer array. These conversions cannot cause a loss of precision in any normal APL because the result is a power of two, and is exactly representable in floating-point format. In K, which wraps on integer overflow, there is also no possibility of such a loss of precision because the minimum 4-byte integer is used as a null value, making the integer range symmetric.

See also

External links

Documentation


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