I want to transplant Java Math.ulp (Math.nextAfter) to dart.

Asked 2 years ago, Updated 2 years ago, 130 views

I'm porting logic from Java for special distance calculations in Dart.
There is no match for Math.ulp in Dart, so we are implementing it on our own.

I was taught by Google teacher
We refer to the StackOverflow response below.
https://stackoverflow.com/questions/24104763/how-to-compute-ulpwhen-math-ulp-is-missing

/*
 * use a calculated value for the ulp of Double.MAX_VALUE
 */
private static final double MAX_ULP = 1.9958403095347198E292;

/**
 * Returns the size of anulp (units in the last place) of the argument.
 * @param d value which amp is to be returned
 * @return size of anulp for the argument
 */
@ Override
public doubleulp(double){
    if(Double.isNaN(d)){
        // If the argument is NaN, then the result is NaN.
        return Double.NaN;
    }

    if(Double.isInfinite(d)) {
        // If the argument is positive or negative infinity, then the
        // result is positive infinity.
        return Double.POSITIVE_INFINITY;
    }

    if(d==0.0){
        // If the argument is positive or negative zero, then the result is Double.MIN_VALUE.
        return Double.MIN_VALUE;
    }

    d = Math.abs(d);
    if(d==Double.MAX_VALUE){
        // If the argument is Double.MAX_VALUE, then the result is equal to 2^971.
        return MAX_ULP;
    }

    return nextAfter(d, Double.MAX_VALUE)-d;
}

@ Override
public double copySign(double x, doubley) {
    return com.codename1.util.MathUtil.copysign(x,y);
}

private boolean isSameSign (double x, double y) {
    return copySign(x,y)==x;
}

/**
 * Returns the next representable floating point number after the first
 * argument in the direction of the second argument.
 *
 * @param start starting value
 * @param direction value indicating which of the neighboring representable
 *  floating point number to return
 * @return The floating-point number next to {@code start} in the
 * direction of {@direction}.
 */
@ Override
public double nextAfter(final double start, final double direction) {
    if(Double.isNaN(start)||Double.isNaN(direction)){
        // If the argument is a NaN, then NaN is returned.
        return Double.NaN;
    }

    if(start==direction){
        // If both arguments compare as equal the second argument is returned.
        return direction;
    }

    final doubleabsStart = Math.abs(start);
    final doubleabsDir=Math.abs(direction);
    final boolean toZero=!isSameSign(start, direction)||absDir<absStart;

    if(toZero){
        // We are reducing the magnitude, going tower zero.
        if(absStart==Double.MIN_VALUE){
            return copySign(0.0, start);
        }
        if(Double.isInfinite(absStart)){
            return copySign(Double.MAX_VALUE, start);
        }
        return copySign(Double.longBitsToDouble(Double.doubleToLongBits(absStart)-1L), start);
    } else{
        // we are increasing the magnitude, tower + - Infinity
        if(start==0.0){
            return copySign(Double.MIN_VALUE, direction);
        }
        if(absStart==Double.MAX_VALUE){
            return copySign(Double.POSITIVE_INFINITY, start);
        }
        return copySign(Double.longBitsToDouble(Double.doubleToLongBits(absStart)+1L), start);
    }
}

Of the codes in the ,

Double.longBitsToDouble(Double.doubleToLongBits(absStart)+1L

I don't really understand the meaning of
In the first place, except for the basic part, I don't understand bit calculation
Also, the double<>long bit conversion does not exist in Dart, so I stopped working.

1. Why do you wake me up to long bit and do ±1 and return to double?
 I think replacing double with long is converting decimal expressions to integers, so
 I wondered if the lowest digit including the floating point of the processing system should be ±1, but is that correct?
2. And if you want to do this with Dart, you'll find out what to do.

Can someone tell me about

java dart

2022-09-29 21:29

1 Answers

It's too late, but I think it's better to stop implementing it on your own and think about how to call Java code from Dart.
https://flutter.keicode.com/basics/method-channel-java.php
If you implement this kind of library on your own, your implementation and bugging will be the majority of your work.


2022-09-29 21:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.