公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了当`Math.ulp`丢失时,如何计算`ulp`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要给定双精度值的ulp,但由于我是为代号1开发的,所以没有提供ulp(double)。有没有人知道用Java计算ulp的有效算法?Codename One仅提供了Math类中的一些方法(javadoc用于CN1版本),并且填补了MathUtil中的一些空白。

作为一种解决办法,我使用此(不正确的)代码,直到我找到一个可用的替代代码:

private double ulp(double y) {
    return y/1e15;
}

编辑:我为review发布了我自己的代码。以防其他人需要这个。

推荐答案

好的,因为我没有找到可用的替代品(ApacheHarmony和OpenJDK最终都使用了CN1上没有的本机方法),所以我编写了自己的版本(针对OpenJDK-Version进行了测试)。以防万一有人需要。

至于代号一:我向MathUtil类提交了一个补丁,所以希望迟早会添加这个补丁。

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

/**
 * Returns the size of an ulp (units in the last place) of the argument.
 * @param d value whose ulp is to be returned
 * @return size of an ulp for the argument
 */
@Override
public double ulp(double d) {
    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, double y) {
    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 either 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 double absStart = Math.abs(start);
    final double absDir = Math.abs(direction);
    final boolean toZero = !isSameSign(start, direction) || absDir < absStart;

    if (toZero) {
        // we are reducing the magnitude, going toward 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, toward +-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);
    }
}

这篇关于当`Math.ulp`丢失时,如何计算`ulp`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:Math ulp 丢失 计算
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定