- 1 Number formation by examples
Number formation by examples
Citing posts from the J Programming Forum:
A word is numeric if and only if it begins with 0-9 or _
Within a numeric word, zero is not required before a decimal point.
1e.4 is not allowed because the exponent must be an integer.
– Henry Rich (2017-07-01)
Thinking that all these years (decades?) I could have written _.5 instead of _0.5!
I was wondering if anyone had used that form before in actual code and I searched the forum posts; apparently, although there are pleanty of instances of _0.5 nobody has used _.5 in actual code, at least in the forums, with one possible exception.
Learning J is akin to reading an interesting book which it seems it never ends. I learned something useful today.
– José Mário Quintana (2017-07-02)
This page is meant as an informal introduction aimed at beginners (like myself); for a more formal reference have a look at these Dictionary sections
The examples have been gained from browsing the Forums, reading chapters of Books on J, and from experimenting.
What makes a number?
A numeric (constant) is formed using some of these elements:
- the ten digits
0..9from our decimal system
- the (25) lower case letters
a..z, for use as digits with foreign bases (> 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
Basically numbers composed of digits like
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 datatype !7 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 neccessarily be a float:
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
Basically numbers composed of digits, with an embedded decimal point, like
Here are some floating constants (mostly from function calls):
] Phi=. -:1+%:5 NB. golden ratio 1.61803 datatype Phi floating ] phi=. -:1-~%: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
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 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
|Header A||Header B||Header C|
|Level 1||Level 2||Level 3|
|Level a||Level s (this is optional stuff)||Level d|
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
Transcendentals (p and x)
Foreign Bases (b)
In strongly typed languages (like e.g. ADA) we have boolean constants
FALSE. In J, by contrast, integers
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
Special topics are explored in the ancillary pages listed at the end of NuVoc. Take a look at page Vocabulary/Xxxxx, which goes deeper into topics mentioned here.