[jira] [Resolved] (VELOCITY-996) Precision loss when Long values are converted to BigDecimal in VTL arithmetic
"Claude Brisson (Jira)" <[email protected]> Sat, 13 Jun 2026 18:01:00 +0000 (UTC)
| Newsgroups | gmane.comp.jakarta.velocity.devel |
|---|---|
| Message-ID | <[email protected]> |
[ https://issues.apache.org/jira/browse/VELOCITY-996?page=3Dcom.atlass=
ian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Claude Brisson resolved VELOCITY-996.
-------------------------------------
Fix Version/s: 2.5
Assignee: Claude Brisson
Resolution: Fixed
PR 65 merged
> Precision loss when Long values are converted to BigDecimal in VTL arithm=
etic
> -------------------------------------------------------------------------=
----
>
> 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
> Assignee: Claude Brisson
> Priority: Minor
> Fix For: 2.5
>
>
> MathUtils.toBigDecimal(Number) appears to convert Number instances throug=
h 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=
can 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> BigInt=
eger
> ## 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)