Bug 141201

Summary: Basic: MOD result is different for values passed as literals vs. using variables
Product: LibreOffice Reporter: Mike Kaganski <mikekaganski>
Component: BASICAssignee: Andreas Heinisch <andreas.heinisch>
Status: RESOLVED FIXED    
Severity: normal CC: andreas.heinisch, himajin100000, rafael.palma.lima
Priority: medium Keywords: needsDevEval
Version: unspecified   
Hardware: All   
OS: All   
See Also: https://bugs.documentfoundation.org/show_bug.cgi?id=141200
https://bugs.documentfoundation.org/show_bug.cgi?id=84435
Whiteboard: target:7.2.0 target:7.1.3
Crash report or crash signature: Regression By:

Description Mike Kaganski 2021-03-23 14:56:50 UTC
> 3 MOD 1.5

=> result is 0

> DIM n
> 3 MOD n

=> result is 1

Obviously in the first case, the literal '1.5' was truncated to 1, while in the second case, it was rounded to 2.

See also tdf#141200, where MOD documentation is discussed.

Ref: https://ask.libreoffice.org/en/question/300133/macro-using-mod-and-variables-produces-wrong-answer/
Comment 1 Mike Kaganski 2021-03-23 16:29:34 UTC
> > DIM n
> > 3 MOD n

Oh, of course I missed a line:

> DIM n
> n = 1.5
> 3 MOD n

It's easier to see the rounding/truncation using '5 MOD 2.5'.
Comment 2 Rafael Lima 2021-04-01 17:48:32 UTC
I can confirm this in LO 7.1.2.2.

Here's a simple example of how this bug may affect results:

Sub ExampleMod
   Dim a As Double, b as Double
   a = 16.4
   b = 5.9
   Print a Mod b 'Prints 4
   Print 16.4 Mod 5.9 'Prints 1
End Sub

The first Print statement returns the correct value (consistent with MS VBA's result).

In the second Print statement it seems that LO Basic only considers the integer part of the operands. So 16.4 turns into 16 and 5.9 turns into 5, then 16 MOD 5 returns 1.

I am changing this to NEW.
Comment 3 Mike Kaganski 2021-04-05 06:31:12 UTC
Oh - this is actually a regression from tdf#84435, which was a misguided incorrect change...
Comment 4 Commit Notification 2021-04-05 14:39:47 UTC
Andreas Heinisch committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/a9fcd2cf2bfb42693787e8c4197e5988e155e235

tdf#141201 - Round MOD literals to Integer values

It will be available in 7.2.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 5 Commit Notification 2021-04-06 09:44:08 UTC
Andreas Heinisch committed a patch related to this issue.
It has been pushed to "libreoffice-7-1":

https://git.libreoffice.org/core/commit/6fa9053b1bdc69e78d4403975d7623eb22b05898

tdf#141201 - Round MOD literals to Integer values

It will be available in 7.1.3.

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.