4,494
edits
m (Text replacement - "{{APL built-ins}}" to "{{APL built-ins}}Category:Primitive functions") |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
(8 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{Built-in|Add|+}}, '''Plus''', '''Addition''', or '''Sum''', is a [[dyadic]] [[scalar function]] that [[wikipedia:Addition|adds]] numbers together. As a | {{Built-in|Add|+}}, '''Plus''', '''Addition''', or '''Sum''', is a [[dyadic]] [[scalar function]] that [[wikipedia:Addition|adds]] numbers together. As a basic arithmetic operation, Add is subject to the language's [[number]] specification. Add shares the glyph <syntaxhighlight lang=apl inline>+</syntaxhighlight> with the [[monadic]] function [[Conjugate]], and is closely related to [[Subtract]] (<syntaxhighlight lang=apl inline>-</syntaxhighlight>). | ||
== Examples == | == Examples == | ||
< | <syntaxhighlight lang=apl> | ||
1 2 3 + 2 3 4 | 1 2 3 + 2 3 4 | ||
3 5 7 | 3 5 7 | ||
0.1 + 3 4 5 | 0.1 + 3 4 5 | ||
3.1 4.1 5.1 | 3.1 4.1 5.1 | ||
</ | </syntaxhighlight> | ||
== Properties == | == Properties == | ||
Line 14: | Line 14: | ||
Addition is [[wikipedia:Commutative property|commutative]] in almost all number systems. It is [[wikipedia:Associative property|associative]] when performed with no rounding, but is unlikely to be associative when rounded to an inexact precision (see [[#Floating point error|Floating point error]]). | Addition is [[wikipedia:Commutative property|commutative]] in almost all number systems. It is [[wikipedia:Associative property|associative]] when performed with no rounding, but is unlikely to be associative when rounded to an inexact precision (see [[#Floating point error|Floating point error]]). | ||
The [[identity element]] for Add is < | The [[identity element]] for Add is <syntaxhighlight lang=apl inline>0</syntaxhighlight>. The [[inverse]] of the function <syntaxhighlight lang=apl inline>n∘+</syntaxhighlight> or the equivalent function <syntaxhighlight lang=apl inline>+∘n</syntaxhighlight> is <syntaxhighlight lang=apl inline>-∘n</syntaxhighlight>. Using [[Commute]] (<syntaxhighlight lang=apl inline>⍨</syntaxhighlight>), we can write <syntaxhighlight lang=apl inline>+⍣¯1</syntaxhighlight> {{←→}} <syntaxhighlight lang=apl inline>-⍨</syntaxhighlight>. | ||
Since adding a number to itself is equivalent to doubling that number, we can express the ''double'' function as < | Since adding a number to itself is equivalent to doubling that number, we can express the ''double'' function as <syntaxhighlight lang=apl inline>+⍨</syntaxhighlight>. | ||
=== Reduction === | |||
[[Reduction]] with Add gives the sum of the whole list. | |||
== Scalar mapping == | == Scalar mapping == | ||
Line 29: | Line 33: | ||
When using a limited-precision format such as 8-byte floats, the results of addition are rounded in order to fit into the result type. This rounding can cause the results of adding several numbers to be different depending on the order in which they are added, that is, addition fails to be associative: | When using a limited-precision format such as 8-byte floats, the results of addition are rounded in order to fit into the result type. This rounding can cause the results of adding several numbers to be different depending on the order in which they are added, that is, addition fails to be associative: | ||
< | <syntaxhighlight lang=apl> | ||
1e20 + ¯1e20 + 1 | 1e20 + ¯1e20 + 1 | ||
0 | 0 | ||
Line 36: | Line 40: | ||
(1e20 + ¯1e20) + 1 ⍝ Not equivalent | (1e20 + ¯1e20) + 1 ⍝ Not equivalent | ||
1 | 1 | ||
</ | </syntaxhighlight> | ||
Addition of floating-point numbers may also be subject to [[overflow]], resulting in a [[DOMAIN ERROR]] or an [[infinite]] result. | Addition of floating-point numbers may also be subject to [[overflow]], resulting in a [[DOMAIN ERROR]] or an [[infinite]] result. | ||
== See also == | |||
* [[Times]] | |||
* [[Or]] | |||
* [[Union]] | |||
== External Links == | == External Links == | ||
Line 44: | Line 53: | ||
=== Documentation === | === Documentation === | ||
* [ | * [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Add.htm Dyalog] | ||
* [http://microapl.com/apl_help/ch_020_020_020.htm APLX] | * [http://microapl.com/apl_help/ch_020_020_020.htm APLX] | ||
* [https://www.jsoftware.com/help/dictionary/d100.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/plus#dyadic J NuVoc] | * [https://www.jsoftware.com/help/dictionary/d100.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/plus#dyadic J NuVoc] | ||
{{APL built-ins}}[[Category:Primitive functions]] | * [https://mlochbaum.github.io/BQN/doc/arithmetic.html#basic-arithmetic BQN] | ||
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar dyadic functions]] |