Allow dur/dur division (#3812)

It is perfectly valid to divide two durations, it is a ratio with no
unit.
This commit is contained in:
Ram-Z 2025-03-12 20:48:49 +00:00 committed by GitHub
parent 5c67d22540
commit f73b42d23f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 9 deletions

View file

@ -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<double>(_duration) / static_cast<double>(right._duration);
break;
}
break;
}

View file

@ -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;
}