Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Can Java 8 do exact arithmetic as in C# Date: Mon, 8 Jan 2018 08:42:50 -0500 Organization: A noiseless patient Spider Lines: 35 Message-ID: References: <0ef8b3c0-e45c-4624-a8b8-2bdc67135380@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Mon, 8 Jan 2018 13:42:51 -0000 (UTC) Injection-Info: h2725194.stratoserver.net; posting-host="9abe5c43625e8f62c6ccbf84f7874f8c"; logging-data="2529"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hfsIXDV/ouXVgC/xaSsVm" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 In-Reply-To: Content-Language: en-US Cancel-Lock: sha1:PVvgcWoGWP/8AXpxXQEeZvt4LKQ= Xref: csiph.com comp.lang.java.programmer:37938 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. -- esosman@comcast-dot-net.invalid Eleven hundred eight days to go.