Skip to content

Commit

Permalink
refactor: Remove Math.nextUp call in QueryConstants (deephaven#5747)
Browse files Browse the repository at this point in the history
Math.nextUp is not presently usable in GWT, so this patch lets the JS
API share QueryConstants and ensures that the MIN_FINITE values are
correct, without actually initializing them via a call to Math.nextUp.

Partial deephaven#188
Co-authored-by: Ryan Caudy <rcaudy@gmail.com>
  • Loading branch information
niloc132 authored Jul 11, 2024
1 parent b1feeeb commit 2e835ce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions engine/query-constants/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ plugins {
id 'java-library'
id 'io.deephaven.project.register'
}

dependencies {
testImplementation libs.junit4
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,13 @@ private QueryConstants() {}
/**
* Minimum finite value of type float.
*/
public static final float MIN_FINITE_FLOAT = Math.nextUp(-Float.MAX_VALUE);
/*
* Implemented as a constant so that it can be translated correctly to JS without needing a proper {@code
* Math.nextUp()} or {@code Math.nextAfter()} implementation that works in JavaScript. Value is generated by running
* {@code Float.toHexString(Math.nextUp(-Float.MAX_VALUE))} in jshell, and tests validate that this matches the
* expected expression.
*/
public static final float MIN_FINITE_FLOAT = -0x1.fffffcp127f;

/**
* Maximum finite value of type float.
Expand Down Expand Up @@ -241,7 +247,13 @@ private QueryConstants() {}
/**
* Minimum finite value of type double.
*/
public static final double MIN_FINITE_DOUBLE = Math.nextUp(-Double.MAX_VALUE);
/*
* Implemented as a constant so that it can be translated correctly to JS without needing a proper {@code
* Math.nextUp()} or {@code Math.nextAfter()} implementation that works in JavaScript. Value is generated by running
* {@code Double.toHexString(Math.nextUp(-Double.MAX_VALUE))} in jshell, and tests validate that this matches the
* expected expression.
*/
public static final double MIN_FINITE_DOUBLE = -0x1.ffffffffffffep1023;

/**
* Maximum finite value of type double.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.util;

import org.junit.Test;

import static org.junit.Assert.*;

public class QueryConstantsTest {

@Test
public void testMinFiniteFloat() {
final float calculated = Math.nextUp(-Float.MAX_VALUE);
// noinspection SimplifiableAssertion
assertTrue(calculated == QueryConstants.MIN_FINITE_FLOAT);
assertEquals(
Float.floatToIntBits(calculated),
Float.floatToIntBits(QueryConstants.MIN_FINITE_FLOAT));
}

@Test
public void testMinFiniteDouble() {
final double calculated = Math.nextUp(-Double.MAX_VALUE);
// noinspection SimplifiableAssertion
assertTrue(calculated == QueryConstants.MIN_FINITE_DOUBLE);
assertEquals(
Double.doubleToLongBits(calculated),
Double.doubleToLongBits(QueryConstants.MIN_FINITE_DOUBLE));
}
}

0 comments on commit 2e835ce

Please sign in to comment.