Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #4066

Re: Java puzzler

From Tom Anderson <twic@urchin.earth.li>
Newsgroups comp.lang.java.programmer
Subject Re: Java puzzler
Date 2011-05-14 10:18 +0100
Organization Stack Usenet News Service
Message-ID <alpine.DEB.2.00.1105140954470.12373@urchin.earth.li> (permalink)
References (6 earlier) <iqhhim$blg$4@speranza.aioe.org> <iqhjks$798$1@dont-email.me> <AcCdnflMosQt-lHQnZ2dnUVZ_t2dnZ2d@earthlink.com> <iqhumi$750$1@dont-email.me> <jY-dnS9ZV4RyG1HQnZ2dnUVZ_tadnZ2d@earthlink.com>

Show all headers | View raw


On Thu, 12 May 2011, Patricia Shanahan wrote:

> On 5/12/2011 5:40 PM, markspace wrote:
>> On 5/12/2011 4:05 PM, Patricia Shanahan wrote:
>> 
>>> If (byte)100 + (byte)100 caused an overflow, I would need an unsigned
>>> byte type to do the same job.
>> 
>> OK, I think I understand. If the goal is to allow unsigned arithmetic
>> with automatic overflow detection, they we'd have to add unsigned
>> integer primitives to the language.
>
> The original proposal was to make all arithmetic detect overflow, and
> that would remove the existing ability to do unsigned arithmetic by
> ignoring overflow.

The semantics of which, i've realised, is an utter can of worms. Does 
this:

byte b = 100;
byte c = (b + b) - b;

Throw an exception? A interpreter which evaluates expression-by-expression 
would blow up when it came to the b + b. An optimising compiler can pretty 
easily optimise that expression out of existence altogether, but in doing 
so, it eliminates any opportunity to detect an overflow. If you require 
the exception, you close the door to a lot of optimisations.

One semantics that would work (i think) would be that all expressions are 
evaluated at infinite precision, and overflow is checked for on assignment 
to a variable (or parameter, etc). That might be rather hard to implement. 
Also, it means that this:

byte b = 100;
byte c = b + b;
byte d = c - c;
// c is never used after this point

Would not mean the same thing as this:

byte b = 100;
byte d = (b + b) - (b + b);

Because one puts the intermediate value through a variable, and the other 
does not. That's not great.

Would it be okay if i changed my mind again? :)

Seriously, having thought about it more, i think that:

byte b = 100;
int i = b + b;
assert i == -56;

Is absolutely fine. At the point at which you're adding b to itself, 
you're adding bytes, so it's perfectly natural that they add using the 
rules of bytes. It's only when you assign to the int variable that the 
type changes to int. After all, if i wrote:

List l = Arrays.asList("int", "i", "=", "b", "+", "b");
Collection c = l.subList(0, 2);

The call to subList is allowed, because the expression l.subList(0, 2) is 
evaluated according to the type l has where it's declared, not the type 
it's about to be assigned to. Widening conversions of primitives and 
pointers are very different things, but i don't think they should be 
gratuitously different.

How about this:

short one = 1;
short two = 2;
float aHalf = one / two;

What should the value of aHalf be?

tom

-- 
The RAMAN VESSEL enters the SOLAR SYSTEM. The explorers explore it,
and it is COOL. Then they LEAVE. Then the Raman vessel LEAVES. --
Book-A-Minute SF/F

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Java puzzler Gábor Bakos <aborgabor@gmail.com> - 2011-05-11 01:44 -0700
  Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-11 05:24 -0700
    Re: Java puzzler Tom Anderson <twic@urchin.earth.li> - 2011-05-11 21:57 +0100
      Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-11 16:07 -0700
        Re: Java puzzler Tom Anderson <twic@urchin.earth.li> - 2011-05-12 21:38 +0100
          Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-12 13:45 -0700
            Re: Java puzzler Nancy 3 <n3@gmai1.c0m> - 2011-05-12 16:56 -0400
              Re: Java puzzler markspace <-@.> - 2011-05-12 14:32 -0700
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-12 16:05 -0700
                Re: Java puzzler markspace <-@.> - 2011-05-12 17:40 -0700
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-12 18:18 -0700
                Re: Java puzzler markspace <-@.> - 2011-05-12 19:26 -0700
                Re: Java puzzler Tom Anderson <twic@urchin.earth.li> - 2011-05-14 10:18 +0100
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-14 08:09 -0700
                Re: Java puzzler Lew <noone@lewscanon.com> - 2011-05-14 13:00 -0400
                Re: Java puzzler Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-05-14 15:08 -0400
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-14 13:25 -0700
                Re: Java puzzler markspace <-@.> - 2011-05-14 08:46 -0700
                Re: Java puzzler Spock <spock@starfleet.ufp> - 2011-05-14 19:33 -0400
                Re: Java puzzler Tom Anderson <twic@urchin.earth.li> - 2011-05-15 00:41 +0100
                Re: Java puzzler markspace <-@.> - 2011-05-14 18:35 -0700
                Re: Java puzzler Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-05-14 23:43 -0400
                Re: Java puzzler Tom Anderson <twic@urchin.earth.li> - 2011-05-15 11:16 +0100
                Re: Java puzzler markspace <-@.> - 2011-05-15 05:40 -0700
                Re: Java puzzler Tom Anderson <twic@urchin.earth.li> - 2011-05-15 15:23 +0100
                Re: Java puzzler markspace <-@.> - 2011-05-15 08:09 -0700
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-15 07:37 -0700
                Re: Java puzzler markspace <-@.> - 2011-05-15 08:00 -0700
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-15 08:16 -0700
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-15 07:35 -0700
                Re: Java puzzler markspace <-@.> - 2011-05-15 08:11 -0700
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-15 08:15 -0700
                Re: Java puzzler Lew <noone@lewscanon.com> - 2011-05-15 13:11 -0400
                Re: Java puzzler Nancy 3 <n3@gmai1.c0m> - 2011-05-12 20:47 -0400
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-12 18:00 -0700
                Re: Java puzzler markspace <-@.> - 2011-05-12 18:01 -0700
                Re: Java puzzler Nancy 3 <n3@gmai1.c0m> - 2011-05-12 21:22 -0400
                Re: Java puzzler Patricia Shanahan <pats@acm.org> - 2011-05-12 18:32 -0700
                Re: Java puzzler Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 22:34 -0400
              Re: Java puzzler Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 22:27 -0400
      Re: Java puzzler "John B. Matthews" <nospam@nospam.invalid> - 2011-05-12 00:28 -0400
      Re: Java puzzler Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-05-12 10:33 -0400
  Re: Java puzzler Lew <noone@lewscanon.com> - 2011-05-11 09:12 -0400
  Re: Java puzzler Lew <noone@lewscanon.com> - 2011-05-11 09:13 -0400
  Re: Java puzzler markspace <-@.> - 2011-05-11 08:00 -0700

csiph-web