Vocabulary/Constants
Number formation by examples
For a more formal reference have a look at these Dictionary sections
What makes a number?
Any word beginning with a digit (0-9
) or underscore (_
),
and not ending with a colon (:
), is numeric.
A numeric (constant) is formed using some of these elements:
- the ten digits
0..9
from our decimal system - the (26) lower case letters
a..z
, for use as digits with bases other than 10 - the underscore
_
for denoting (by itself) infinity or (in composition) a negative number, therefore - the double underscore
__
for negative infinity - the period
.
used as decimal point (can't start a numeric) - letters (and pairs of letters), namely
e r j ar ad p x b
, as explained below
A numeric constant may contain more than one number; if the constant contains spaces, the constant will be a numeric list.
Integer and Boolean, and overriding default precision
Basically numbers composed of digits like 1729
or _16
.
The values 0 and 1 create Boolean precision. The presence of other digits creates integer precision.
Some verbs have an integer result and may therefore be used to define an integer constant. Some verbs produce other precisions:
] a=. - 3 + _5 NB. integer (btw, execution is right to left) 2 datatype a integer ] b=. 3 * _5 NB. integer _15 datatype b integer ] c=. 10 %: 1024 NB. %: always produces floating precision or higher 2 datatype c floating ] d=. !7 NB. so does ! 5040 datatype d floating f=: verb def '*/ >: i. y' NB. here's an integer factorial f 7 5040 datatype f 7 integer
The negative sign is strictly part of the number (starting it):
_2^4 NB. i.e. (_2)^4, of datatype floating 16 _(2^4) NB. doesn't work; 2^4 is not part of a numeric word |syntax error | _(2^4) -2^4 NB. maybe you meant this, the negated result _16
J uses the smallest precision possible to represent the value given:
2 5 datatype 2 integer datatype 1 boolean datatype _2e3 NB. _2000 in scientific notation integer datatype 100j0 integer
You can override the smallest-precision rule by giving clear indication of the desired precision:
- any constant of more than one digit will never be demoted to boolean
- any real constant containing a decimal point will never be demoted to integer
- any complex constant with a decimal point in the real part will never be demoted to integer
- any complex constant with a decimal point in the imaginary part will never be demoted to floating
- any complex constant containing ar or ad will never be demoted to floating
datatype 01 integer datatype 2. floating datatype 100.j0 floating datatype 100.j0. complex
Floats
Basically numbers composed of digits, with an embedded decimal point, like 3.14
or _0.0667
.
Here are some floating constants (mostly from function calls):
] Phi=. -:>:%:5 NB. golden ratio 1.61803 datatype Phi floating ] phi=. -:<:%:5 0.618034 ] icd=. 1-~10%:2 NB. annual interest for doubling a capital within 10 yrs 0.0717735 NB. is about 7.2% pa pr=. verb def '10^0.1*y' NB. calc power ratio from gain (in dB) ] pf=. pr 3 NB. 3 dB gain 1.99526 NB. means power (output/input) has about doubled ] cs=. 3.4321e2 343.21 ] ua=. 149597870700 NB. oops, looks like an integer, having collided with print precision 1.49598e11 datatype ua NB. but wait a minute, this is a 32-bit OS floating datatype 1495978707 NB. reducing by factor 100, voila! integer
For a discussion of integers, floats and precision problems, see
Extended Integers
The above mentioned mishap can be prevented, using so-called extended integers:
] uaX=. x: 149597870700 149597870700 ] uax=. 149597870700x 149597870700 ] f12=. !12 4.79002e8 datatype !12 floating ] f12x=. !12x 479001600 datatype !12x extended ] p2=. 2^32 4.29497e9 ] p2X=. x: 2x^32 4294967296 ] p2x=. 2x^32 4294967296
Further information on extended integers and functions using them is given in
- J NuVoc (x:) Extended Precision
- Dictionary section G. Extended and Rational Arithmetic
- Roger Hui's Essay 'Extended Precision Functions'
Infinity
Infinity is of type floating, is a regular number (and deals with some special cases in a reasonable fashion preventing application crashes):
datatype _ floating datatype __ floating 1%_ 0 1%__ 0 (1%_) -: (1%__) 1 1%0 _ 0%0 0
(This follows in part the reasoning of Leonhard Euler more than twohundred years ago.)
The question, what to make of 0%0
, was answered differently in APL and J – see E.E. McDonnell's paper 'Zero Divided by Zero'.
The Hierarchy of Letters, Listing All Constant Types
Each letter splits the constant into the left value (to the left of the letter) and the right value (to the right of the letter).
Letter | Class | Meaning | What may appear in the left value (referred to as x) | What may appear in the right value (referred to as y) |
b | Custom base | x is the base, y indicates the value | Any number not using b, and not indicating extended precision (i. e. ending with x). A rational base is converted to floating-point. | Digits or lowercase letters. Letters have values 10-35 (a=10, b=11, etc.) |
x | Mathematical exponentials | Exponential form with base e. The value of the constant is x*e^y. | Any number not using b, p, or x, and not indicating extended precision (i. e. ending with x). A rational base is converted to floating-point. | Any number not using b, p, or x, and not indicating extended precision (i. e. ending with x). A rational exponent is converted to floating-point. |
p | Exponential form with base π. The value of the constant is x*π^y. | Any number not using b, p, or x, and not indicating extended precision (i. e. ending with x). A rational base is converted to floating-point. | Any number not using b, p, or x, and not indicating extended precision (i. e. ending with x). A rational exponent is converted to floating-point. | |
j | Complex numbers | a+bi form: the value of the constant is (x j. y), i. e. (x + yi) | Any number not using b, p, x, j, ar, or ad, and not indicating extended precision (i. e. ending with x). A rational value is converted to floating-point. | Any number not using b, p, x, j, ar, or ad, and not indicating extended precision (i. e. ending with x). A rational value is converted to floating-point. |
ar | angle-in-radians form. The value of the constant is (x * ^ j. y), i. e. (xe^{yi}) | Any number not using b, p, x, j, ar, or ad, and not indicating extended precision (i. e. ending with x). A rational value is converted to floating-point. | Any number not using b, p, x, j, ar, or ad, and not indicating extended precision (i. e. ending with x). A rational value is converted to floating-point. | |
ad | angle-in-degrees form. The value of the constant is (x * ^ 180p_1 %~ j. y), i. e. (xe^{πyi/180}) | Any number not using b, p, x, j, ar, or ad, and not indicating extended precision (i. e. ending with x). A rational value is converted to floating-point. | Any number not using b, p, x, j, ar, or ad, and not indicating extended precision (i. e. ending with x). A rational value is converted to floating-point. | |
r | Extended-precision rational | The value of the constant is x % y. If x or y contains a decimal point or scientific notation, the constant will have integer or floating-point precision, otherwise rational precision | A possibly signed number that may contain a decimal point or scientific notation | A possibly signed number that may contain a decimal point or scientific notation |
e | Scientific notation | The value of the constant is x * 10 ^ y | A possibly signed number that may contain a decimal point | A possibly signed integer |
x as suffix | Extended-precision numeric | x is the value | Digits 0-9 only, possibly signed | N/A |
fq as suffix | Long floating-point value | x is the value | Digits 0-9 only, possibly signed | N/A |
Scientific Notation (e)
You probably have come across 'E-Notation' on a (pocket) scientific calculator. This way of writing numbers is found in areas where people deal with either rather small or rather large quantities (like in chemistry, particle physics or astronomy). In standard form, some leading digits are given (depending on the precision needed) with one digit in front of the decimal point, and combined with a power-of-ten section which indicates order of magnitude: s × 10^n
where 1 ≤ |s| < 10
.
The concept is found across many programming languages; in J, it is written as compound sen
(no spaces), where s
may be any float, the lower case letter e
is used to signal the exponent part, and the only restriction is that the exponent n
be an integer.
] cee=. 4.01e4 NB. Earth's (equatorial) circumference (km) 40100 ] cl=. 2.99792458e8 NB. speed of light in vacuum (m/s) 2.99792458e8 ] mn=. 1.675e_27 NB. neutron mass (kg) 1.675e_27 _21.4e_3 NB. first underscore indicates a negative number, second is part of the OoM _0.0214 4.1 e 5 NB. there should be no spaces |value error: e | 4.1 e 5 4.1 e5 |syntax error | 4.1 e5 4.1e 5 |ill-formed number 1e.5 NB. exponent should be integer |ill-formed number 1e1e2 NB. 'nesting' not defined |ill-formed number
Rationals (r)
Rational numbers are represented in J in the form xrx where x is an (extended) integer and r is the separator implying division of the first integer by the second, e.g. 1r2 is one-half.
The integers are extended, i.e. unlimited precision by default, but do not require the x suffix that extended integers do in isolation. So we could represent pi to 26 places past the decimal like this:
314159265358979323846264338r100000000000000000000000000 157079632679489661923132169r50000000000000000000000000 (1p1,o.1)-314159265358979323846264338r100000000000000000000000000 NB. "1p1" and "o.1" both evaluate to pi as a floating-point number. 0 0
We see here that J will display a rational in its lowest term representation.
80r100 4r5
Rationals are handy for representing fractions that do not have finite decimal equivalents, e.g. 1r3 for one-third.
22j20 ": 1r3 NB. Display decimal representation to 20 digits past decimal point 0.33333333333333333333 22j20 ": 1%3 NB. Floating-point number showing imprecision past 16 digits 0.33333333333333331483
Here is an essay showing one use of rationals.
Transcendentals (p and x)
Transcendental numbers based on π (pi) and e (base of natural log) are specified using the infix qualifiers p and x, respectively.
So, π is 1p1 and π^2 is 1p2. Similarly, e is 1x1 and e^2 is 1x2; 2π is 2p1 and 2e is 2x1. The inverses of π and e are as one would expect:
1p_1 0.31831 1x_1 0.367879
If you wanted to specify e^π, you might think 1x1p1 would work but it doesn't as it's an invalid specification. However, you could specify it this way:
1x3.141592653589793 23.1407 2.718281828459045 ^ 3.141592653589793 23.1407
Along these lines, if you wanted to evaluate part of Euler's identity, you could write this to give an answer accurate within the limits of floating-point representation:
1+1x0j3.141592653589793116 NB. 1+e^πi 0j1.22465e_16 NB. ≈ 0
The math stack exchange presents some examples where powers of pi have been relevant.
Foreign Bases (b)
J supports numbers represented in bases other than 10 (where 10 is the number of digits most people have on their hands, or 5+5
), using b to separate the base (on the left) from the representation (on the right).
The base can be any arbitrary number.
The representation can use any sequence of digits where digits represent integers in the range 0 through 35. Digits with values greater than 9 are represented using letters of the alphabet, with a
representing 10, and z
representing 35. Thus, letters to the right of the b do not take on the significance they might otherwise have. 1000b2e3 represents 2014003, for example. The decimal point character is respected in the sequence to the right of b, as is a leading minus sign (_), though "decimal" in this context is analogous rather than literal.
Frequently used bases include 2, 8 and 16. But this notation can be used to represent the evaluation of many simple polynomials.
Complex (j)
Complex numbers are basically pairs of real numbers (coordinates) in the complex plane. Of these rectangular components, one is called the real part, the other the imaginary part. To distinguish between the two, the latter gets sort of a "marker".
In the Mathematics literature, to specify the imaginary part, the letter i
is used throughout. On the other hand, almost all textbooks in Electrical Engineering and allied fields use the letter j
(suggested by Charles Proteus Steinmetz, who introduced j
as a "distinguishing index" (of the vertical component), to later show that it equals the square root of -1
).
- [ Source: CP Steinmetz, Theory and Calculation of Alternating Current Phenomena, New York, 1897, §§26-27. ]
This avoids confusions with e.g. i
used for (induced) current. J goes along with that, as i.
is already taken for indexing purposes.
In J, the compound ajb
defines a complex number a+j*b
with real part a
and imaginary part b
.
}. > p. 1 0 1 NB. roots of x^2+1 = 0 being (+j) and (-j) 0j1 0j_1 }. > p. 8 0 0 1 NB. 1j1.73205 as the principal cube root of _8 1j1.73205 1j_1.73205 _2 _8^1r3 1j1.73205 -8^1r3 NB. Negate (-) is not part of a number _2
Complex numbers may be constructed
- dynamically - using the Primitive
j.
and accepting numbers and expressions as arguments, or - statically - using the compound format which accepts only constants.
Using the Primitivo
Verb j.
has monadic or dyadic use:
j. 2 3 5 7 NB. monadic use: creating an imginary number, 0j2 0j3 0j5 0j7 0j1 * 2 3 5 7 NB. (j.) being equivalent to the imaginary unit (0j1) 0j2 0j3 0j5 0j7 x=. 1 2 3 4 y=. 2 3 5 7 x j. y NB. dyadic use: creating a complex number by adding a real part 1j2 2j3 3j5 4j7 (%:3) j. ^.2 NB. complex number (pair of real numbers from expressions) 1.73205j0.693147 1 j. 4 NB. watch the spaces between the verb and its arguments, or 1j4 1j.4 NB. you'll end up with a different number than intended 1j0.4
Verb j.
will accept complex arguments as well:
j. 2j3 NB. product (0+j*1)*(2+j*3) _3j2 NB. may be interpreted as a vector rotation by π/2 rad (or +90 deg); j.(^:4) 2j3 NB. 4*π/2 means a full rotation (resulting in an identical vector) 2j3 2j4 j. 3 NB. imaginary part gets augmented 2j7 2j4 j. 2j3 NB. calculates the sum (2+j*4)+(-3+j*2) _1j6
Verb j.
will also accept transcendental arguments:
1p1 j. 1x1 NB. complex number (π+j*e) 3.14159j2.71828
Using the Compound
Besides Reals, the compound ajb
may have parts of Rationals or numbers in Scientific Notation:
1r2j2r3 NB. complex number (1/2+j*2/3) 0.5j0.666667 _2e3j2e_3 NB. complex number (-2*10^3+j*2*10^-3) _2000j0.002
However, when using the compound format only one transcendental component is accepted:
1p1j1x1 NB. not permitted |ill-formed number
Within the compound the transcendental part takes preference.
Real part is trancendental:
1p1 j. 2 NB. complex number (π+j*2), but ... 3.14159j2 1p1j2 NB. complex number π^(1+j*2) = π*cos(2*log(π))+j*π*sin(2*log(π)) _2.06836j2.36463 1x1j2 NB. complex number e^(1+j*2) = e*cos(2)+j*e*sin(2) _1.1312j2.47173
Imaginary part is transcendental:
2 j. 1p1 NB. complex number (2+j*π), but ... 2j3.14159 2j1p1 NB. complex number (2+j*1)*&pi: = (2*π+j*π) 6.28319j3.14159 3j2x1 NB. complex number (3+j*2)*e = (3*e+j*2*e) 8.15485j5.43656 1j1x2 NB. complex number (1+j*1)*e^2 = (e^2+j*e^2) 7.38906j7.38906
In case your project or application is data-type sensitive, you should take a second look (as usual with J):
datatype 1 j. 0 NB. using the verb complex datatype 2 j. 0 complex datatype 1.41 j. 0 complex datatype (%:2) j. 0 complex datatype 1r2 j. 0 complex datatype _2e3 j. 0 complex datatype 1j0 NB. using the compound boolean datatype 2j0 integer datatype 1.41j0 floating datatype 1r2j0 floating datatype _2e3j0 integer
Ordered Pairs
At times, the compound *j*
serves as a vehicle or container for couples (ordered pairs) of values in a more general way.
Compound Arguments to Primitives
Some primitives may be served with complex arguments as a way (seen convenient) of putting two (real) numbers into one atom:
- Format: field width, number of decimals
wjd
- Copy: number of copies, number of fills
cjf
- Steps: interval or range, number of steps
rjs
%: >: i.4 1 1.41421 1.73205 2 6j2 (":) %: >: i.4 NB. using Format (":) 1.00 1.41 1.73 2.00 2^ i. _5 16 8 4 2 1 2j0 0 3j2 1 4j1 (#) 2^ i. _5 NB. using Copy (#) 16 16 4 4 4 0 0 2 1 1 1 1 0 (i:) _5j8 NB. using Steps (i:) 5 3.75 2.5 1.25 0 _1.25 _2.5 _3.75 _5
(See the jdot page for more examples.)
Compound Use in Applications
Somehow related and mentioned for the sake of completeness:
- 2D Coordinates:
The Plot package (and some user applications) make use of the *j*
format in defining surface co-ordinates (x;y)
; e.g. these lines
require 'plot' plot _3j_1 5j7
will result in the graph of an ascending line starting at (-3;-1)
and ending at (5;7)
; while this code snippet
require 'plot' ]f=. (i: 2j10) j. 1 NB. defining 11 points _2j1 _1.6j1 _1.2j1 _0.8j1 _0.4j1 0j1 0.4j1 0.8j1 1.2j1 1.6j1 2j1 ]g=. _1j2 1j_1r4 NB. defining two more points _1j2 1j_0.25 'type dot; pensize 2' plot ;f,g NB. plotting all 13 points
will display 11 equally spaced points on a horizontal line starting with (-2;1)
and ending with (2;1)
, with two isolated ones (one above and one below that line).
Booleans
In strongly typed languages (like e.g. ADA) we have boolean constants TRUE
and FALSE
. In J, by contrast, integers 0
and 1
double as booleans to grant maximum freedom in code development; context will decide on whether a boolean or a number is used/expected, which in turn allows the use in logical operations as well as in calculations.
] Phi=. -: >: %: 5 NB. golden ratio 1.61803398875 ] phi=. -: <: %: 5 0.61803398875 % Phi NB. reciprocal 1/Phi 0.61803398875 phi = % Phi NB. (tolerant) comparison "Does phi equal 1/Phi?" creates boolean value 1 S=. 'LLHLHLH' NB. binary number as High/Low gate states 'H' = S NB. comparison "Is Hi state present?" creates a boolean list 0 0 1 0 1 0 1 #. 'H' = S NB. which may then be converted into decimal representation 21 ] v=. <. 0.1 * 19 ?. 100 NB. sample data 4 9 2 6 4 4 1 0 6 6 7 8 5 8 6 9 7 8 1 m=: +/ % # NB. (verb) arithmetic mean m v NB. mean of data set 5.31579 v < <: m v NB. comparison "Which values are lower than (m-1)?" 1 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 +/ v < <: m v NB. summing the boolean list: there are 7 of those 7
Further reading
Special topics are explored in the ancillary pages listed at the end of NuVoc. Take a look at page Vocabulary/Nouns, which goes into topics mentioned here.
See also: Guides/Constants and Vocabulary/Words.