tdf#162711 - VBASupport requires banker’s rounding for integer conversions

Change-Id: Ife3f7aae3ba094fe83a5eda6b3d50d9358cc1184
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/194286
Tested-by: Jenkins
Reviewed-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
This commit is contained in:
Andreas Heinisch
2025-11-24 20:42:41 +01:00
parent 5607c17930
commit 76aafa18b7
4 changed files with 19 additions and 12 deletions
+5 -7
View File
@@ -23,13 +23,11 @@ Sub verify_testCInt()
TestUtil.AssertEqual(CInt(-1.9), -2, "CInt(-1.9)")
TestUtil.AssertEqual(CInt(0.2), 0, "CInt(0.2)")
REM In excel:
REM If the fraction is less than or equal to .5, the result will round down.
REM If the fraction is greater than .5, the result will round up.
REM TestUtil.AssertEqual(CInt(0.5), 0, "CInt(0.5)")
REM TestUtil.AssertEqual(CInt(1.5), 2, "CInt(1.5)")
REM TestUtil.AssertEqual(CInt(2.5), 2, "CInt(2.5)")
' tdf#162711 - VBASupport requires banker’s rounding for integer conversions
TestUtil.AssertEqual(CInt(0.5), 0, "CInt(0.5)")
TestUtil.AssertEqual(CInt(1.5), 2, "CInt(1.5)")
TestUtil.AssertEqual(CInt(2.5), 2, "CInt(2.5)")
TestUtil.AssertEqual(CInt(3.5), 4, "CInt(3.5)")
TestUtil.AssertEqual(CInt(10.51), 11, "CInt(10.51)")
TestUtil.AssertEqual(CInt("&H75FF"), 30207, "CInt(""&H75FF"")")
+5 -4
View File
@@ -22,10 +22,11 @@ Sub verify_testCLng()
TestUtil.AssertEqual(CLng(-1.9), -2, "CLng(-1.9)")
TestUtil.AssertEqual(CLng(0.2), 0, "CLng(0.2)")
REM TestUtil.AssertEqual(CLng(0.5), 0, "CLng(0.5)")
REM If the fraction is less than or equal to .5, the result will round down.
REM If the fraction is greater than .5, the result will round up.
' tdf#162711 - VBASupport requires banker’s rounding for integer conversions
TestUtil.AssertEqual(CLng(0.5), 0, "CLng(0.5)")
TestUtil.AssertEqual(CLng(1.5), 2, "CLng(1.5)")
TestUtil.AssertEqual(CLng(2.5), 2, "CLng(2.5)")
TestUtil.AssertEqual(CLng(3.5), 4, "CLng(3.5)")
TestUtil.AssertEqual(CLng(10.51), 11, "CLng(10.51)")
TestUtil.AssertEqual(CLng("&H75FF"), 30207, "CLng(""&H75FF"")")
+4
View File
@@ -26,6 +26,10 @@ Sub verify_testMid()
TestUtil.AssertEqual(Mid(start:=6, string:="LibreOffice" ), "Office", "Mid() with 2 keyword names" )
TestUtil.AssertEqual(Mid(length:=5, start:=1, string:="LibreOffice" ), "Libre", "Mid() with 3 keyword names" )
' tdf#162711 - VBASupport requires banker’s rounding for integer conversions
TestUtil.AssertEqual(Mid("abc", 2.5, 1), "b", "Mid(""abc"", 2.5, 1)")
TestUtil.AssertEqual(Mid("abcd", 3.5, 1), "d", "Mid(""abcd"", 3.5, 1)")
Exit Sub
errorHandler:
TestUtil.ReportErrorHandler("verify_testMid", Err, Error$, Erl)
+5 -1
View File
@@ -24,6 +24,7 @@
#include <basic/sbx.hxx>
#include <basic/sbxcore.hxx>
#include <basic/sbxdef.hxx>
#include <runtime.hxx>
#include <o3tl/float_int_conversion.hxx>
#include <o3tl/safeint.hxx>
@@ -34,7 +35,10 @@ class SbxArray;
template <std::integral I> I ConvertWithOverflowTo(double f)
{
f = rtl::math::round(f);
// tdf#162711 - VBASupport mode requires banker’s rounding for integer conversions
f = rtl_math_round(f, 0,
SbiRuntime::isVBAEnabled() ? rtl_math_RoundingMode_HalfEven
: rtl_math_RoundingMode_Corrected);
if (!o3tl::convertsToAtMost(f, std::numeric_limits<I>::max()))
{
SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);