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


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

Re: Can Java 8 do exact arithmetic as in C#

From j4n bur53 <janburse@fastmail.fm>
Newsgroups comp.lang.java.programmer
Subject Re: Can Java 8 do exact arithmetic as in C#
Date 2018-01-08 21:11 +0100
Organization albasani.net
Message-ID <p30j91$cvc$2@news.albasani.net> (permalink)
References <0ef8b3c0-e45c-4624-a8b8-2bdc67135380@googlegroups.com> <p2vnnj$cqa$1@news.albasani.net> <p2vsgr$2f1$1@dont-email.me>

Show all headers | View raw


Yes indeed, thank you very much.

2–13 Overflow Detection
http://www.hackersdelight.org/basics2.pdf

Eric Sosman schrieb:
> On 1/8/2018 7:21 AM, j4n bur53 wrote:
>> The source code is available, its a pure Java
>> implementation, no native code. For example addExact
>> is implemented as follows:
>>
>>      /**
>>       * Returns the sum of its arguments,
>>       * throwing an exception if the result overflows an {@code int}.
>>       *
>>       * @param x the first value
>>       * @param y the second value
>>       * @return the result
>>       * @throws ArithmeticException if the result overflows an int
>>       * @since 1.8
>>       */
>>      public static int addExact(int x, int y) {
>>          int r = x + y;
>>          // HD 2-12 Overflow iff both arguments
>>          // have the opposite sign of the result
>>          if (((x ^ r) & (y ^ r)) < 0) {
>>              throw new ArithmeticException("integer overflow");
>>          }
>>          return r;
>>      }
>>
>> HD 2-12 probably means Handbook of ... (who knows?)
>> chapter 2 section 12.
> 
> https://en.wikipedia.org/wiki/Hacker's_Delight
> 
> ... would be my guess.
> 

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


Thread

Can Java 8 do exact arithmetic as in C# bursejan@gmail.com - 2018-01-08 04:17 -0800
  Re: Can Java 8 do exact arithmetic as in C# j4n bur53 <janburse@fastmail.fm> - 2018-01-08 13:21 +0100
    Re: Can Java 8 do exact arithmetic as in C# Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-01-08 08:42 -0500
      Re: Can Java 8 do exact arithmetic as in C# j4n bur53 <janburse@fastmail.fm> - 2018-01-08 21:11 +0100
        Re: Can Java 8 do exact arithmetic as in C# bursejan@gmail.com - 2018-01-08 18:08 -0800
          Re: Can Java 8 do exact arithmetic as in C# bursejan@gmail.com - 2018-01-08 18:22 -0800
          Re: Can Java 8 do exact arithmetic as in C# v_borchert@despammed.com (Volker Borchert) - 2018-01-09 19:48 +0000
  Re: Can Java 8 do exact arithmetic as in C# Arne Vajhøj <arne@vajhoej.dk> - 2018-01-08 20:04 -0500
    Re: Can Java 8 do exact arithmetic as in C# bursejan@gmail.com - 2018-01-08 17:53 -0800

csiph-web