Repository navigation
Fix compilation with precision=double - #1527
Conversation
Several call sites mixed float literals with real_t values in single-type templates, which fails to deduce T once real_t is double: Math::pow(T, T), the 3-argument Math::is_equal_approx(T, T, T), std::clamp and std::max. Cast the literals to real_t, which is a no-op in single precision. Also replace brace-initialized Colors that narrow real_t to float, which GCC only warns about but Clang rejects. Fixes Redot-Engine#1526. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B7GMHzgkmsQbz3CYaSrxxS
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughThe changes adjust numeric argument types in math and editor calls, including normalization comparisons, grid calculations, slider bounds, and decal sizing. They also change several ChangesDouble-precision type consistency
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Medium Suggested reviewers: Merge Risk: ⚪ Minimal · up to
|
Arctis-Fireblight
left a comment
There was a problem hiding this comment.
LGTM, but I would like for @OldDev78 to look it over as well since this PR also touches Worldscape.
DaveTheEggman
left a comment
There was a problem hiding this comment.
Math, gridmap & canvas item changes LGTM
OldDev78
left a comment
There was a problem hiding this comment.
Indeed, Most of these changes were brought over from ReX, where precision=double was not intended to be supported. As such, I neglected support for double types both in the math templates and in Color back then. An unfortunate oversight.
Thank you for providing this nice minimal fix.
Checked the fixes for math and WorldScape3D. LGTM.
Fixes #1526.
Problem
The editor doesn't compile with
precision=double. With GCC, it fails in 18 places. With Clang, which macOS, iOS, Android and Web builds use, it fails in another 10.Cause
There are two kinds of error, both from mixing
floatwithreal_t. In a double build,real_tisdouble.Math::pow(T, T), the 3-argumentMath::is_equal_approx(T, T, T),std::clampandstd::maxtake a single typeTfor every argument. A call likeMath::pow(2.f, static_cast<real_t>(n))can't deduceT. These come from the math refactor that made these functions single-type templates (d890d7d), plus call sites added in 2ceb95e and 237a7e0.Color{ x, ... }with areal_tvalue narrowsdoubletofloatinside braces, which is ill-formed. GCC only warns (-Wnarrowing), while Clang rejects it (-Wc++11-narrowing). These were added with WorldScape3D (237a7e0).Fix
These are minimal casts, and none of them changes anything in single precision, where
real_tisfloat.core/math/vector2.cpp,vector3.h,quaternion.cpp(is_normalized()):1.fbecomesstatic_cast<real_t>(1.f).editor/scene/canvas_item_editor_plugin.cpp(9 sites):Math::pow(2.f, ...)becomesMath::pow(static_cast<real_t>(2.f), ...). This matches line 486 of the same file, which already does it.modules/gridmap/editor/grid_map_editor_plugin.cpp(3 sites): the base is alreadyreal_t, becauseMAXof0.fand areal_tgivesreal_t. So only the2.fexponent is cast.modules/worldscape_3d/editor/ui/double_slider.cpp,worldscape_3d_ui.cpp:403: the float bounds passed tostd::clampandstd::maxare cast toreal_t.modules/worldscape_3d/editor/menu/channel_packer.cpp,worldscape_3d_ui.cpp:638/647/650:Color{ ... }becomesColor( ... ), anddivbecomesfloat.divis an int product and is only used to build aColor.Colorhas a single 4-argument constructor,Color(float, float, float, float), so parentheses select the same constructor as braces did, and the values match what GCC already produced.I kept explicit casts rather than making the math templates accept mixed types again. The comment on
Math::powasks for explicit casts, andbasis.cpp:128already does the same foris_equal_approx.Testing
All builds are
platform=linuxbsdon CachyOS, with GCC 16.2.1 and Clang 23.1.1.precision=double: editor (tests=yes)use_llvm=yes),precision=double: editor,template_debug,template_release, editor withmodule_mono_enabled=yesgd_mono.cpp--headless --test)precision=single, GCC and ClangBefore this PR, I also did full GCC double-precision builds of
template_debug,template_releaseand the .NET editor, with glue and assemblies, using an earlier version of these fixes. They needed no changes beyond the sites fixed here.I also tested the double-precision editor build manually.
I couldn't compile the non-Linux platform code here. I checked it by searching for the same patterns, and found nothing else that would fail.
Follow-up suggestion
No CI job builds with
precision=double, which is how these went unnoticed. A double-precision build in CI, preferably with Clang since it catches the narrowing errors, would prevent regressions. I've left that out of this PR.AI disclosure
Per
AI_POLICY.md: I used Claude Code to find and fix these errors, run the builds and tests, and draft this description. The change was also put through an AI review before submission, which found the Clang-only narrowing errors. I tested the change manually before submitting.🤖 Generated with Claude Code
Summary by CodeRabbit