Guides/Constants

From J Wiki
Jump to: navigation, search

J has a rich constant description syntax. J's constant notation is a sublanguage in its own right.


Useful examples:

1r3 1/3 exactly (rational number) rational
1.1r3.3 0.3333... floating point approximation of 1.1/3.3=1/3 floating point
1r7 1/7 another exact representation rational
1r7p0 0.142857... = floating point approximation of 1/7 floating point
1p1 π floating point
2r3p1 {2\over 3} \pi floating point
180p_1 {180\over\pi} = degrees in radian floating point
1r180p1 {\pi\over 180} = radians in degree floating point
0j2p1 2\pi i = exponent coefficient in Fourier transformation complex
_ \infty, infinity floating point
__ -\infty, negative infinity floating point
_r1, 1r0 \infty, extended infinity, compare

   _>10^1000x
0
   1r0>10^1000x
1

extended
2b10101010 10101010_2 = AA_{16} = 170 integer
16baa same integer
1 0 1 0 1 0 list of bits

Not so useful examples:

7e1p_2j3e_2b_9j3x1e8 exercise to the reader
1x0j1p1 If this worked, it would demonstrate Euler's equation (i.e. _1 = ^ 1p1 * 0j1), but it doesn't, because 'x' and 'p' are on the same level of the Constant Heirarchy

Delimited Pseudo Constants

Often it's hard to type in and read large values and in user interface they use thousand delimited syntax, such as 1,234.56. The 0&". monad handles this nicely. Here are a few examples:

   0".'1,000.25'
1000.25
   0".'1,000,123'
1000123
   0j15":0".'3.14159,26535,89793'
3.141592653589793
   0".'1,234 5,678.9'
1234 5678.9
   #:0".'2b1000,1000,1000,1000'
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0

Notes:

  • Aside from extended precision numbers (and even in many of those cases), any value you can create using the sublanguage of constants, you can also create using primitives. For example, 12j14 can be expressed as 12 j. 14, 12x14 is equivalent to 12 * ^ 14, and, similarly, 12p14 is identical to 12 * 14 ^~ o. 1. Rational numbers can be created using extended precision numbers in combination with %. Because in-line constants are small, efficiency should not be an issue. And, since the results are identical, choosing between the direct, constant form and the indirect, executed form is a matter of readability and maintainability. This is a matter of PersonalTaste.
  • J constants are created with the simplest possible type, regardless of the symbols used create them, so 1.0 is not real; it is not even an integer; it is a boolean. Only extended and rational constants force explicit type conversions.

To demonstrate:

	   nums =. '1 1.0 1r1 1j0 1p0 1e0 1x0 16b1 1x'
	   require 'strings'
	   (,. ' is '&,"1)&:>/  <@:>"1  (,: datatype@:".&.>) ' ' cut nums
	1    is boolean
	1.0  is boolean
	1r1  is extended
	1j0  is boolean
	1p0  is boolean
	1e0  is boolean
	1x0  is boolean
	16b1 is boolean
	1x   is extended
  • The semantics of arbitrary width numbers (extended and rationals) are different from the semantics of the fixed-width numbers (which reflect the underlying machine implementation).
   16^16
1.84467e19
   16^16+0x
18446744073709551616
  • Though it isn't spelled out in the language of the Dictionary, some constants with notational mixture form illegal numbers. Example:
1r1 1x OK
1j1 1x Not OK. Probably because J doesn't support extended precision complex numbers (and given the first bullet above, the x forces extended precision, so the intepreter can't default the constant to 1j1 1j0).
16bffff 1x Not OK. This makes some sense because an 'x' suffix in a based number is ambiguous. The trailing x could mean "convert to extended precision" or it could mean "the digit 'x' in base N" (all digits are valid in all based numbers, see below). But if the numeric lexer were slightly smarter, this could be resolved.
  • When using 'based' constants, be aware that:
  1. . You cannot use uppercase letters. For example, 16bFFFF is an ill formed number
  2. . Digits larger than the base are permissible. 2b1010 2b2020 and 16bzyxwv are acceptible numbers; the base portion only specifies the radix, not the interpretation of the digits, which are always evaluted in base 36.
  3. . While the base portion of a constant can be arbitrarily large, the value portion is restricted to using 36 digits (10 numerals + 26 letters), so the notation cannot specify all numbers in bases larger than 36.
  4. . The Dictionary sentence Moreover, digits with a trailing x denote an extended precision integer is poorly phrased, because 16b000x has digits with a trailing x (but is not entirely composed of them), and is not extended -- it's integer.
  5. . Assuming a single, well formed, based constant, the evaluation is:
   split  =. (  (}.~ >:) ; {.~  )  i.&'b'
   digits =. '0123456789abcdefghijklmnopqrstuvwxyz'&i.&.>  @:  split
   baseN  =. #.~&:>/  @:  ,&(<10)  @:  digits

   baseN '16bffff'
65535
   See Parse, Lex, Execute
  • J internal types has information on J's internal representation of the various types of numbers (and other data).