From f73b42d23f6318d8e14d2c7113fcda0865964e4c Mon Sep 17 00:00:00 2001 From: Ram-Z Date: Wed, 12 Mar 2025 20:48:49 +0000 Subject: [PATCH] Allow dur/dur division (#3812) It is perfectly valid to divide two durations, it is a ratio with no unit. --- src/Variant.cpp | 5 +++-- test/variant_divide_test.cpp | 11 ++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Variant.cpp b/src/Variant.cpp index a5b68f172..ef0a08ac3 100644 --- a/src/Variant.cpp +++ b/src/Variant.cpp @@ -75,7 +75,6 @@ #define STRING_VARIANT_DIV_DUR_BOOL "Cannot divide duration by Boolean" #define STRING_VARIANT_DIV_DUR_STR "Cannot divide durations by strings" #define STRING_VARIANT_DIV_DUR_DATE "Cannot divide durations by dates" -#define STRING_VARIANT_DIV_DUR_DUR "Cannot divide durations by durations" #define STRING_VARIANT_MOD_BOOL "Cannot modulo Booleans" #define STRING_VARIANT_MOD_DATE "Cannot modulo date values" #define STRING_VARIANT_MOD_DUR "Cannot modulo duration values" @@ -1760,7 +1759,9 @@ Variant& Variant::operator/=(const Variant& other) { throw std::string(STRING_VARIANT_DIV_DUR_DATE); case type_duration: - throw std::string(STRING_VARIANT_DIV_DUR_DUR); + _type = type_real; + _real = static_cast(_duration) / static_cast(right._duration); + break; } break; } diff --git a/test/variant_divide_test.cpp b/test/variant_divide_test.cpp index c1df0c158..b1fc973d3 100644 --- a/test/variant_divide_test.cpp +++ b/test/variant_divide_test.cpp @@ -302,13 +302,10 @@ int TEST_NAME(int, char**) { } // duration / duration -> duration - try { - Variant v55 = v5 / v5; - t.fail("1200 / 1200 --> error"); - } catch (...) { - t.pass("1200 / 1200 --> error"); - } - + Variant v55 = v5 / v5; + t.is(v55.type(), Variant::type_real, "1200 / 1200 --> real"); + t.is(v55.get_real(), 1.0, "1200 / 1200 --> 1.0"); + t.is((v5 / v52).get_real(), 3.14136, EPSILON, "1200 / 382 --> 3.14136"); return 0; }