swarm-support
[Top][All Lists]
Advanced

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

Re: Integer arithmetic in Java


From: Tim Howe
Subject: Re: Integer arithmetic in Java
Date: Wed, 5 Dec 2001 11:43:49 -0800

Steve,

The behavior you described can be deduced, more or less, from two basic
principles.

One principle is that languages can be much simpler if they're context-free.
One of the ways in which Java is context-free is that expressions are
evaluated (and values are promoted) without regard to the context in which
the expressions are found.

Another principle is that conversion is automatic and silent if no
information is lost in the conversion.

So we start with the expression

    (float) = (int) / (int)

This expression decomposes into two subexpressions.

1. The compiler evaluates the subexpression (int) / (int), which by integer
arithmetic results in (int).

2. The compiler evaluates the subexpression (float), which is trivial.

3. The compiler evaluates the subexpression

    (float) = (int)

Do we lose any information when we convert an int to a float? No, because 13
contains no more information than 13.000000 does, so the compiler converts
the int with no warning.

(Minor note: step 2 above may actually occur before step 1, depending on the
compiler implementation.)

Here's another example: ++s * i >= f / d

The compiler evaluates this in three steps:

        s is promoted to int for the unary operation; result of the
left-hand side (lhs) is int;
        f is promoted to double; result of rhs is double;
        lhs is promoted to double; result is boolean.

Here's a fun example: ar[--s] = ++s * i >= f / d ? 35 : --s + 1 (yes, it
compiles). Evaluation and promotion are completely explained by the two
principles described above. The issue mentioned in the "minor note" above
becomes dangerous in this expression.

- Tim


----- Original Message -----
From: "Jozef Babjak" <address@hidden>
To: "Swarm support" <address@hidden>
Sent: Tuesday, December 04, 2001 3:21 PM
Subject: Re: Integer arithmetic in Java


> Hi,
>
> in java  (int) / (int) gives int value, but
>          (int) / (float)                 or
>        (float) / (int) gives float value.
>
> If U want to obtain float value from (int)/(int) expression, do this:
>
>  aFloat = (float)(anInt) / (anotherInt)    or
>  aFloat = (anInt) / (float)(anotherInt).
>
>
>
> J. Babjak
>
> address@hidden
>
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> On Tue, 4 Dec 2001, M Lang / S Railsback wrote:
>
> >
> > In de-bugging our first model in Java Swarm we finally figured out that:
> >
> > aFloat = (anInt) / (anotherInt);
> >
> > gives aFloat a value truncated to a whole number. (We were doing
> > movement calculations on a 2D grid with integer coordinates, trying to
> > figure out why the critters ended up in the wrong places.)
> >
> > Pardon my inexperience, but is that something that everybody knows? It
> > certainly seems like a way to introduce undetected errors...IF we
> > weren't blessed with Swarm probe displays and GUIs to find them!
> >
> > Steve
> >
> > --
> > address@hidden
> > Lang, Railsback & Assoc.
> > 250 California Ave., Arcata CA 95521
> > 707-822-0453; Fax 822-1868
> >
> >                   ==================================
> >    Swarm-Support is for discussion of the technical details of the day
> >    to day usage of Swarm.  For list administration needs (esp.
> >    [un]subscribing), please send a message to <address@hidden>
> >    with "help" in the body of the message.
> >
> >
>
>
>                   ==================================
>    Swarm-Support is for discussion of the technical details of the day
>    to day usage of Swarm.  For list administration needs (esp.
>    [un]subscribing), please send a message to <address@hidden>
>    with "help" in the body of the message.
>
>

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.


reply via email to

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