Bug 142922 - Basic: Round VBA compatibility function implements wrong rounding mode
Summary: Basic: Round VBA compatibility function implements wrong rounding mode
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Baltasar
URL:
Whiteboard: target:7.3.0 target:7.2.0.0.beta2
Keywords: difficultyBeginner, easyHack, implementationError, skillCpp
Depends on:
Blocks: Macro-VBA
  Show dependency treegraph
 
Reported: 2021-06-17 19:45 UTC by Mike Kaganski
Modified: 2021-08-06 04:02 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:
Regression By:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Kaganski 2021-06-17 19:45:02 UTC
As explained in VBA documentation for Round [1]:

> This VBA function returns something commonly referred to as bankers rounding.

[MS-VBAL] [2] defined Banker's rounding as alternative name for "round-to-even"; it is one of rounding modes defined in IEEE 754, its recommended rounding mode for decimal [3].

[1] also provides some examples demonstrating the values returned bu VBA's Round:

> ?Round(0.12335,4)
>  0,1234
> ?Round(0.12345,4)
>  0,1234
> ?Round(0.12355,4)
>  0,1236
> ?Round(0.12365,4)
>  0,1236

However, LibreOffice's Round function [4] implementation [5] does not implement the required rounding mode. Executing the example calculations mentioned above gives 0.1234, 0.1235, 0.1236, 0.1237.

FTR: we have a rtl_math_round function, that accepts a flag defining rounding mode, rtl_math_RoundingMode_HalfEven among the possible values, which may be used in the VBA function implementation.

[1] https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/round-function

[2] https://docs.microsoft.com/en-us/openspecs/microsoft_general_purpose_programming_languages/ms-vbal/98152b5a-4d86-4acb-b875-66cb1f49433e

[3] https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules

[4] https://help.libreoffice.org/7.2/en-US/text/sbasic/shared/03170000.html?DbPAR=BASIC

[5] https://opengrok.libreoffice.org/xref/core/basic/source/runtime/methods1.cxx?r=86d298f8&mo=63707&fi=2377#2377

[6] https://opengrok.libreoffice.org/xref/core/sal/rtl/math.cxx?r=49af7e22&mo=38462&fi=1130#1130
Comment 1 Mike Kaganski 2021-06-17 19:57:03 UTC
The fix to this easyhack must include a unit test. Please CC me to the respective gerrit change, so that I could review and assist.
Comment 2 b. 2021-06-30 22:21:47 UTC Comment hidden (spam)
Comment 3 Mike Kaganski 2021-06-30 22:25:03 UTC Comment hidden (off-topic)
Comment 4 Baltasar 2021-07-01 13:24:35 UTC
I'll start working on this.
Comment 5 Commit Notification 2021-07-06 12:51:14 UTC
baltasarq committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/0eacbfa75132724faaeeb9ea22a1c3e44eefb515

tdf#142922 fixes Round to follow half-to-even rounding

It will be available in 7.3.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 6 Commit Notification 2021-07-07 20:00:35 UTC
baltasarq committed a patch related to this issue.
It has been pushed to "libreoffice-7-2":

https://git.libreoffice.org/core/commit/70e86db58bd04d919b3f67efad6c94dc967fd900

tdf#142922 fixes Round to follow half-to-even rounding

It will be available in 7.2.0.0.beta2.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.