# 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 may contain more than one number. If the constant contains spaces, the constant will be a numeric list.

A numeric (constant) is formed using some of these elements:

• the ten digits `0..9` from our decimal system
• the (25) 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 o x b`, as explained below
• spaces between atoms of a constant list

## Integers

Basically numbers composed of digits like `1729` or `_16`.

Some verbs have an integer result and may therefore be used to define an integer constant, but be aware:

```   ] 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. this looks promising,
2
datatype c                   NB. but ...
floating
] d=. !7                     NB. same,
5040
datatype d                   NB. but ...
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
```

Not every number containing a decimal point will necessarily be a float. J represents a constant using the smallest precision that will hold it accurately:

```   5.
5
datatype 5.
integer
5.000
5
datatype 5.000
integer
datatype _2.e3       NB. _2000 in scientific notation
integer
datatype _2*10^3     NB. but ...
floating
```

## 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

### 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.)

• Euler on infinity (German)

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. (xeyi) 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

## 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.998e8       NB. speed of light in vacuum (m/s)
299800000
] 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
```

## 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` (attributed to Charles P Steinmetz, who introduced `j` as a "distinguishing index" (of the vertical component), to later show that it equals the square root of `-1`). 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
```

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
```