Bug 56315 - [PATCH] Floating point rounding problems with ROUND function
Summary: [PATCH] Floating point rounding problems with ROUND function
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.10-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-25 22:21 UTC by Jostein Tveit
Modified: 2015-03-14 15:00 UTC (History)
0 users



Attachments
Simple Java program and Excel sheet to reproduce the bug (6.38 KB, application/x-zip-compressed)
2014-03-25 22:21 UTC, Jostein Tveit
Details
[PATCH] Patch against trunk to solve this bug (6.24 KB, patch)
2014-03-28 12:22 UTC, Jostein Tveit
Details | Diff
The patch including unit test against excel spreadsheet (6.24 KB, application/x-gzip)
2014-03-28 12:30 UTC, Jostein Tveit
Details
The patch as text file (1.65 KB, patch)
2014-03-28 12:31 UTC, Jostein Tveit
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jostein Tveit 2014-03-25 22:21:18 UTC
Created attachment 31438 [details]
Simple Java program and Excel sheet to reproduce the bug

When running the attached code I get the following output:
A1 numeric: 0.049999999999999975
A3 numeric: 0.0
A3 text: 0
A3 formatted: 0
A4 concatenated: test 0

If I open the spreadsheet in Excel it displays:
A1: 0.05
A3: 0.1
A4: test 0.1


The ROUND function in cell A3 returns 0.0 in POI, while it returns 0.1 in Excel.
I assume this is because of the problems with storing exact floating point numbers, but I really think POI and excel should return the same result.

When the value is used in a CONCATENATE function in cell A4, the result from POI also differs from Excel.
Comment 1 Jostein Tveit 2014-03-28 12:22:53 UTC
Created attachment 31453 [details]
[PATCH] Patch against trunk to solve this bug

I fixed this bug by using

new java.math.BigDecimal(NumberToTextConverter.toText(n))

instead of

java.math.BigDecimal.valueOf(n)

All other unit tests goes green, so I assume this solution is ok.
Comment 2 Jostein Tveit 2014-03-28 12:30:25 UTC
Created attachment 31454 [details]
The patch including unit test against excel spreadsheet

I'm adding the patch and the tar.gz separately
Comment 3 Jostein Tveit 2014-03-28 12:31:14 UTC
Created attachment 31455 [details]
The patch as text file

I'm adding the patch and the tar.gz separately
Comment 4 Dominik Stadler 2014-04-18 18:22:26 UTC
Thanks for the patch, this is applied with r1588538 with some additional testing in TestMathX.
Comment 5 Dominik Stadler 2015-03-14 15:00:13 UTC
Why was this bug reopened? Please provide some useful explanation if you believe a bug is not fixed. 

Before reopening bugs, please make sure to verify the problem with latest trunk builds (either from source or nightly snapshot builds) as fixes usually only become available as part of releases after quite some time.