Test integerness: Difference between revisions

m
(Added Julia language)
Line 343:
If however large numbers are to be affirmed as integral even if there is no integer variable capable of holding such values, then a different approach is required. Given that a floating-point number has a finite precision, there will be some number above which no digits can be devoted to fractional parts and so the number represented by the floating-point value must be integral, while for smaller numbers the floating point value can be compared to its integer truncation, as above. Suppose a decimal computer (like the IBM1620!) for convenience, using eight decimal digits for the mantissa (and two for the exponent, as did the IBM1620). A (non-zero) normalised number must be of the form d·ddddddd and the largest number with a fractional digit would be 9999999·9 (represented as 9·9999999E+06 or possibly as ·99999999E+07 depending on the style of normalisation) and the next possible floating-point number would be 1·0000000E+07, then 1·0000001E+07, ''etc.'' advancing in steps of one. No fractional part is possible. Thus the boundary is clear and a test need merely involve a comparison: integral if greater than that, otherwise compare the floating-point number to its truncated integer form.
 
The argument is the same with binary (or base 4, 8 or 16), but, you have to know what base is used to prepare the proper boundary value, similarly you must ascertain just how many digits of precision are in use, remembering that in binary the leading one of normalised numbers may be represented implicitly, or it may be explicitly present. One would have to devise probing routines with delicate calculations that may be disrupted by various compiler optimisation tricks and unanticipated details of the arithmetic mill. For instance, Thethe Intel 8087 floating-point co-processor and its descendants use an implicit leading-one bit for 32- and 64-bit floating-point numbers, but ''not'' for 80-bit floating-point numbers. So if your compiler offers a REAL*10 type, such variables will enjoy a slightly different style of arithmetic. Further, ''during'' a calculation (add, subtract, multiply, divide) a further three guard bits (with special meanings) are employed. Calculations are done with full 83-bit precision to yield an 80-bit result; it is only when values are stored that they are converted to single or double precision format in storage - the register retains full precision. On top of that, the arithmetic can employ "denormalised" numbers during underflow towards zero. Chapter 6 of ''The I8087 Numeric Data Processor'', page 219, remarks "At least some of the generalised numerical solutions to common mathematical procedures have coding that is so involved and tricky in order to take care of all possible roundoff contingencies that they have been termed 'pornographic algorithms'". So a probe routine that worked for one design will likely need tweaking when tried on another system.
 
To determine the number of digits of precision, one probes somewhat as follows:<lang Fortran> X = 1
1,220

edits