|
Chapter 19: NumbersThe topics covered in this chapter are: are:
19.1 Six Different Kinds of NumbersJ supports computation with numbers of these kinds:
19.1.1 Floating-Point NumbersA floating-point number is a number represented in the computer in such a way that: (1) there may be a a fractional part as well as a whole-number part. (2) a fixed amount of computer storage is occupied by the number, whatever the value of the number. and (3)the precision with which the number is represented is limited to at most about 17 significant decimal digits (on a PC). Examples of floating-point numbers are 0.25 2.5 12345678901 We will use the term "real" more or less interchangeably with "floating-point".
19.1.2 Scientific NotationWhat is sometimes called "scientific notation" is a convenient way of writing very large or very small numbers. For example, 1500000 may be written as 1.5e6, meaning 1.5 * 10^6. The general scheme is that a number written in the form XeY, where Y is a (positive or negative) integer means (X * 10^Y).
Note that in 3e2 the letter e is not any kind of function; it is part of the notation for writing numbers, just as a decimal point is part of the notation. We say that the string of characters 3 followed by e followed by 2 is a numeral which denotes the number 300. The string of characters 3 followed by 0 followed by 0 is another numeral denoting the same number. Different forms of numerals provide convenient ways to express different numbers. A number expressed by a numeral is also called a "constant" (as opposed to a variable.) We will come back to the topic of numerals: now we return to the topic of different kinds of numbers. 19.1.3 Booleans and IntegersWhen an array contains only the truth-values 0 and 1, the J system recognises this fact and, for the sake of efficiency, represents the numbers in a compact internal form, called "Boolean", rather than in the floating-point form. Similarly for an array containing only (relatively small) whole numbers, a compact internal representation called "integer" is used. The choice of appropriate representation is managed entirely automatically by the J system, and is not normally something the programmer must be aware of. However, the J system does provide a means of testing the representation of a number. Here is a utility function for the purpose. types =: 'bool';'int';'float';'complex';'ext int';'rational' type =: > @: ({ & types) @: (1 4 8 16 64 128 & i.) @: (3 !: 0)
19.1.4 Comparison of Real Numbersto be supplied 19.1.5 Complex NumbersThe square root of -1 is the imaginary number conventionally called "i". A complex number which is conventionally written as, for example, 3+i4 is in J written as 3j4. In J an imaginary number is always regarded as a complex numbers with real part zero. Thus "i", the square root of -1, can be written 0j1.
A complex number can be built from two separate real numbers by arithmetic in the ordinary way, or more conveniently with the built-in function j. (lowercase j dot, called "Complex").
A complex number such as 3j4 is a single number, a scalar. To extract its real part and imaginary part separately we can use the built-in verb +.(plus dot, called "Real/Imaginary"). To extract separately the magnitude and angle (in radians) we can use the built-in verb *. (asterisk dot, called "Length/Angle").
Given a magnitude and angle, we can build a complex number by taking sine and cosine, or more conveniently with the built-in function r. (lowercase r dot, called "Polar"). sin =: 1 & o. cos =: 2 & o. mag =: 5 ang =: 0.92729522 NB. radians
A complex constant with magnitude X and angle (in radians) Y can be written in the form XarY, meaning X r. Y. Similarly, if the angle is given in degrees, we can write XadY.
19.1.6 Extended IntegersA floating-point number, having a limited storage space in the computer's memory, can represent an integer exactly only up to about 17 digits. For exact computations with longer numbers, "extended integers" are available. An "extended integer" is a number which exactly represents an integer no matter how many digits are needed. An extended integer is written with the digits followed with the letter 'x'. Compare the following:
Here a is an approximation while b is an exact result.
We can see that adding 1 to a makes no difference, while adding 1 to b does make a difference:
19.1.7 Rational NumbersA "rational number" is a single number which represents exactly the ratio of two integers, for example, two-thirds is the ratio of 2 to 3. Two-thirds can be written as a rational number with the notation 2r3. The point of rationals is that they are are exact representations using extended integers. Arithmetic with rationals gives exact results.
Rationals can be constructed by dividing extended integers. Compare the following:
A rational can be constructed from a given floating-point number with the verb x:
A rational number can be converted to a floating-point approximation with the inverse ofx: , that is, verb x: ^: _1
Given a rational number, its numerator and denominator can be recovered with the verb 2 & x:, which gives a list of length 2.
19.1.8 Type ConversionWe have numbers of six different types: boolean, integer, extended integer, rational, floating-point and complex. Arithmetic can be done with a mixture of types. For example an integer plus an extended gives an extended, and a rational times a float gives a float.
The general scheme is that the six types form a progression: from boolean to integer to extended to rational to floating-point to complex. We say that boolean is the simplest or "lowest" type and complex as the most general or "highest" type Where we have two numbers of different types, the one of lower type is converted to match the type of the higher. and the result is of the "higher".
19.2 Special Numbers19.2.1 "Infinity"A floating-point number can (on a PC) be no larger than about 1e308, because of the way it is stored in the computer's memory. Any arithmetic which attempts to produce a larger result will in fact produce a special number called "infinity" and written _ (underscore). For example:
We also have "negative infinity" written as __ (underscore underscore). Infinity is a floating-point number: type _ float 19.2.2 Indeterminate Numbersto be supplied 19.3 NotationsWe have seen above numerals formed with the letters e, r and j, for example: 1e3, 2r3, and 3j4. Here we look at more letters for forming numerals. A numeral written with letter p, of the form XpY means X * pi ^ Y where pi is the familiar value 3.14159265....
Similarly, a numeral written with letter x, of the form XxY means X * e ^ Y where e is the familiar value 2.718281828....
These p and x forms of numeral provide a convenient way of writing constants accurately without writing out many digits. Finally, we can write numerals with a base other than 10. For example the binary or base-2 number with binary digits 101 has the value 5 and can be written as 2b101. 2b101 5 The general scheme is that NbDDD.DDD is a numeral in number-base N with digits DDD.DDD . With bases larger than 10, we will need digits larger than 9, so we take letter 'a' as a digit with value 10, 'b' with value 11, and so on up to 'z' with value 35. For example, letter 'f' has digit-value 15, so in hexadecimal (base 16) the numeral written 16bff has the value 255. The number-base N is given in decimal.
One more example. 10b0.9 is evidently a base-10 number meaning "nine-tenths" and so, in base 20, 20b0.f means "fifteen twentieths" 10b0.9 20b0.f 0.9 0.75 19.3.1 Combining the NotationsThe notation-letters e, r, j ar ad p x and b may be used in combination. For example we can write 1r2p1 to mean "pi over two". Here are some further examples of possible combinations. A numeral in the form XrY denotes the number X%Y. A numeral in the form XeYrZ denotes the number (XeY) % Z because e is considered before r.
A numeral in the form XjY denotes the complex number (X j. Y) (that is, (X + (%: _1) * Y). A numeral in the form XrYjZ denotes the number (XrY) j. Z because r is considered before j
A numeral in the form XpY denotes the number X*pi^Y. A numeral in the form XjYpZ denotes (XjY) *pi^Z because j is considered before p.
A numeral in the form XbY denotes the number Y-in-base-X. A numeral in the form XpYbZ denotes the number Z-in-base-(XpY) because p is considered before b.
19.4 How Numbers are DisplayedA number is displayed by J with, by default, up to 6 or 7 significant digits. This means that, commonly, small integers are shown exactly, while large numbers, or numbers with many significant digits, are shown approximately.
The number of significant digits used for display is determined by a global variable called the "print-precision". If we define the two functions: ppq =: 9 !: 10 NB. print-precision query pps =: 9 !: 11 NB. print-precision set then the expression ppq '' gives the value of print-precision currently in effect, while pps n will set the print-precision to n.
19.4.1 The "Format" VerbThere is a built-in verb ": (doublequote colon, called "Format"). Monadic Format converts a number into a string representing the number with the print-precision currently in effect. In the following example, note that a is a scalar, while the formatted representation of a is a list of characters.
The argument can be a list of numbers and the result is a single string.
Dyadic Format allows more control over the representation. The left argument is complex: a value of say, 8j4 will format the numbers in a width of 8 characters and with 4 decimal places.
If the width is specified as zero (as in say 0j3) then sufficient width is allowed. If the number of decimal places is negative (as in 10j_3) then numbers are shown in "scientific notation"
This brings us to the end of Chapter 19. |
Copyright © Roger Stokes 2000. This material may be freely reproduced, provided that this copyright notice is also reproduced.
last updated 11Jan01