Read Sean

Read me, read Sean.
posts - 508, comments - 655, trackbacks - 9, articles - 4

[Jakarta Commons笔记] org.apache.commons.lang.math

Posted on 2005-08-01 13:18 laogao 阅读(3971) 评论(3)  编辑  收藏 所属分类: On Java

 

Jakarta Commons中,专门处理数学计算的类分别可以在两个地方找到:一是Commons Langorg.apache.commons.lang.math包中,二是在Commons Math这个单独的子项目中。由于后者主要是处理复数、矩阵等,相对使用比较少,在我的笔记中就只简单讲讲Commons Lang中的math包。对后者感兴趣的可以看看

http://jakarta.apache.org/commons/math/

 

org.apache.commons.lang.math包中共有10个类,这些类可以归纳成四组:

1- 处理分数的Fraction类;

2- 处理数值的NumberUtils类;

3- 处理数值范围的RangeNumberRangeIntRangeLongRangeFloatRangeDoubleRange类;

4- 处理随机数的JVMRandomRandomUtils类。

 

下面我举个例子分别说明上述四组的使用方法:

 

package sean.study.jakarta.commons.lang;

 

import org.apache.commons.lang.ArrayUtils;

import org.apache.commons.lang.BooleanUtils;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.lang.math.DoubleRange;

import org.apache.commons.lang.math.Fraction;

import org.apache.commons.lang.math.NumberUtils;

import org.apache.commons.lang.math.RandomUtils;

import org.apache.commons.lang.math.Range;

 

public class LangMathUsage {

 

    public static void main(String[] args) {

        demoFraction();

        demoNumberUtils();

        demoNumberRange();

        demoRandomUtils();

    }

 

    public static void demoFraction() {

        System.out.println(StringUtils.center(" demoFraction ", 30, "="));

        Fraction myFraction = Fraction.getFraction(144, 90);

        // Fraction myFraction = Fraction.getFraction("1 54/90");

        System.out.println("144/90 as fraction: " + myFraction);

        System.out.println("144/90 to proper: " + myFraction.toProperString());

        System.out.println("144/90 as double: " + myFraction.doubleValue());

        System.out.println("144/90 reduced: " + myFraction.reduce());

        System.out.println("144/90 reduced proper: "

                + myFraction.reduce().toProperString());

        System.out.println();

    }

 

    public static void demoNumberUtils() {

        System.out.println(StringUtils.center(" demoNumberUtils ", 30, "="));

        System.out.println("Is 0x3F a number? "

                + StringUtils.capitalize(BooleanUtils.toStringYesNo(NumberUtils

                        .isNumber("0x3F"))) + ".");

        double[] array = { 1.0, 3.4, 0.8, 7.1, 4.6 };

        double max = NumberUtils.max(array);

        double min = NumberUtils.min(array);

        String arrayStr = ArrayUtils.toString(array);

        System.out.println("Max of " + arrayStr + " is: " + max);

        System.out.println("Min of " + arrayStr + " is: " + min);

        System.out.println();

    }

 

    public static void demoNumberRange() {

        System.out.println(StringUtils.center(" demoNumberRange ", 30, "="));

        Range normalScoreRange = new DoubleRange(90, 120);

        double score1 = 102.5;

        double score2 = 79.9;

        System.out.println("Normal score range is: " + normalScoreRange);

        System.out.println("Is "

                + score1

                + " a normal score? "

                + StringUtils

                        .capitalize(BooleanUtils.toStringYesNo(normalScoreRange

                                .containsDouble(score1))) + ".");

        System.out.println("Is "

                + score2

                + " a normal score? "

                + StringUtils

                        .capitalize(BooleanUtils.toStringYesNo(normalScoreRange

                                .containsDouble(score2))) + ".");

        System.out.println();

    }

 

    public static void demoRandomUtils() {

        System.out.println(StringUtils.center(" demoRandomUtils ", 30, "="));

        for (int i = 0; i < 5; i++) {

            System.out.println(RandomUtils.nextInt(100));

        }

        System.out.println();

    }

 

}

 

以下是运行结果:

 

======== demoFraction ========

144/90 as fraction: 144/90

144/90 to proper: 1 54/90

144/90 as double: 1.6

144/90 reduced: 8/5

144/90 reduced proper: 1 3/5

 

====== demoNumberUtils =======

Is 0x3F a number? Yes.

Max of {1.0,3.4,0.8,7.1,4.6} is: 7.1

Min of {1.0,3.4,0.8,7.1,4.6} is: 0.8

 

====== demoNumberRange =======

Normal score range is: Range[90.0,120.0]

Is 102.5 a normal score? Yes.

Is 79.9 a normal score? No.

 

====== demoRandomUtils =======

75

63

40

21

92

 

以上就是Commons Langmath包通常的用法。提一下NumberUtils.inNumber(String)方法,它会正确判断出给定的字符串是否是合法的数值,而我在前面的笔记中提到的StringUtils.isNumeric(String)只能判断一个字符串是否是由纯数字字符组成。Commons Langmath包的各个类都还有很多实用的方法,远不止我在例子中用到的这些,如果你感兴趣,参照一下Commons LangJavadoc吧。

 

 

Feedback

# re: [Jakarta Commons笔记] org.apache.commons.lang.math  回复  更多评论   

2006-01-15 10:37 by 大嘴巴
google summer code 上的project,作者是叫zhangxiaogang,是您吗?

# re: [Jakarta Commons笔记] org.apache.commons.lang.math  回复  更多评论   

2006-01-15 21:21 by 大胃
不是。

# re: [Jakarta 笔记] org.apache.commons.lang.math[未登录]  回复  更多评论   

2012-12-17 14:37 by doctor
Commons math 或JmathLib,在eclipse中怎么使用,我想写个将扫描的网格线数据转换成函数式的工具,现存的不好用,如:getData,FindGraph,engauge,QQ;873128294,zmhhxl@yahoo.com.cn

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问