Test integerness: Difference between revisions
m
→Tricky
(Added Julia language) |
m (→Tricky) |
||
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,
To determine the number of digits of precision, one probes somewhat as follows:<lang Fortran> X = 1
|