Implicit type conversion: Difference between revisions

m
→‎{{header|J}}: fix some archaic bits
(Added EasyLang implementation)
m (→‎{{header|J}}: fix some archaic bits)
Line 815:
Nouns break down into four disjoint collections of subtypes: boxed, literal, numeric and symbolic (which is rarely used).
Most of J's implicit conversions happen within the first three subtypes. (And J supports some "extra conversions" between these types in some cases where no values are involved.
For example a length 0 list which contains no characters (literals) may be used as a list which contains no numbers (numerics)).
 
There is one type of box, twothree types of literals (8 bit wide, 16 bit wide and 1632 bit wide -- each of which are typically interpreted as unicode by the OS), and a variety of types of numerics.
 
There is one type of box, two types of literals (8 bit wide and 16 bit wide), and a variety of types of numerics.
Sparse arrays are also (partially) supported and treated internally as distinct datatypes, implemented under the covers as a sequence of arrays (one to indicate which indices have values, and another to hold the corresponding values, and also a default value to fill in the rest).
 
The primary implicit type conversion in J applies to numeric values.
 
In particular, J tries to present numeric values as "[http://www.jsoftware.com/pipermail/beta/2006-April/000749.html analytic]"; that is, numeric values which are "the same" should presented to the user (J programmer) as "the same" in as many different contexts as is feasible, irrespective of their representation in the the computer's model or layout in memory. So, for example, on a 32-bit machine, `2147483647` (or `(2^31)-1`) is the largest value a signed integer, which is stored in 4 bytes, can represent; in a 32 bit J implementation, incrementing this value (adding 1) causes the underlying representation to switch to IEEE double-precision floating point number. In a 64 bit J implementation, `9223372036854775807` (or `(2^63)-1`) is the largest value which can be represented as a fixed width integer.
In other words `1+(2^31)-1` doesn't overflow: it represents `2^31` exactly (using double the memory: 8 bytes). Similar comments apply to the two varieties of character values (ASCII and Unicode), though the implications are more straightforward and less interesting.
 
In other words `1+(2^31)-1` doesn't overflow: it represents `2^31` exactly (using double the memory on a 32 bit J implementation: 8 bytes). Similar comments apply to the two varieties of character values (ASCII and Unicode), though the implications are more straightforward and less interesting.
 
Having said that all that, because of the potential performance penalties involved, J does not stretch this abstraction too far. For example, numbers will never be automatically promoted to the (available, but expensive) arbitrary precision format, nor will values be automatically "demoted" (automatic demotion, paired with automatic promotion, has the potential to cause cycles of expansion and contraction during calculation of intermediate values; this, combined with J's homogeneous array-oriented nature, which requires an entire array to be promoted/demoted along with any one of its values, means including automatic demotion would probably hurt programs' performance more often than it benefited them.)
Line 883 ⟶ 886:
 
J has verbs causing explicit conversion. Some appear in the above examples.
J's lexical notation provides for us to directly specify the datatype as demonstrated in the other samples. The Extended and Rational Arithmetic section of the J dictionary (DOJ) explains the fundamental implicit conversions.
Before quoting this the section here, please note that arrays have the homogeneous data type of the highest atomic data type as shown in the 0 1 2 integer vector---implicit conversion without using the primitive verbs.
 
J's lexical notation provides forlet's us to directly specify the datatype of a constant, as demonstrated in the otherthese samples. The Extended and Rational Arithmetic section of the J dictionary (DOJ) explains the fundamental implicit conversions.
''Various primitive verbs produce (exact) rational results if the argument(s) are rational; non-rational verbs produce (inexact) floating point or complex results when applied to rationals, if the verb only has a limited number of rational arguments that produce rational results.
 
''Various primitive verbs produce (exact) rational results if the argument(s) are rational; non-rational verbs produce (inexact) floating point or complex results when applied to rationals, ifsome theverbs verbwhich onlyproduce hasirrational aresults limitedmay numberinstead ofproduce rational argumentsresults thatwhen produceit's possible to do so accurately and the verb's arguments are rational results.(or are values which can be promoted to rational).
 
(For example, %:y is rational if the atoms of y are perfect squares; ^0r1 is floating point.)
The quotient of two extended integers is an extended integer (if evenly divisible) or rational (if not). Comparisons involving two rationals are exact.
''Dyadic verbs (e.g. + - * % , = <) that require argument type conversions do so according to the following table:''
<pre>
| B I X Q D Z
6,951

edits