groovy

BigDecimal unnecessarily coerced into double on method invocation

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0-JSR-5
  • Fix Version/s: 1.0-JSR-6
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    0

Description

This bug occurs in MetaClassHelper.java on line 393. It unnecessarily creates a new BigDecimal when one was already passed in. Calling doubleValue() changes the original value.

Java Class:
import java.math.BigDecimal;
class BigDecimalEcho {
public static BigDecimal echoX ( BigDecimal x, BigDecimal y) { return x; }
}

GroovyTest:
import BigDecimalEcho

println BigDecimalEcho.echoX(9.95, 1.0)
println BigDecimalEcho.echoX(9.95, 1)

Output:
9.95
9.949999999999999289457264239899814128875732421875

Activity

Hide
blackdrag blackdrag added a comment -

I think the whole coercion mechanism need a rework, but for now the patch is easy. It's line 402 btw,

1) the whole ifs should be transformed in if-else-if since, there are no cases that are covered by mnore than one if, so why go through all them?

2)
if (param == BigDecimal.class ) { ans[i] = new BigDecimal(((Number)argument).doubleValue()); coerced = true; continue; }

shoud be changed to:

if (param == BigDecimal.class ) {
if (argument instanceof BigDecimal) { ans[i] = argument; } else { ans[i] = new BigDecimal(((Number)argument).doubleValue()); }
coerced = true; continue;
}

4) is it really a coercion to not to convert a BigDecimal?

5) BigInteger does have just the same bug.

Show
blackdrag blackdrag added a comment - I think the whole coercion mechanism need a rework, but for now the patch is easy. It's line 402 btw, 1) the whole ifs should be transformed in if-else-if since, there are no cases that are covered by mnore than one if, so why go through all them? 2) if (param == BigDecimal.class ) { ans[i] = new BigDecimal(((Number)argument).doubleValue()); coerced = true; continue; } shoud be changed to: if (param == BigDecimal.class ) { if (argument instanceof BigDecimal) { ans[i] = argument; } else { ans[i] = new BigDecimal(((Number)argument).doubleValue()); } coerced = true; continue; } 4) is it really a coercion to not to convert a BigDecimal? 5) BigInteger does have just the same bug.
Hide
blackdrag blackdrag added a comment -

should be fixed now. I have reorganized the coercion mechanism.

Show
blackdrag blackdrag added a comment - should be fixed now. I have reorganized the coercion mechanism.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: