commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Elkins <chr...@scardini.com>
Subject Re: [lang] NumberRange contribution
Date Mon, 25 Feb 2002 19:47:04 GMT
On Mon, Feb 25, 2002 at 02:19:01PM -0500, Henri Yandell wrote:
> 
> Definitely seems worth including. I've got a math.Interval class I wrote
> to be a range of two doubles (while reading a lisp book) and was wondering
> if you'd think the following methods would do well in NumberRange:
> 
> NumberRange add(NumberRange)
> NumberRange sub(NumberRange)
> NumberRange mul(NumberRange)
> NumberRange div(NumberRange)

While I don't personally have use for them, I can see cases where they might
be useful (especially add/sub for widening/narrowing a range easily).

> the hard part would be deciding what kind of Number to use when returning.
> Not sure I can see an easy solution that wouldn't invovle replicating the
> whole Java maths operator data type casting thing.
>
> So if one is a range of Integer and one is a range of Float, and it's an
> add, then you make a range of Float. Or something.
> 
> Any ideas?

I ran into similar problems (in another class I'm cleaning up for contribution)
where I wanted to use compareTo on arbitrary Number instances. I end up
promoting both Numbers to BigDecimals. For example,

public static int compareNumber(Number lhs, Number rhs) {
    BigDecimal tmp1 = new BigDecimal(lhs.toString());
    BigDecimal tmp2 = new BigDecimal(rhs.toString());
    return tmp1.compareTo(tmp2);
}

While one could make a case for using BigDecimal(lhs.doubleValue()) (although
it has the problem of always promoting integer values to floating-point
values) instead of the above, neither are very lightweight solutions.

-- 
Christopher Elkins

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message