JDK-8298060 : Fix precision bug in gesture recognizer classes
  • Type: Bug
  • Component: javafx
  • Sub-Component: graphics
  • Affected Version: openjfx19
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2022-12-03
  • Updated: 2022-12-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
While doing clean up of unnecessary casts in javafx/graphics, we discovered a potential problem in the gesture recognizer classes.  The result of System.nanotime() is cast to a double before doing a subtraction of the previous nano time.  If the value in the long is large (high bits are set) then the cast will loose the low order bits if done before calculating the difference of the two values.

     long rotationStartTime = System.nanotime();
     // time passes...
     long time = System.nanotime();

     double timeFromLastRotation = ((double)time - rotationStartTime) / 1000000;

The cast on `time` here could wipe out the low order bits (and also on `rotationStartTime` as it will be promoted to `double`). This could lead to the diff being zero when it shouldn't be.

While here, it may be good to also:

- Use `TimeUnit.NANOSECONDS.toMillis(long)` for the conversions or to use named constants for the divisors or to not do any conversion and use proper constants for the comparisons done.

                double timeFromLastRotation = ((double)time - rotationStartTime) / 1000000;
                if (timeFromLastRotation < 300) {

Could instead of doing a division (with a magic value) and then a comparison (with a magic value) instead simply do:

                long timeFromLastRotation = time - rotationStartTime;
                if (timeFromLastRotation < DURATION_300_MS) {

- Rename variables that hold a time value that is not in seconds to include the unit (ie. `millis`, `nanos`)

          timeFromLastRotation -> nanosSinceLastRotation

- Not use `double` for any of these calculations (ie. timeFromLastRotation has no business being `double`)


Comments
A pull request was submitted for review. URL: https://git.openjdk.org/jfx/pull/966 Date: 2022-12-03 22:17:55 +0000
05-12-2022