Skip to content

add remap_default (issue 1157)#1216

Open
Tekkitslime wants to merge 8 commits intoRedot-Engine:masterfrom
Tekkitslime:master
Open

add remap_default (issue 1157)#1216
Tekkitslime wants to merge 8 commits intoRedot-Engine:masterfrom
Tekkitslime:master

Conversation

@Tekkitslime
Copy link

@Tekkitslime Tekkitslime commented Mar 5, 2026

This commit adds remap_default, which has an extra argument that is returned if istart == istop, that is, if there is a division by zero in remap, it returns the default argument.

Summary by CodeRabbit

  • New Features

    • Added remap_default() — safely remaps numeric values between ranges, returning a provided fallback when the result would be infinite or NaN.
  • Documentation

    • Documented remap_default() with signature, behavior notes, and usage examples; updated remap entry to reference the safer variant.
  • Tests

    • Added tests covering remap_default() across types, edge cases, infinities and NaN.

@coderabbitai
Copy link

coderabbitai bot commented Mar 5, 2026

Walkthrough

Adds a new VariantUtilityFunctions::remap_default(value, istart, istop, ostart, ostop, default_value) that remaps a value between ranges and returns the provided default when the result is infinite or NaN. Changes include implementation, header declaration, binding registration, documentation, and tests.

Changes

Cohort / File(s) Summary
Function Definition & Declaration
core/variant/variant_utility.cpp, core/variant/variant_utility.h
Added static remap_default() implementation and declaration; uses core/math/math_funcs.h; registered in variant utility bindings as a MATH function.
Documentation
doc/classes/@GlobalScope.xml
Added remap_default(value, istart, istop, ostart, ostop, default_value) documentation, examples, and updated remap() docs to reference the new safe variant.
Tests
tests/core/variant/test_variant_utility.h
Added templated tests for remap_default() (float/double) covering boundaries, infinities, NaN, and fallback behavior; added core/math/math_defs.h include.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'add remap_default (issue 1157)' clearly and concisely summarizes the main change: adding a new remap_default function to handle edge cases in remapping.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
core/variant/variant_utility.cpp (1)

559-564: Add targeted tests for both remap_default branches.

This introduces new edge-case behavior; please add coverage for (1) istart == istop fallback and (2) normal remap passthrough to prevent regressions.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@core/variant/variant_utility.cpp` around lines 559 - 564, Add unit tests
covering VariantUtilityFunctions::remap_default for both branches: one test
where istart == istop verifies the function returns the provided default_value
for representative inputs, and another test where istart != istop verifies it
delegates to Math::remap by asserting the expected remapped numeric result
(include multiple cases like values inside and outside the input range). Place
tests in the existing variant utility test suite and use the same numeric
precision/assertion helpers used elsewhere for floating comparisons.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@core/variant/variant_utility.cpp`:
- Around line 559-564: Add unit tests covering
VariantUtilityFunctions::remap_default for both branches: one test where istart
== istop verifies the function returns the provided default_value for
representative inputs, and another test where istart != istop verifies it
delegates to Math::remap by asserting the expected remapped numeric result
(include multiple cases like values inside and outside the input range). Place
tests in the existing variant utility test suite and use the same numeric
precision/assertion helpers used elsewhere for floating comparisons.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: bd3518ed-e105-487d-bf3a-56cdccf4f01b

📥 Commits

Reviewing files that changed from the base of the PR and between dde68ad and 77cd24e.

📒 Files selected for processing (3)
  • core/variant/variant_utility.cpp
  • core/variant/variant_utility.h
  • doc/classes/@GlobalScope.xml

@JoltedJon JoltedJon linked an issue Mar 7, 2026 that may be closed by this pull request
@JoltedJon
Copy link
Contributor

@Tekkitslime Would you mind adding in some unit tests for this new function?

You can add the test to tests/core/variant/test_variant_utility.h

You have to compile the project with unit tests enabled to test it by doing scons tests=yes and then running the project with ./bin/<redot_binary> --test

Copy link
Contributor

@Arctis-Fireblight Arctis-Fireblight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tekkitslime I agree with Jon, can you add unit tests please?

Also, I know that in the original issue, istart and istop were equal, but I am not sure if this is the only potential error condition for remap(). We might want to either add additional guards / checks, or just run the remap() function and just return the default value if we happen to hit an error condition. Thoughts?

