You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sebb <se...@gmail.com> on 2023/12/24 17:50:35 UTC
Re: (commons-numbers) branch master updated: Simplify conversion of numbers to unsigned
On Sun, 24 Dec 2023 at 16:58, <ah...@apache.org> wrote:
>
> This is an automated email from the ASF dual-hosted git repository.
>
> aherbert pushed a commit to branch master
> in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
>
>
> The following commit(s) were added to refs/heads/master by this push:
> new a15b3e68 Simplify conversion of numbers to unsigned
> a15b3e68 is described below
>
> commit a15b3e68136dd94ea20e4085afc45aa73d46362e
> Author: Alex Herbert <ah...@apache.org>
> AuthorDate: Sun Dec 24 16:57:58 2023 +0000
>
> Simplify conversion of numbers to unsigned
> ---
> .../java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
> index 4063f440..0b4aa797 100644
> --- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
> +++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
> @@ -28,6 +28,8 @@ import org.junit.jupiter.api.Test;
> *
> */
> class ArithmeticUtilsTest {
> + /** 2^63. */
> + private static final BigInteger TWO_POW_63 = BigInteger.ONE.shiftLeft(63);
>
> @Test
> void testGcd() {
> @@ -538,7 +540,7 @@ class ArithmeticUtilsTest {
> }
>
> private static long toUnsignedLong(int number) {
> - return number < 0 ? 0x100000000L + (long)number : (long)number;
> + return Integer.toUnsignedLong(number);
> }
The private method could now be dropped.
> private static int remainderUnsignedExpected(int dividend, int divisor) {
> @@ -550,7 +552,9 @@ class ArithmeticUtilsTest {
> }
>
> private static BigInteger toUnsignedBigInteger(long number) {
> - return number < 0L ? BigInteger.ONE.shiftLeft(64).add(BigInteger.valueOf(number)) : BigInteger.valueOf(number);
> + return number < 0 ?
> + TWO_POW_63.or(BigInteger.valueOf(number & Long.MAX_VALUE)) :
> + BigInteger.valueOf(number);
> }
>
> private static long remainderUnsignedExpected(long dividend, long divisor) {
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: (commons-numbers) branch master updated: Simplify conversion of numbers to unsigned
Posted by Alex Herbert <al...@gmail.com>.
On Sun, 24 Dec 2023 at 17:51, sebb <se...@gmail.com> wrote:
>
> On Sun, 24 Dec 2023 at 16:58, <ah...@apache.org> wrote:
> >
> > This is an automated email from the ASF dual-hosted git repository.
> >
> > aherbert pushed a commit to branch master
> > in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
> >
> >
> > The following commit(s) were added to refs/heads/master by this push:
> > new a15b3e68 Simplify conversion of numbers to unsigned
> > a15b3e68 is described below
> >
> > commit a15b3e68136dd94ea20e4085afc45aa73d46362e
> > Author: Alex Herbert <ah...@apache.org>
> > AuthorDate: Sun Dec 24 16:57:58 2023 +0000
> >
> > Simplify conversion of numbers to unsigned
> > ---
> > .../java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java | 8 ++++++--
> > 1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
> > index 4063f440..0b4aa797 100644
> > --- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
> > +++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
> > @@ -28,6 +28,8 @@ import org.junit.jupiter.api.Test;
> > *
> > */
> > class ArithmeticUtilsTest {
> > + /** 2^63. */
> > + private static final BigInteger TWO_POW_63 = BigInteger.ONE.shiftLeft(63);
> >
> > @Test
> > void testGcd() {
> > @@ -538,7 +540,7 @@ class ArithmeticUtilsTest {
> > }
> >
> > private static long toUnsignedLong(int number) {
> > - return number < 0 ? 0x100000000L + (long)number : (long)number;
> > + return Integer.toUnsignedLong(number);
> > }
>
> The private method could now be dropped.
I did this, then reverted. IMO it creates more noise in the source
where the method is called as it must be prefixed by "Integer.". This
makes one-liner code go over 120 chars.
>
> > private static int remainderUnsignedExpected(int dividend, int divisor) {
> > @@ -550,7 +552,9 @@ class ArithmeticUtilsTest {
> > }
> >
> > private static BigInteger toUnsignedBigInteger(long number) {
> > - return number < 0L ? BigInteger.ONE.shiftLeft(64).add(BigInteger.valueOf(number)) : BigInteger.valueOf(number);
> > + return number < 0 ?
> > + TWO_POW_63.or(BigInteger.valueOf(number & Long.MAX_VALUE)) :
> > + BigInteger.valueOf(number);
> > }
> >
> > private static long remainderUnsignedExpected(long dividend, long divisor) {
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org