Jump to content

Implicit type conversion: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added EasyLang implementation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(8 intermediate revisions by 5 users not shown)
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
Line 1,490 ⟶ 1,494:
dict({1: 'one', 'two': (2+3j), ('RC', 3): None}) -> {1: 'one', 'two': (2+3j), ('RC', 3): None}
</pre>
===alternative===
One case of implicit casting and one quasi-case:
<syntaxhighlight lang="python">>>> 12/3 # Implicit cast from int to float, by / operator.
4.0
>>> (2+4j)/2 # But no implicit cast for complex parts.
(1+2j)
>>> (11.5+12j)/0.5 # Quasi-case, complex parts implicit cast from float to int.
(23+24j)
</syntaxhighlight>
 
=={{header|Racket}}==
Line 1,581 ⟶ 1,594:
@() pluralize
%() hashify</pre>
 
=={{header|RPL}}==
To make basic calculations easier, RPL accepts to mix floating-point real numbers and either complex numbers or binary integers when using <code>+ - * /</code> operations.
(2,3) 2 *
#37h 16 +
{{out}}
<pre>
2: (4,6)
1: #47h
</pre>
 
=={{header|REXX}}==
Line 1,683 ⟶ 1,706:
 
Defining a new type requires writing an extension to Tcl in [[C]] (or whatever the host programming language is, so [[Java]] for [[JTcl]]); the interfaces for doing this are not directly exposed to the Tcl script level because they require direct memory access, which Tcl normally does not permit in order to promote overall process stability.
 
=={{header|V (Vlang)}}==
A small primitive type can be automatically promoted if it fits completely into the data range of the type on the other side.
 
These are the allowed possibilities:
<syntaxhighlight lang="Vlang">
i8 → i16 → int → i64
↘ ↘
f32 → f64
↗ ↗
u8 → u16 → u32 → u64 ⬎
↘ ↘ ↘ ptr
i8 → i16 → int → i64 ⬏
</syntaxhighlight>
For example, an int value can be automatically promoted to f64 or i64, but not to u32.
 
Note- u32 would mean loss of the sign for negative values.
 
Promotion from int to f32, however, is currently done automatically (but can lead to precision loss).
<syntaxhighlight lang="Vlang">
u := u16(12)
x := f32(45.6)
a := 75
b := 14.7
c := u + a // c is of type `int` - automatic promotion of `u`'s value
println(c) // 87
d := b + x // d is of type `f64` - automatic promotion of `x`'s value
println(d) // 60.2999984741211
</syntaxhighlight>
 
=={{header|Wren}}==
Line 1,695 ⟶ 1,747:
 
However, you can define implicit conversions for user defined types. For example, BigInts can be generated from integral Nums or Strings and, when doing operations on BigInts, values of these types are automatically converted to BigInts so the operations can succeed.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
 
var b1 = BigInt.new(32)
Line 1,702 ⟶ 1,754:
var b3 = b1 + b2 + 2 + "2"
System.print(b3) // 100</syntaxhighlight>
 
=={{header|XPL0}}==
XPL0 doesn't have implicit type conversions. It only has two basic data
types: integer and real. Conversions must be done explicitly. For
example:
<syntaxhighlight lang "XPL0">
int I;
real R;
I:= fix(R);
R:= float(I);
</syntaxhighlight>
 
There is a third declaration type called 'character' (usually abbreviated
'char'). Its variables are the same as integers except when they are
indexed, as for arrays. An indexed character variable accesses bytes,
whereas an indexed integer variable accesses integers.
 
Strings are one-dimensional arrays consisting of bytes containing ASCII
characters.
 
Booleans are represented by integers. Zero is false, and non-zero is
true.
 
Real literals are distinguished from integers by including a decimal point or exponent. For example: 12. or 6e12
 
=={{header|Z80 Assembly}}==
9,476

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.