New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can u fix sum function precision overflow bug when double addition integer #288
Comments
Not sure I understand what the bug is. Can you give an example with JSON input + JSLT code and wrong output? |
This is JSON input [
{
"taskSeqno": "100000265920",
"taskSeqnoFk": "100000396209",
"ramiCode": "11",
"ramiCodeDesc": "22",
"tranStatus": "05",
"tranStatusDesc": "已放款",
"tranDate": "20230101",
"actualAmount": "23000.123",
"actualAmount2": 23000.123,
"lastOperation": "已放款",
"lastOperationCode": "05",
"refundFlag": "N",
"totalReturnAmount": "0",
"refundDate": "20221102",
"tranDesc": "点错了",
"returnTranDesc": "无效果",
"returnAmount": 4000,
"retractStatus": "00",
"retper": "01",
"totTranAmount": 1000.55,
"tranAmount": 1050,
"downPayAmount": 1122.33,
"paymentDProp": 18,
"loanTerm": 12,
"ramiLafratvl": 3.11,
"lafratvl": 1.22,
"discountRate": 5.44,
"factoryRate": 6.55,
"agencyRate": 7.88,
"agencyGroupRate": 8.77,
"ramiCmsn": 4455.66,
"cstCmsn": 3355.55,
"mchtCmsn": 4433.22,
"finalPayment": 7777.55,
"paymentFProp": 45.88,
"isFlexRepymt": "1",
"sxfstage": "1",
"discountFlg": "0",
"discountId": "001"
},
{
"taskSeqno": "100000265920",
"taskSeqnoFk": "100000396209",
"ramiCode": "11",
"ramiCodeDesc": "22",
"tranStatus": "05",
"tranStatusDesc": "已放款",
"tranDate": "20230104",
"actualAmount": "23000",
"actualAmount2": 23000.123,
"lastOperation": "已放款",
"lastOperationCode": "05",
"refundFlag": "N",
"totalReturnAmount": "0",
"refundDate": "20221102",
"tranDesc": "点错了",
"returnTranDesc": "无效果",
"returnAmount": 4000,
"retractStatus": "00",
"retper": "01",
"totTranAmount": 1000.55,
"tranAmount": 1050,
"downPayAmount": 1122.33,
"paymentDProp": 18,
"loanTerm": 12,
"ramiLafratvl": 3.11,
"lafratvl": 1.22,
"discountRate": 5.44,
"factoryRate": 6.55,
"agencyRate": 7.88,
"agencyGroupRate": 8.77,
"ramiCmsn": 4455.66,
"cstCmsn": 3355.55,
"mchtCmsn": 4433.22,
"finalPayment": 7777.55,
"paymentFProp": 45.88,
"isFlexRepymt": "1",
"sxfstage": "1",
"discountFlg": "0",
"discountId": "001"
},
{
"taskSeqno": "100000265920",
"taskSeqnoFk": "100000396209",
"ramiCode": "11",
"ramiCodeDesc": "22",
"tranStatus": "05",
"tranStatusDesc": "已放款",
"tranDate": "20230108",
"actualAmount": "23000",
"actualAmount2": 23000.123,
"lastOperation": "已放款",
"lastOperationCode": "05",
"refundFlag": "N",
"totalReturnAmount": "0",
"refundDate": "20221102",
"tranDesc": "点错了",
"returnTranDesc": "无效果",
"returnAmount": 4000,
"retractStatus": "00",
"retper": "01",
"totTranAmount": 1000.55,
"tranAmount": 1050,
"downPayAmount": 1122.33,
"paymentDProp": 18,
"loanTerm": 12,
"ramiLafratvl": 3.11,
"lafratvl": 1.22,
"discountRate": 5.44,
"factoryRate": 6.55,
"agencyRate": 7.88,
"agencyGroupRate": 8.77,
"ramiCmsn": 4455.66,
"cstCmsn": 3355.55,
"mchtCmsn": 4433.22,
"finalPayment": 7777.55,
"paymentFProp": 45.88,
"isFlexRepymt": "1",
"sxfstage": "1",
"discountFlg": "0",
"discountId": "001"
}
] This is JSON result {
"tranDate" : "20230108",
"customerRate" : 1.22,
"discountAmount" : 6877.5,
"firstPaymentRatio" : 18,
"finalPaymentRatio" : 45.88,
"finalPayment" : 7777.55,
"actualAmount" : 69000.12299999999,
"actualAmount2" : 69000.369,
"loanTerm" : 12,
"totTranAmount" : 1000.55,
"totalRate" : 1000.55,
"totalHandlingFee" : 4455.66
} Such as JSLT script {
"tranDate" : .[size(.) - 1].tranDate,
"customerRate" : .[0].lafratvl,
"discountAmount" : (.[0].tranAmount * .[0].factoryRate),
"firstPaymentRatio" : .[0].paymentDProp,
"finalPaymentRatio" : .[0].paymentFProp,
"finalPayment" : .[0].finalPayment,
"actualAmount": sum([for(.)
number(.actualAmount)]),
"actualAmount2": sum([for(.)
number(.actualAmount2)]),
"loanTerm" : .[0].loanTerm,
"totTranAmount" : .[0].totTranAmount,
"totalRate" : .[0].totTranAmount,
"totalHandlingFee" : .[0].ramiCmsn
} Look at
|
Okay, so your complaint is that
produces If I try this with Python I get:
If I do it in JavaScript I get:
Your proposed solution is to use BigDecimal instead. That will be slower and more memory-intensive, but perhaps it's worth it. People have asked for the same thing before (issue #68). What I wonder is: why do people consider this a problem? Is it because you're just not comfortable with floating numbers behaving they way they do, or is it because there is a real, practical problem involved? |
Think for u solution,This kind of scenario happens in which us want to process the amount field using JSLT script。 |
I wanted to understand what this would look like if the JSON were loaded into a Java process, so I wrote this small program:
The output is:
If I add a few more zeroes the float loses the digits after the decimal completely, but the double keeps working. |
The problem is not limited to the sum() function, because |
You could change the JSTL to do this: {
"actualAmount": round (1000 * sum ([ for (.) number (.actualAmount) ] )) / 1000
} It would still have rounding error. A slow alternative would be
Ugly, I admit. |
No description provided.
The text was updated successfully, but these errors were encountered: