2,951
edits
(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...") |
|||
Line 13: | Line 13: | ||
</source> | </source> | ||
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> | <source lang=apl> | ||
- ¯2.5 1e20 3j¯4 | - ¯2.5 1e20 3j¯4 | ||
2.5 ¯1E20 ¯3J4 | 2.5 ¯1E20 ¯3J4 | ||
</source> | </source> | ||
== 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 <source lang=apl inline>¯128</source> to <source lang=apl inline>127</source> inclusive, so <source lang=apl inline>-¯128</source> 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. | |||
== External links == | == External links == |