[jira] [Created] (VELOCITY-996) Precision loss when Long values are converted to BigDecimal in VTL arithmetic

Kai Bächle (Jira) <[email protected]> Sat, 13 Jun 2026 13:46:00 +0000 (UTC)
Newsgroups gmane.comp.jakarta.velocity.devel
Message-ID <[email protected]>
Kai B=C3=A4chle created VELOCITY-996:
-----------------------------------

             Summary: Precision loss when Long values are converted to BigD=
ecimal in VTL arithmetic
                 Key: VELOCITY-996
                 URL: https://issues.apache.org/jira/browse/VELOCITY-996
             Project: Velocity
          Issue Type: Bug
          Components: Engine
    Affects Versions: 2.4.1
            Reporter: Kai B=C3=A4chle


MathUtils.toBigDecimal(Number) appears to convert Number instances through =
doubleValue() before creating a BigDecimal.

This loses precision for large integral values such as Long.MAX_VALUE. As a=
 result, VTL arithmetic and comparisons involving a Long and a BigDecimal c=
an produce unexpected results.

Minimal VTL example, without any Java context:

=C2=A0
{code:java}
## Pure VTL test for MathUtils.toBigDecimal(Long) precision issue

#set($L =3D 9223372036854775807) ## Long.MAX_VALUE =3D> Long
#set($BI =3D 9223372036854775808) ## one above Long.MAX_VALUE =3D> BigInteg=
er

## Create BigDecimal values without Java context:
#set($BD0 =3D ($BI + 0.0) - $BI) ## BigDecimal 0.0
#set($BDL =3D $BI - 1.0) ## BigDecimal 9223372036854775807.0

Long literal (L): $L ($L.class.simpleName)
BigInteger literal (BI): $BI ($BI.class.simpleName)
BigDecimal zero (BD0): $BD0 ($BD0.class.simpleName)
BigDecimal Long.MAX (BDL): $BDL ($BDL.class.simpleName)

-- OK: pure integer arithmetic --
#set($a =3D $L + 0)
L + 0 =3D $a ($a.class.simpleName)

#set($b =3D $L + 1)
L + 1 =3D $b ($b.class.simpleName)

-- Double path, precision loss is expected here --
#set($c =3D $L + 0.0)
L + 0.0 =3D $c ($c.class.simpleName)

-- BigDecimal path, unexpected precision loss --
#set($d =3D $L + $BD0)
L + BigDecimal(0.0) =3D $d ($d.class.name)

-- Comparison against exact BigDecimal Long.MAX_VALUE --
L =3D=3D BigDecimal(Long.MAX) =3D #if($L =3D=3D $BDL)true#{else}false#end
L > BigDecimal(Long.MAX) =3D #if($L > $BDL)true#{else}false#end{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)