@Tekkitslime
Copy link
Author

There are more error conditions? like what? Maybe passing one of the arguments as NaN or Inf? Yeah i think you have a point, i should just check if remap gives a bad result and only then return the default value!

Tekkitslime and others added 4 commits March 9, 2026 01:00
…te, changed the documentation to reflect this change, and added a few test cases to test_variant_utility, and they work on my machine
Basically, i messed up the branches, this is the remap_default feature branch :P
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@doc/classes/`@GlobalScope.xml:
- Around line 1117-1123: The docs for remap_default currently only mention INF
and NAN, but the implementation uses Math::is_finite() to catch any non-finite
results; update the description of remap_default to indicate it returns
default_value for any non-finite result (including -INF, +INF, and NAN) rather
than only INF/NAN, and optionally use the term "non-finite" for clarity; refer
to remap_default, remap, and Math::is_finite() when editing the text so the
behavior matches the core/variant handling.

In `@tests/core/variant/test_variant_utility.h`:
- Around line 134-147: Run clang-format on the modified test block for the
TEST_CASE_TEMPLATE named "[VariantUtility] remap_default" (the checks calling
VariantUtilityFunctions::remap_default) and commit the resulting formatted
changes so pre-commit no longer rewrites this section; ensure the committed
change exactly matches the CI's expected formatting output for that test block.
- Line 35: tests/core/variant/test_variant_utility.h now includes
"core/math/math_defs.h" which the build cannot resolve; either remove the
unnecessary include from test_variant_utility.h (if math_defs.h is unused) or
correct the include to the project's canonical header path and/or add the
directory to the test target's include paths so the compiler can find
core/math/math_defs.h; locate the include line in test_variant_utility.h and
either delete it or update it and the target's include settings so the header
resolves.
- Around line 139-146: Add a test for the collapsed-range NaN branch where value
== istart == istop so the fallback default is returned: add a CHECK calling
VariantUtilityFunctions::remap_default(100.0, 100.0, 100.0, 0.0, 1000.0, -99.0)
and assert it equals doctest::Approx(-99.0); this covers the
core/math/math_funcs.h collapsed-range (istart==istop) path referenced around
lines 500-512.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7490bf0c-d174-41da-8316-a66bd4baddad

📥 Commits

Reviewing files that changed from the base of the PR and between 77cd24e and 9562abe.

📒 Files selected for processing (3)
  • core/variant/variant_utility.cpp
  • doc/classes/@GlobalScope.xml
  • tests/core/variant/test_variant_utility.h
🚧 Files skipped from review as they are similar to previous changes (1)
  • core/variant/variant_utility.cpp

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (3)
tests/core/variant/test_variant_utility.h (3)

134-147: ⚠️ Potential issue | 🟡 Minor

Commit the clang-format output for this block.

CI is still reporting that pre-commit rewrites this test case, so the branch will keep failing until the formatted result is committed.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/core/variant/test_variant_utility.h` around lines 134 - 147, Run
clang-format on the test block in tests/core/variant/test_variant_utility.h (the
TEST_CASE_TEMPLATE "[VariantUtility] remap_default" that calls
VariantUtilityFunctions::remap_default) and commit the formatted result so
pre-commit no longer rewrites it; ensure the file is re-saved with clang-format
changes applied and include that updated file in the commit.

35-35: ⚠️ Potential issue | 🔴 Critical

Fix the unresolved include before merging.

