octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

mixed type operations in Octave


From: Jaroslav Hajek
Subject: mixed type operations in Octave
Date: Mon, 8 Sep 2008 08:52:53 +0200

hi,

I'm working on a patch to improve integer arithmetics in Octave. While
the semantics for homogeneous operations is basicallly a POC, I'm not
confident about the semantics mixed-integer and real-integer
operations.
Currently, it seems that Matlab allows homogeneous integer
arithmetics, and mixed integer-double arithmetics with scalar double,
while at the same time it disallows int64 arithmetics at all. Octave
is somewhat more permissive in that the double part doesn't need to be
a scalar.
Currently, the semantic of a double-expr OP int-expr is "convert
int-expr to double, do OP, convert result to int". Although this
behaviour is incompatible with almost all other languages, it makes
some sense - the primary motivation being probably that m-code like "a
= int32 (1); a += 1;" should work and produce "a = int32 (2)".

The problem is, that this semantics does not work with 64-bit
integers, because they do not all fit into doubles without losing
digits.

So, the question is what to do? There are several paths:

1. Solve nothing and just mimick Matlab.
Okay, not quite. IMO, not having int64 arithmetics is not dignified
enough for a 64-bit ready system (yeah, Octave is not quite 64-bit
ready, but I hope it aims to be). So at least int64 OP int64 should be
allowed, but we simply disallow int64 OP double.

2. Simply extend the current behaviour to int64-double ops. This sort
of kills the initial point, because then int64(a) + 1 is not the same
as int64 (a) + int64 (1).

3. make `a OP b' (a is intXX, b is double) mean `a OP intXX (b)'. This
solves the "add small constant" problem elegantly, but breaks some
other tricks, like a * 0.5 would no longer mean a / 2.

4. use 3, but only for addition and subtraction. Although it sounds
weird, I actually like this one because I think that few people care
about multiplying and dividing integer and real numbers together.

5. preserve the current semantics for < 32-bit integers, and do 3 or 4
for int64. Though this is close to a "least disturbance" solution, I
think it is (a little?) nasty, having different integer types work
differently.

6. curse all of this mess and make Octave compatible with the rest of
the world, i.e. integer literals have integer type, and the numeric
tower is logical -> integers -> reals. Okay, I don't mean this really
seriously - that would break backward and Matlab compatibility, and
would take a lot of work. It's here just for completeness.

7. pretend that there always exists a real type wide enough to hold
all 64-bit integers, and use the semantics "convert both operands to
wide real, do OP, convert to intXX". On systems where this type really
exists (like the 80-bit long double on x86) it could be implemented
directly; otherwise, it would need software emulation (painfully slow
and laborious).

I have the homogeneous arithmetic working, but before proceeding, I
need to decide this matter and I found out it's not that easy. Perhaps
someone can suggest some particularly good argument for some option,
or suggest something fresh.

Mixed-integer operations are another question. Currently, they're not
allowed from within interpreter, but they're implemented in liboctave,
using "convert to double, do OP, convert back".
The question is, should they be allowed? And if yes, what meaning
should they have? If no, they should probably be removed from
liboctave, because they apparently consume a not quite negligible
amount of compilation time.


regards,

-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]