commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From grace <grace.k...@msci.com>
Subject Re: [jexl] default numeric object type
Date Wed, 01 Feb 2012 01:03:26 GMT
Hi Henrib,

 Expression e1 = jexl.createExpression("123456675765756.657897879879");
 Object a = e1.evaluate(jc);
 Assert.assertEquals(new Double(123456675765756.657897879879).doubleValue(),
((Float)a).floatValue(), 0);

Exception in thread "main" java.lang.AssertionError:
expected:<1.2345667576575666E14> but was:<1.23456679051264E14>

Precision is lost.  Let me know if I am missing something.

I've found that the setReal method in ASTNumberLiteral default it to float.

 public void setReal(String s) {
        Number result;
        Class<?> rclass;
        final int last = s.length() - 1;
        switch (s.charAt(last)) {
            case 'b':
            case 'B': {
                result = new BigDecimal(s.substring(0, last));
                rclass = BigDecimal.class;
                break;
            }
            case 'd':
            case 'D': {
                rclass = Double.class;
                result = Double.valueOf(s);
                break;
            }
            case 'f':
            case 'F':
            default: {
                rclass = Float.class;
                try {
                    result = Float.valueOf(s);
                } catch (NumberFormatException take2) {
                    try {
                        result = Double.valueOf(s);
                    } catch (NumberFormatException take3) {
                        result = new BigDecimal(s);
                    }
                }
                break;
            }
        }
        literal = result;
        clazz = rclass;
    }

It seems if I change it to the below, it will convert the numeric to double. 


 public void setReal(String s) {
        Number result;
        Class<?> rclass;
        final int last = s.length() - 1;
        switch (s.charAt(last)) {
            case 'b':
            case 'B': {
                result = new BigDecimal(s.substring(0, last));
                rclass = BigDecimal.class;
                break;
            }
            case 'd':
            case 'D': {
                rclass = Double.class;
                result = Double.valueOf(s);
                break;
            }
            case 'f':
            case 'F':{
		rclass = Float.class;
                result = Float.valueOf(s);
		break;
	    }
            default: {
                rclass = Double.class;
                try {
                    result = Double.valueOf(s);
                } catch (NumberFormatException take2) {
                    try {
			result = Float.valueOf(s);
                    } catch (NumberFormatException take3) {
                        result = new BigDecimal(s);
                    }
                }
                break;
            }
        }
        literal = result;
        clazz = rclass;
    }

That seems to be a sufficient change according to my tests.  Would you
agree?

Thanks, Grace

--
View this message in context: http://apache-commons.680414.n4.nabble.com/jexl-default-numeric-object-type-tp4326105p4346490.html
Sent from the Commons - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message