C++: fix mod with negative number again

`mod(-42, 2)` would return 2 instead of 0
This commit is contained in:
Olivier Goffart 2025-03-27 13:41:29 +01:00 committed by GitHub
parent 18b05d0734
commit 3621fbcbd3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View file

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