Vocabulary/numberco

From J Wiki
Jump to: navigation, search

>> <<   Down to: Dyad   Back to: Vocabulary Thru to: Dictionary

#: y Antibase 2

Rank Infinity -- operates on x and y as a whole -- WHY IS THIS IMPORTANT?



Returns the binary expansion of a given number y as a Boolean list

   #: 21
1 0 1 0 1

If y contains 4 atoms (integers) then returns 4 numerals, as a table of 4 rows

   #: 21 10 31 7
1 0 1 0 1
0 1 0 1 0
1 1 1 1 1
0 0 1 1 1

Common uses

1. Decimal to binary conversion (as above).

2. Detect an odd number

But see below for a better method

   isOdd=: 13 : '{: #: y'   NB. return final digit {: of binary expansion #: of y
   isOdd 22
0
   isOdd 23
1

Related Primitives

Base 2 (#. y)


Details

1. Each atom is converted separately, and the results are collected into a result using the frame with respect to 0-cells. This is as if the verb had rank 0, except that the number of places in each result is enough to hold the longest result.

If the verb had rank 0, fill would be added on the right, which would change the binary values.

2. If y is negative, it is converted in two's complement form, and the number of places required is the number that would be needed to hold |y.

   #: 7 _3
1 1 1
1 0 1

3. If y is not an integer, the fractional part is added to the least-significant digit of the result.


Oddities

1. If y is a negative floating-point number tolerantly equal to an integer, the result loses a bit.

   #: _2
1 0
   #: - 2.1-0.1
0


x #: y Antibase

Rank 1 0 -- operates on lists of x, and individual atoms of y -- WHY IS THIS IMPORTANT?



Generalizes the action of (#: y) to other bases than 2 (including mixed bases)

   2 #: 21                  NB. A single atom: 2 shows only the final binary digit
1
   2 2 2 2 2 #: 21          NB. 5 atoms in x gives 5 binary digit in result
1 0 1 0 1
   2 2 2 2 2 #: 21 10 31 7
1 0 1 0 1
0 1 0 1 0
1 1 1 1 1
0 0 1 1 1

Common uses

x #: y is used only when you need to state how many places you want in the result, or if x contains differing values. If you want just sufficient places to hold the value of y in the base x, use  #.inv to convert to a fixed base.

#.inv is the same as  #.^:_1 .

inv is a

  • Standard Library word(s) residing in the 'z'-locale.
  • Defined in the factory script stdlib.ijs.
  • View definition(s) by entering in the J session:  open'stdlib'

1. Convert number to list of decimal digits

   (8#10) #: 90801
0 0 0 9 0 8 0 1
   10 #.inv 90801   NB. just sufficient places
9 0 8 0 1

2. Convert y in seconds to time-interval in (hours,minutes,seconds)

   HMS=: 24 60 60 & #:
   HMS 86399
23 59 59

3. Convert a number to quotient/remainder form

   0 5 #: 26        NB. Quotient and remainder after dividing by 5
5 1
   0 5 #: _7
_2 3

4. Detect an odd number

   isOdd=: 2 & #:
   isOdd 21
1
   isOdd 21 10 31 7
1 0 1 1

Related Primitives

Base (x #. y)


More Information

1. To remember which is which, note that #. (whose inflection is a single dot) produces an atom. Whereas #: (multiple dots) produces a list.


Details

1.  x #: y produces a result with the shape of x. To get a varying number of digits, use #.^:_1 .

2.  x #: y starts with the last atom of x, and calculates  ({:x) | y. This remainder becomes the last place of the result. The integer part of the quotient is passed to the next-last atom of x, and another remainder calculated. The sequence of remainders is the result.

To be precise, the value passed to the next atom is (y-remainder)%x. If the quotient was tolerantly close to an integer, the remainder will be set to exactly 0, and a small non-integer part will be passed to the next digit.

3. It follows that the value in each place of the result is between 0 and the corresponding atom of x, with equality possible only on the 0 side.

   _2 60 60 #: 14399
_1 59 59
   _2 60 60 #: 14399
_1 59 59
   _2 _60 60 #: 14399
0 _1 59

4. An exception to the above: an atom of x that is 0 puts no limit on the result value in that place (and all higher places will have result 0).

An infinite value has the same effect as a 0.

   24 60 60 #: 132400   NB. Limited to a 24-hour range
12 46 40
   0 60 60 #: 132400  NB. No limit
36 46 40
   24 0 60 #: 132400
0 2206 40

5. The residue calculation imbedded in x #: y is tolerant. For intolerant comparison use x #:!.0 y .

   10 10 #: (9.99999999999999)   NB. Close to 10
1 0
   10 10 #:!.0 (9.99999999999999)  NB. But not exactly
0 10

6. Tolerant comparison does not round the argument - it merely transfers the error to the next-higher place

   ":!.(16)  10 10 #:!.0 (9.99999999999999)   NB. Intolerant #: leaves all fractional parts in lowest place
0 9.999999999999989
   ":!.(16)  10 10 #: (9.99999999999999)   NB. Tolerant may move them to higher places
0.9999999999999989 0
   ":!.(16)  10 10 #:!.0 (10.0000000000001)
1 9.947598300641403e_14
   ":!.(16)  10 10 #: (10.0000000000001)
1.00000000000001 0

Use These Combinations

Combinations using x #: y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Integer [quotient/]remainder of power of 2 integer x #: y with x in the form (0,power of 2)
Odometer function (y gives the size of each wheel; result is all readings from 0 0 0 0 to <: y) integer (#: i.@(*/)) y @: in place of @