Number

From APL Wiki
Revision as of 22:50, 26 February 2024 by Marshall (talk | contribs) (→‎Numeric types: Dyalog doesn't do complex decfs)
Jump to navigation Jump to search

In APL, a number is a scalar array representing a mathematical number. Numbers and characters were the only possible element types in APL\360 and remain the basic data types in modern APLs. In addition to quantities for computation, they are used to represent Booleans (0 and 1) and indices. Numeric operations may be subject to floating-point rounding and comparison tolerance. Traditionally, APL provides only one user-visible numeric type, so that numbers round identically regardless of how they are represented, but newer APLs may support additional types such as arbitrary-precision integers that behave differently.

Numeric types

Traditionally, APL supports only one numeric type that is visible to the user, such as a floating-point number, or complex number whose components are floating-point. This design choice is sometimes expressed as "a number is a number is a number". It started with APL\360, which uses packed 1-bit Boolean and 4-bit integer arrays for performance reasons but converts them automatically on overflow to give all numbers the semantics of 64-bit HFP floats:

We recognized that the finite limits on the representation of numbers imposed by an implementation would raise problems which might require some compromise in the definition of the language, and we tried to keep these compromises to a minimum. For example, it was clear that we would have to provide both integer and floating point representations of numbers and, because we anticipated use of the system in logical design, we wished to provide an efficient (one bit per element) representation of logical arrays as well. However, at the cost of considerable effort and some loss of efficiency, both well worthwhile, the transitions between representations were made to be imperceptible to the user, except for secondary effects such as storage requirements.[1]

The same principle is reflected in ISO/IEC 13751:2001, which specifies that different representations of a number must be indistinguishable to a conforming program (such a program can call primitives but not implementation-specific system functions).

Several dialects, beginning with SHARP APL in 1981, support complex numbers implemented as pairs of floats. These can be used alongside individual floating-point numbers and other optimized representations because the real numbers are a subset of floating-point numbers. NARS2000 goes further in providing quaternion and octonion support.

Most often the numeric type has the precision of a 64-bit float, although some early implementations used smaller sizes because of resource limitations: for example APL\1130 used 32-bit floats. Modern implementations overwhelmingly use the 64-bit IEEE 754 double because of its support in hardware. While IEEE supports both negative and positive zero, this distinction is usually hidden from the programmer because it's incompatible with integer representations. Dyalog APL also supports 128-bit decimal floats by setting ⎕FR; if a program uses a consistent ⎕FR value and real numbers only then it will see only one floating-point type, but complex numbers with non-zero imaginary part always use binary floats for the components.

Some newer dialects provide additional types that are distinguishable from floats, often by greater precision. Extended-precision integers are one possibility: for example, dzaima/APL provides these if requested with a trailing L in a numeric literal or ⎕BIG, while Kap uses exact rational numbers by default. NARS2000 supports numerous numeric types, and April provides access to the Common Lisp numeric tower, which also includes many types.

APL-derived languages may not follow the principle of a single numeric type. Nial, J, K/Q, I, and Ivy all define integers and floats as separate types and implement them with differing behavior. GNU APL also uses distinguishable 64-bit integers and floats for ordinary execution. Rules for conversion between types may differ with language; for example, addition that overflows a 64-bit integer will wrap in K but promote to a float in J. RAD, BQN, Uiua, and Lil use a single numeric type like APL.

References

  1. Adin Falkoff and Ken Iverson. The Evolution of APL (web). ACM SIGPLAN Notices, Volume 13, Number 8. 1978-08.


APL features [edit]
Built-ins Primitives (functions, operators) ∙ Quad name
Array model ShapeRankDepthBoundIndex (Indexing) ∙ AxisRavelRavel orderElementScalarVectorMatrixSimple scalarSimple arrayNested arrayCellMajor cellSubarrayEmpty arrayPrototype
Data types Number (Boolean, Complex number) ∙ Character (String) ∙ BoxNamespaceFunction array
Concepts and paradigms Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity elementComplex floorArray ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ GlyphLeading axis theoryMajor cell searchFirst-class function
Errors LIMIT ERRORRANK ERRORSYNTAX ERRORDOMAIN ERRORLENGTH ERRORINDEX ERRORVALUE ERROREVOLUTION ERROR