Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: v_borchert@despammed.com (Volker Borchert) Newsgroups: comp.lang.java.programmer Subject: Re: Can Java 8 do exact arithmetic as in C# Date: 9 Jan 2018 19:48:24 GMT Organization: Private site at Eddersheim, Germany Lines: 27 Distribution: world Message-ID: References: <0ef8b3c0-e45c-4624-a8b8-2bdc67135380@googlegroups.com> <3db83943-14d9-4589-a535-59ccf4b826d3@googlegroups.com> X-Trace: individual.net M4Ing0lRqNTkccsEbxZs9wpct45aLU/V3jSdBh4/en5BQ/qtQE Cancel-Lock: sha1:cyOvoQGS4YfcukpW8FSttsBYD0w= Xref: csiph.com comp.lang.java.programmer:37951 bursejan@gmail.com wrote: > On 64-bit CPU, the following shouldn't be that > costly Might depend on the actual CPU. > and is an alternative to the Java code: > > int x; > int y; > int z; > long h = (long)x + (long)y; > if (h < Integer.MIN_VALUE || Integer.MAX_VALUE < h) { > /* overflow situation */ > } else { > /* normal situation */ > z = (int)h; > } You might want to use non-circuit OR here to reduce the number of conditional branches to predict. -- "I'm a doctor, not a mechanic." Dr Leonard McCoy "I'm a mechanic, not a doctor." Volker Borchert