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


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

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 13:21 +0100
Organization albasani.net
Message-ID <p2vnnj$cqa$1@news.albasani.net> (permalink)
References <0ef8b3c0-e45c-4624-a8b8-2bdc67135380@googlegroups.com>

Show all headers | View raw


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.

bursejan@gmail.com schrieb:
> Java 8 has a nice feature, maybe this
> isn't known very well. But there are
> 
> a couple of new operations in Math:
> 
>      /**
>       * 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);
> 
>      /**
>       * Returns the sum of its arguments,
>       * throwing an exception if the result overflows a {@code long}.
>       *
>       * @param x the first value
>       * @param y the second value
>       * @return the result
>       * @throws ArithmeticException if the result overflows a long
>       * @since 1.8
>       */
>      public static long addExact(long x, long y);
> 
> Etc..
> 
> ------------ Arne wrote ---------------
> 
> 
> 	Arne Vajhøj 	
> 02:06 (vor 11 Stunden)
> Nachricht auf Deutsch übersetzen
> On 1/5/2018 2:59 PM, Daniele Futtorovic wrote:
> - zitierten Text einblenden -
> I like C# with the check enabled.
> 
> using System;
> 
> namespace E
> {
>       public class Program
>       {
>           private static void Avg(int a, int b)
>           {
>               Console.WriteLine("Average of {0} and {1} is {2}", a, b, (a
> + b) / 2);
>           }
>           public static void Main(string[] args)
>           {
>               Avg(123, 456);
>               Avg(int.MaxValue, int.MaxValue);
>               Console.ReadKey();
>           }
>       }
> }
> 
> What is best:
> 
> Average of 123 and 456 is 289
> Average of 2147483647 and 2147483647 is -1
> 
> or:
> 
> Average of 123 and 456 is 289
> System.OverflowException: Arithmetic operation resulted in an overflow.
> 
> ?
> 
> If one really need the integer overflow to work then it is actually
> still possible by making it explicit.
> 
> (I had big problems coming up with an example where it make sense
> to overflow, but ...)
> 
> using System;
> using System.Text;
> 
> namespace E
> {
>       public class Program
>       {
>           private static int[] Encrypt(int[] a, int k)
>           {
>               int[] res = new int[a.Length];
>               for(int i = 0; i < res.Length; i++)
>               {
>                   unchecked
>                   {
>                       res[i] = a[i] + k;
>                   }
>               }
>               return res;
>           }
>           private static int[] Decrypt(int[] a, int k)
>           {
>               return Encrypt(a, -k);
>           }
>           private const int KEY = 123456789;
>           public static void Main(string[] args)
>           {
>               int[] a1 = { int.MinValue, -1, 0, 1, int.MaxValue };
>               int[] a2 = Encrypt(a1, KEY);
>               int[] a3 = Decrypt(a2, KEY);
>               for(int i = 0; i < a1.Length; i++)
>               {
>                   Console.WriteLine("{0} -> {1} -> {2}", a1[i], a2[i],
> a3[i]);
>               }
>               Console.ReadKey();
>           }
>       }
> }
> 
> works fine even with the check enabled due to the:
> 
> unchecked { }
> 
> block.
> 
> I think this is the right way. Check by default and have a way
> to disable the check for specific code sections where it is
> needed.
> 
> Arne
> 

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