"core/math/math_defs.h" is still not resolvable from this test target, so the file does not compile as patched. Either remove this dependency or switch to a header path that is actually available to the tests.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/core/variant/test_variant_utility.h` at line 35, The test header
tests/core/variant/test_variant_utility.h currently includes the unresolved
header "core/math/math_defs.h"; either remove that include if the math
definitions are not needed, replace it with a header that is actually exported
to the test target (e.g., the public math header used elsewhere in tests), or
add the appropriate dependency so "core/math/math_defs.h" is available to the
test target; update the include line or test target configuration accordingly
and re-run compilation to confirm the unresolved include is resolved.

139-139: ⚠️ Potential issue | 🟡 Minor

Add the value == istart == istop collapsed-range case.

Line 139 covers the value != istart && istart == istop branch, but core/math/math_funcs.h:500-512 also produces NaN for the 0/0 case when value == istart == istop. core/variant/variant_utility.cpp:560-567 should return the fallback there as well, so this path still needs explicit coverage.

Proposed test addition
 	CHECK(VariantUtilityFunctions::remap_default(150.0, 100.0, 100.0, 0.0, 1000.0, -99.0) == doctest::Approx(-99.0));
+	CHECK(VariantUtilityFunctions::remap_default(100.0, 100.0, 100.0, 0.0, 1000.0, -99.0) == doctest::Approx(-99.0));
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/core/variant/test_variant_utility.h` at line 139, The remap_default
logic must handle the collapsed-range 0/0 case when value == istart == istop to
avoid NaN: update VariantUtilityFunctions::remap_default (in variant_utility.cpp
around the existing istart==istop handling at lines ~560-567) to explicitly
detect istart == istop and value == istart and return the provided fallback
value, and add a unit test in tests/core/variant/test_variant_utility.h
mirroring the existing check (e.g., call remap_default with value, istart, istop
all equal and expect the fallback) so the collapsed-range equal-value path is
covered.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@tests/core/variant/test_variant_utility.h`:
- Around line 134-147: Run clang-format on the test block in
tests/core/variant/test_variant_utility.h (the TEST_CASE_TEMPLATE
"[VariantUtility] remap_default" that calls
VariantUtilityFunctions::remap_default) and commit the formatted result so
pre-commit no longer rewrites it; ensure the file is re-saved with clang-format
changes applied and include that updated file in the commit.
- Line 35: The test header tests/core/variant/test_variant_utility.h currently
includes the unresolved header "core/math/math_defs.h"; either remove that
include if the math definitions are not needed, replace it with a header that is
actually exported to the test target (e.g., the public math header used
elsewhere in tests), or add the appropriate dependency so
"core/math/math_defs.h" is available to the test target; update the include line
or test target configuration accordingly and re-run compilation to confirm the
unresolved include is resolved.
- Line 139: The remap_default logic must handle the collapsed-range 0/0 case
when value == istart == istop to avoid NaN: update
VariantUtilityFunctions::remap_default (in variant_utility.cpp around the
existing istart==istop handling at lines ~560-567) to explicitly detect istart
== istop and value == istart and return the provided fallback value, and add a
unit test in tests/core/variant/test_variant_utility.h mirroring the existing
check (e.g., call remap_default with value, istart, istop all equal and expect
the fallback) so the collapsed-range equal-value path is covered.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 102310bf-c258-4293-b5ca-2fcb2798881d

📥 Commits

Reviewing files that changed from the base of the PR and between 9562abe and f5fccc5.

📒 Files selected for processing (1)
  • tests/core/variant/test_variant_utility.h

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (3)
doc/classes/@GlobalScope.xml (1)

1117-1122: ⚠️ Potential issue | 🟡 Minor

Document this as “non-finite,” not just INF/NAN.

The implementation falls back on any non-finite result, so this description is still too narrow: -INF also returns default_value.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@doc/classes/`@GlobalScope.xml around lines 1117 - 1122, Update the
documentation for remap_default to state that it returns default_value when the
remap would produce a non-finite result (including INF, -INF, or NAN) instead of
only mentioning INF/NAN; keep the existing examples and cross-references to
remap, lerp, inverse_lerp and clamp, but replace the narrow wording with the
term “non-finite” so readers understand negative infinity is also covered.
tests/core/variant/test_variant_utility.h (2)

35-35: ⚠️ Potential issue | 🔴 Critical

Fix the unresolved math_defs.h include.

Line 35 still adds a header this test target can't resolve, so the suite does not compile. Remove it if it's unnecessary here, or switch to a header that is already available on this target's include path.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/core/variant/test_variant_utility.h` at line 35, The test target fails
to compile because tests/core/variant/test_variant_utility.h includes an
unresolved header "core/math/math_defs.h"; remove that include from
test_variant_utility.h if the test does not need it, or replace it with a header
that is present on this test target's include path (i.e., use an existing public
math header already available to the tests) so the unresolved symbol math_defs.h
is no longer referenced.

139-146: ⚠️ Potential issue | 🟡 Minor

Add the collapsed-range NaN case too.

The current block covers the istart == istop && value != istart path, but it still misses value == istart == istop, which goes through the 0 / 0 branch and should also return the fallback.

