Overloaded Operators:
operator used for more than one purpose. writability vs. reliability. is this so? depends on how use it.
op overloading in C++. give me an example (<<, >>, +).
by language def, by user.
APL and SNOBOL have binary, unary versions of almost all operators, leads to confusion.
C++: & operator, as address of and bitwise and.
unrelated functions. similarly, * operator.
general problem of reusing symbols. comments, references, etc.
a /*p
unary, binary -.
"distinct symbols increase readability." is this so? + for string appending.
when used correctly, aids in readability.
add(matmul(A, B), matmul(C, D)
vs.
A * B + C * D
on the other hand, need know types of both operands to know meaning of operator.
example from Pascal, vs. C++: find sum of bunch of ints.
avg := sum / count;
avg = sum / count;
in Pascal, / specifies float result of division between two ints. VB as well, / vs
Type conversion
Narrowing vs. Widening conversion. widening generally safer.
mixed mode expressions = op has operands of different types. coercion to appropriate type where operator not defined on that type. also, there are explicit conversions via typecasting.
Diff langs disagree on whether to do coercion.
int a, b, c; float d;
a = b * d; // as an error for b * c
case in flexibility vs. efficiency:
PL/I - string can be combined with an int. string is scanned for numeric value, and if a decimal point, assumed to be a float, and coerce other operand to float. Coercion, and choice of coercion, at run time.
reduce flexibility in favor of reliability
Ada:
cannot mix int and float in arithmetic expression except for **, exponent.
most others, no such restrictions.
Java (byte, short), C++ (short, char). operators such as + first convert to int, and then convert back on assignment, even though both operands of the same type.
Explicit type conversion
Ada:
AVG := FLOAT(SUM) / FLOAT(COUNT)
C:
(int) angle
also C++'s function-like casting
errors can result from coercions. divide by zero when divide by a float.
Relational, Boolean Expressions
for the most part, similar. sometimes slight differences. FORTRAN used .EQ. and .NE. because symbols not on punch cards.
Ada uses = for comparison. /= for not equal.
relational given lower precedence than arithmetic, so that can say
a + b > c + d
Boolean expressions:
In Ada:
**, abs, not
*, /, mod, rem
unary +, -
etc.
lowest:
and, or, xor, and then, or else
these ops non-associative. A > B and A < k =" 0"> b > c.
Python handles this interestingly.
x <> z
This is interpreted by Python as::
if x <> z:
return y > z # 1 for plain comparisons
else:
return y > z # 0 for plain comparisons
else:
return x <>
Note that this requires testing the truth value of the result of
comparisons, with potential "shortcutting" of the right-side
comparison testings. In other words, the truth-value of the
result of a comparison determines the result of a chained
operation.
When know answer already, can ignore rest.
(13 * a) * (b / 13 - 1)
what if a is 0? But not easily detected. By boolean, already considering truth value.
(a >= 0) and (b < style="font-weight: bold;">speed.
can use this shortcircuiting to your advantage.
if (index in range and a[index] > 9)
so as to prevent an error.
on the other hand, sometimes will not expect that it is shortcircuiting, so issue of reliability. calling a function, incrementing a variable.
how decide: FORTRAN 77: up to lang implementor, if part is a func which assigns to a nonlocal variable, that nonlocal becomes "undefined"
difficulty in detecting these side-effects of functions.
In Ada: and then, or else does short-circuiting.
In C++: &&, || do short-circuit. If wish to avoid short-circuit, force 1 and 0 results and use bitwise operators.
Assignment
A = B
what about A = B = C?
depends. in C, yes. In PL/I, Basic, Visual Basic, = also does comparison, so would assign truth value of B = C to A.
ALGOL 60: := for assignment.
Mult targets:
PL/I: Sum, Total = 0.
other langs, e.g., C, do not.
Conditional targets
?: to give an lval rather than an rval, in C++.
Compound Assignment
a = a + b
a += b
C, C++, Java.
interesting feature of C# that defines these for you.
Unary Assignment
in C, C++, Java, etc. ++a.
when apply two unary ops, one on left side, one on right, works right to left:
- (count ++) even without parens
Assignment has an expression value
in C.
while ((ch = getchar() != EOF)
Mixed mode assignment
Do the source, target need be same type? FORTRAN, C, C++: coercion.
Pascal: depends - int to real, but not real to int.
Ada, Modula2 - nope.
Java: differs from C++ - only if widening results. increases reliability.
1 comment:
please note:
the python example above is messed up, because of the way blogger was handling things inside less than and greater than signs -- it was treating them as tags and stripping them out. This even after using html encoding tricks.
Follow the link for better text.
Post a Comment