Fix POWER formula returning the base for a zero exponent#3614
Open
Osamaali313 wants to merge 1 commit into
Open
Fix POWER formula returning the base for a zero exponent#3614Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
The `POWER(base, exponent)` formula function reads its exponent with a
truthy fallback:
const exponent = params[1]?.value || 1;
return Math.pow(value, exponent);
`POWER(x, 0)` must return 1 (x^0 = 1), but `0 || 1` evaluates to 1, so the
exponent silently becomes 1 and the function returns the base `x` instead.
`validateParams` throws when fewer than 2 params are given, so `params[1]`
always exists at eval time — the `|| 1` default is never needed for a
missing argument and its only reachable effect is corrupting an explicitly
passed `0`. The sibling `Ceiling`/`Floor` functions use `params[1]?.value || 0`,
which is harmless there because their default equals the falsy value; `Power`
is the one case where the default (1) differs from the meaningful falsy input
(0). Use `??` so an explicit `0` exponent is honored.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
POWER(base, exponent)formula function (packages/core/src/formula/functions/numeric.ts) reads its exponent with a truthy fallback:POWER(x, 0)must return1(the identity x⁰ = 1), but0 || 1evaluates to1, so the exponent silently becomes1and the function returns the basexinstead of1.validateParamsthrows when fewer than 2 params are supplied, soparams[1]always exists at eval time — the|| 1"default" is never needed for a missing argument, and its only reachable effect is corrupting an explicitly-passed0.Evidence (siblings prove intent)
CeilingandFloorin the same file useparams[1]?.value || 0— harmless because their default (0) equals the falsy value.Poweris the one case where the default (1) differs from the meaningful falsy input (0), so|| 1can only ever misfire.Reproduction
POWER(2, 0)POWER(10, 0)POWER(2, 3)Fix
??preserves the null-safety intent while honoring an explicit0. Existing tests (numeric.spec.ts) only cover exponents 2 and -2, so the zero case was unguarded.