➕ Suggested test
 	CHECK(VariantUtilityFunctions::remap_default(150.0, 100.0, 100.0, 0.0, 1000.0, -99.0) == doctest::Approx(-99.0));
+	CHECK(VariantUtilityFunctions::remap_default(100.0, 100.0, 100.0, 0.0, 1000.0, -99.0) == doctest::Approx(-99.0));
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/core/variant/test_variant_utility.h` around lines 139 - 146, Add a test
covering the collapsed-range where value == istart == istop and those are NaN so
the function hits the 0/0 branch and should return the fallback; update tests in
tests/core/variant/test_variant_utility.h by adding a CHECK calling
VariantUtilityFunctions::remap_default with value, istart and istop all set to
NAN (and the same fallback -99.0) and assert it equals doctest::Approx(-99.0).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@doc/classes/`@GlobalScope.xml:
- Around line 1117-1122: Update the documentation for remap_default to state
that it returns default_value when the remap would produce a non-finite result
(including INF, -INF, or NAN) instead of only mentioning INF/NAN; keep the
existing examples and cross-references to remap, lerp, inverse_lerp and clamp,
but replace the narrow wording with the term “non-finite” so readers understand
negative infinity is also covered.

In `@tests/core/variant/test_variant_utility.h`:
- Line 35: The test target fails to compile because
tests/core/variant/test_variant_utility.h includes an unresolved header
"core/math/math_defs.h"; remove that include from test_variant_utility.h if the
test does not need it, or replace it with a header that is present on this test
target's include path (i.e., use an existing public math header already
available to the tests) so the unresolved symbol math_defs.h is no longer
referenced.
- Around line 139-146: Add a test covering the collapsed-range where value ==
istart == istop and those are NaN so the function hits the 0/0 branch and should
return the fallback; update tests in tests/core/variant/test_variant_utility.h
by adding a CHECK calling VariantUtilityFunctions::remap_default with value,
istart and istop all set to NAN (and the same fallback -99.0) and assert it
equals doctest::Approx(-99.0).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4a3ffce3-1f10-4cd5-96b6-5c20f155c1a2

📥 Commits

Reviewing files that changed from the base of the PR and between 7ff8093 and 391bdad.

📒 Files selected for processing (4)
  • core/variant/variant_utility.cpp
  • core/variant/variant_utility.h
  • doc/classes/@GlobalScope.xml
  • tests/core/variant/test_variant_utility.h

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/core/variant/test_variant_utility.h (1)

134-134: Unused template parameter T.

The template parameter T is declared but never used in the test body—all literals are double. This causes the test to run twice with identical behavior. Since remap_default accepts double parameters (per implementation in variant_utility.cpp), consider simplifying to a plain TEST_CASE.

♻️ Suggested simplification
-TEST_CASE_TEMPLATE("[VariantUtility] remap_default", T, float, double) {
+TEST_CASE("[VariantUtility] remap_default") {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/core/variant/test_variant_utility.h` at line 134, The
TEST_CASE_TEMPLATE declaration uses an unused template parameter T causing
duplicate identical tests; change the test from
TEST_CASE_TEMPLATE("[VariantUtility] remap_default", T, float, double) to a
non-templated TEST_CASE so it only runs once and matches remap_default's double
parameters (update the test declaration in
tests/core/variant/test_variant_utility.h and remove any unused T references so
the test body calls remap_default(double) as implemented in
variant_utility.cpp).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/core/variant/test_variant_utility.h`:
- Line 134: The TEST_CASE_TEMPLATE declaration uses an unused template parameter
T causing duplicate identical tests; change the test from
TEST_CASE_TEMPLATE("[VariantUtility] remap_default", T, float, double) to a
non-templated TEST_CASE so it only runs once and matches remap_default's double
parameters (update the test declaration in
tests/core/variant/test_variant_utility.h and remove any unused T references so
the test body calls remap_default(double) as implemented in
variant_utility.cpp).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c0f7e46b-5e52-4e78-8c66-447021d01b07

📥 Commits

Reviewing files that changed from the base of the PR and between 391bdad and e87d25c.

📒 Files selected for processing (1)
  • tests/core/variant/test_variant_utility.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FEAT: Remap function can return nan and inf, Safe overload needed

3 participants