Change mod() to always return positive number (#6179)

* Change `mod()` to always return positive number

Closes #6178

ChangeLog: The mod function was changed to always return a positive value (#6178)
This commit is contained in:
Olivier Goffart 2024-09-17 11:37:32 +02:00 committed by GitHub
parent ca64212369
commit 02467bd4cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 30 additions and 10 deletions

View file

@ -3297,7 +3297,7 @@ fn compile_builtin_function_call(
}
BuiltinFunction::Mod => {
ctx.generator_state.conditional_includes.cmath.set(true);
format!("std::fmod({}, {})", a.next().unwrap(), a.next().unwrap())
format!("([](float a, float b) {{ return a >= 0 ? std::fmod(a, b) : std::fmod(a, b) + std::abs(b); }})({},{})", a.next().unwrap(), a.next().unwrap())
}
BuiltinFunction::Round => {
ctx.generator_state.conditional_includes.cmath.set(true);

View file

@ -2713,7 +2713,10 @@ fn compile_builtin_function_call(
quote!(sp::animation_tick())
}
BuiltinFunction::Debug => quote!(slint::private_unstable_api::debug(#(#a)*)),
BuiltinFunction::Mod => quote!((#(#a as f64)%*)),
BuiltinFunction::Mod => {
let (a1, a2) = (a.next().unwrap(), a.next().unwrap());
quote!(sp::Euclid::rem_euclid(&(#a1 as f64), &(#a2 as f64)))
}
BuiltinFunction::Round => quote!((#(#a)* as f64).round()),
BuiltinFunction::Ceil => quote!((#(#a)* as f64).ceil()),
BuiltinFunction::Floor => quote!((#(#a)* as f64).floor()),