当前位置: 首页>>代码示例>>Java>>正文


Java LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION属性代码示例

本文整理汇总了Java中org.apache.commons.math3.exception.util.LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION属性的典型用法代码示例。如果您正苦于以下问题:Java LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION属性的具体用法?Java LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION怎么用?Java LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在org.apache.commons.math3.exception.util.LocalizedFormats的用法示例。


在下文中一共展示了LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: Fraction

/**
 * Create a fraction given the numerator and denominator.  The fraction is
 * reduced to lowest terms.
 * @param num the numerator.
 * @param den the denominator.
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public Fraction(int num, int den) {
    if (den == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          num, den);
    }
    if (den < 0) {
        if (num == Integer.MIN_VALUE ||
            den == Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              num, den);
        }
        num = -num;
        den = -den;
    }
    // reduce numerator and denominator by greatest common denominator.
    final int d = ArithmeticUtils.gcd(num, den);
    if (d > 1) {
        num /= d;
        den /= d;
    }

    // move sign to numerator.
    if (den < 0) {
        num = -num;
        den = -den;
    }
    this.numerator   = num;
    this.denominator = den;
}
 
开发者ID:biocompibens,项目名称:SME,代码行数:36,代码来源:Fraction.java

示例2: getReducedFraction

/**
 * <p>Creates a {@code Fraction} instance with the 2 parts
 * of a fraction Y/Z.</p>
 *
 * <p>Any negative signs are resolved to be on the numerator.</p>
 *
 * @param numerator  the numerator, for example the three in 'three sevenths'
 * @param denominator  the denominator, for example the seven in 'three sevenths'
 * @return a new fraction instance, with the numerator and denominator reduced
 * @throws MathArithmeticException if the denominator is {@code zero}
 */
public static Fraction getReducedFraction(int numerator, int denominator) {
    if (denominator == 0) {
        throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
                                          numerator, denominator);
    }
    if (numerator==0) {
        return ZERO; // normalize zero.
    }
    // allow 2^k/-2^31 as a valid fraction (where k>0)
    if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
        numerator/=2; denominator/=2;
    }
    if (denominator < 0) {
        if (numerator==Integer.MIN_VALUE ||
                denominator==Integer.MIN_VALUE) {
            throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
                                              numerator, denominator);
        }
        numerator = -numerator;
        denominator = -denominator;
    }
    // simplify fraction.
    int gcd = ArithmeticUtils.gcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
    return new Fraction(numerator, denominator);
}
 
开发者ID:biocompibens,项目名称:SME,代码行数:38,代码来源:Fraction.java


注:本文中的org.apache.commons.math3.exception.util.LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。