See "About This JavaScript Site" in JavaScript Index and Introduction.
The value of an ECMA-3 JavaScript Number is stored as a floating-point IEEE Double, for which see also in and via my page Pascal / Delphi / + Types.
JavaScript provides no direct access to the full bit pattern of a Number. The bit pattern can be discovered indirectly (except for NaN), and presented as required in a String; and the Number can be constructed from the bit string. It is not necessary to handle every bit individually.
One can also get the exact value of a Number, as a decimal string. For example, 1/3 is stored in a Number as the bit pattern 0x3fd5555555555555 the exact value of which is 0.333333333333333314829616256247390992939472198486328125.
Integers (up to ±253) are held exactly. But logical operations on Numbers (with the bitwise operators << >> >>> ~ & ^ | ) use 32 bit integers.
The input value should be range-checked, if necessary.
The following sets array A to represent the value of X as four byte-sized Number values.
A = [] ; J = 4
while (J--) { A[J] = X & 0xFF ; X = X>>8 } // X/=256
Form Base Converters can also be used for binary to/from hexadecimal conversion.
| Type | S | E | F | S: sign; E: biased exponent; F: fractional part of mantissa | ||||
|---|---|---|---|---|---|---|---|---|
| Bits | NaN | ±Infinity | ±0 | Denorm | Normal | |||
| Single | 1 | 8 | 23 | S, 0xff, >0 | S, 0xff, 0 | S, 0, 0 | S, 0, F | S, E, F |
| Double | 1 | 11 | 52 | S, 0x7ff, >0 | S, 0x7ff, 0 | |||
| A Number is of the first sort that it matches Denorm : F is preceded by '0.' ; Normal : F is preceded by '1.' | ||||||||
See also IEEE-754 Calculators by Dr Christopher Vickery.
Code here is for conversion to/from IEEE Single/Double. Conversion to match Single necessarily loses accuracy.
The float code is modularised for minimum repetition. For specific work, functions ...Fwd ...Rev could be in-lined, and screen I/O removed.
The binary is separately computed both as four bytes/words and as Sign Exponent Mantissa. Interconversion would be mere string manipulation.
The float LSB might not always be accurate, though it seems good. The code now allows for NaN and ±Infinity and over-range of Single, and for values needing denormals. Zero is now handled as an extreme denormal. An input Number NaN is deemed to have sign=0 and mantissa=1; every NaN looks the same to JavaScript.
TEST THESE CAREFULLY. I have a Pascal/Delphi program, IEEEBITS in programs/ which does the conversion from Number to Single and Double by type-cast, reliably, for comparison.
Enter a String (number or expression) and press Convert. The String is read into Qty as a Number, and the default conversion back to String is shown in Number. SEM and Bits show Qty as independently converted to binary by the code above (there is a cross-check).
The fields of Bits can be edited.
Then press Reverse (Double is a bit slow for extreme exponents). Complete shows (as a String) the result of converting Bits to a Number; the Value string shows the exact decimal value of Bits, calculated in decimal arithmetic. Check is Value converted to Number and then shown by automatic conversion to String.
Because of the inherent reduction in accuracy, Complete will generally differ from Number. Numbers too small for Single Denormal become zero, and Numbers too large become Infinity. In each case, sign is retained.
Complete should agree with Number.
Try 0.01, 0.06, 0.01+0.06 and 0.07.
ISBN-13 has been introduced to supersede ISBN-10; no new ISBN-10 should be issued after 2006. For a description of ISBN-13, see at isbn-international; thanks to Paul Harper of NM Library for finding that document.
The final character of each Number is a check digit, calculated from the seven, nine or twelve descriptive digits of the Main Part. ISSN and ISBN-10 use corresponding digit-weighting algorithms; that for ISBN-13 differs.
ISBN-10 converts to ISBN-13 by prepending an EAN UCC (generally 978) and recalculating the check digit.
For each form, there are functions below to verify a full ISSN or ISBN and to calculate the check digit (as a one-character string) from the main part. Input to those is free-format, but for ISNCodeTest the check digit must be the final character.
In the following code, 9999 is merely a sufficiently large multiple of 11.
Written on the assumption that ISSN-8 is like ISBN-10, which fits test ISSNs.
Function BigCat is efficiently recursive - Wombat is an iterative equivalent. And EjHCat is cunning. Note the differing behaviour of the various routines when the repeat unit is longer than one character.
N.B. The underlying algorithms are, probably, more or less, the simplest; likewise the actual code. But the code is not necessarily optimum, and the structuring of the details could be rather different in other languages.
More complex routines cacheing intermediate results can be substantially faster.
Recursive functions Combs and Perms combine and permute N elements of Src at a time into Got in all ways. Array All collects the results, for which Function Fn is an example.
Two ways of collecting selections in Got are shown. In Perms character elements are concatenated into a String. In Combs elements are treated as full Objects and pushed onto an Array (but converted into a plus-separated string in Fn for display here).
In these, array-handling efficiency has not been considered.
The set of permutations of "Mississippi" is the set of permutations of sorted "Mississippi" ("Miiiippssss"), and sorting is fast enough.
So first sort the input, then after for (;;) insert code for but only if this element differs from the previous - in other words, where successive available elements are indistinguishable, only one should be a candidate.
In simple usage, an array Arr can be initialised like Arr = [3,"4",5] to have Arr.length = 3 elements Arr[0] Arr[1] Arr[2]. The elements can be of any type and can differ in type; an "index" does not have to be an integer. Actually, an Array is an Object with extra properties.
The .length property of an array is read/write. It does not represent the number of elements currently defined in the array, but is related to the numbering of the elements with non-negative integer indexes.
The constructor new Array(N) sets the length to N (rounded towards zero, then considered as unsigned 32-bit).
When a value is given to an element with a non-negative integer index, and .length is not already greater than that index, JavaScript makes it one greater than that index.
Writing to the .length property drops any elements with integer indexes of that value or above.
String(A) shows just the integer-indexed elements, comma-separated, from zero to .length-1, with undefined elements showing as empty substrings.
Approximately.
These are substitutes for methods provided in current systems but not in MS IE 4.
Note that a truly general-purpose substitute method must be at least one of two things : fully compliant under all circumstances with the ECMA standard, and/or fully compliant under all circumstances with the behaviour of the majority of common browsers.
However, when a substitute method is used on a particular page or site it is only necessary that it complies with the user's expectations in the circumstances in which it can actually be called. For example, it may be guaranteed that the arguments are in the obvious range, that the array is non-empty, etc.
A substitute method that is known to be different should be given a distinct name.
To be tested more? :-
Needs much more testing.