24 Jun 2004 09:39
Re: Float/Double/BigDecimal promotion & numeric literals
On 24 Jun 2004, at 08:15, John Wilson wrote: > > On 24 Jun 2004, at 06:41, J. Matthew Pryor wrote: > >> I was trying to call an inherited Java method from a groovy class >> >> Let's say the method was declared as >> >> void someInheritedMethod(Object param1, double param2) >> >> In my groovy class I was doing this: >> >> double doubleParam = 60000.0; >> someInheritedMethod(anObject, doubleParam); >> >> Now that was bombing out with an InvalidArgumentException : >> incompatible >> argument type (or something like that) and I now realize that the >> problem >> was probably that despite declaring doubleParam as double, it was >> being >> promoted to a BigDecimal (least surprising apparently, and then >> couldn't >> be demoted to double when it came time to call the method? [that's my >> theory >> anyway] >> >> So I changed the code to this: >> double doubleParam = 60000D; >> someInheritedMethod(anObject, doubleParam.doubleValue()); >> >> This variant caused a syntax error on the 'D' : not sure if that's a >> fair >> cop or not, but changing it to: >> double doubleParam = 60000.0D; >> someInheritedMethod(anObject, doubleParam.doubleValue()); >> >> Worked fine [perhaps I don't need the .doubleValue() part] >> >> 2 questions: >> >> 1. Is my theory about why the first code failed correct? Does that >> seem >> reasonable given I was statically typing? >> 2. Should 60000D really be a syntax error, whereas 60000.0D is not? >> >> Yeah I know that is 3 questions, but anyway .... >> >> > > I think it should have worked OK the first time. > > I'm currently looking at the way in which the runtime selects methods > to call. It is certainly broken for some cases at the moment. It's a > bit tricky to get it right but I think I'm making progress! Great stuff - thanks John! Basically when it can't find an exact match, we need to add rules to use the closest numeric types, auto up casting from smaller numbers to larger numbers. I think there's a few issues in JIRA for this bug... http://jira.codehaus.org/browse/GROOVY-467 James ------- http://radio.weblogs.com/0112098/
, and then
>> couldn't
>> be demoted to double when it came time to call the method? [that's my
>> theory
>> anyway]
>>
>> So I changed the code to this:
>> double doubleParam = 60000D;
>> someInheritedMethod(anObject, doubleParam.doubleValue());
>>
>> This variant caused a syntax error on the 'D' : not sure if that's a
>> fair
>> cop or not, but changing it to:
>> double doubleParam = 60000.0D;
>> someInheritedMethod(anObject, doubleParam.doubleValue());
>>
>> Worked fine [perhaps I don't need the .doubleValue() part]
>>
>> 2 questions:
>>
>> 1. Is my theory about why the first code failed correct? Does that
>> seem
>> reasonable given I was statically typing?
>> 2. Should 60000D really be a syntax error, whereas 60000.0D is not?
>>
>> Yeah I know that is 3 questions, but anyway ....
>>
>>
>
> I think it should have worked OK the first time.
>
> I'm currently looking at the way in which the runtime selects methods
> to call. It is certainly broken for some cases at the moment. It's a
> bit tricky to get it right but I think I'm making progress!
Great stuff - thanks John! Basically when it can't find an exact match,
we need to add rules to use the closest numeric types, auto up casting
from smaller numbers to larger numbers.
I think there's a few issues in JIRA for this bug...
RSS Feed