[tree,hist] do not silently hide sqrt of negative value in T(Tree)Formula#22756
Open
ferdymercury wants to merge 4 commits into
Open
[tree,hist] do not silently hide sqrt of negative value in T(Tree)Formula#22756ferdymercury wants to merge 4 commits into
ferdymercury wants to merge 4 commits into
Conversation
return nan instead of sqrt(abs(value)) Fixes root-project#22755 Was like that since the early beginnigns https://github.com/root-project/root/blame/dd588ebc2c01182af573ea9ac7ce2ddcbc734b6e/treeplayer/src/TTreeFormula.cxx
return nan instead of sqrt(abs(value)) for consistency with TTreeFormula
Test Results 23 files 23 suites 3d 11h 59m 49s ⏱️ For more details on these failures, see this check. Results for commit 85307bd. |
guitargeek
requested changes
Jul 8, 2026
guitargeek
left a comment
Contributor
There was a problem hiding this comment.
Thank you very much!
What about adding a unit test like this to tree/treeplayer/test/regression.cxx?
// https://github.com/root-project/root/issues/22755
// sqrt() of a negative argument used to evaluate to sqrt(abs(x)) instead of NaN
TEST(TTreeFormulaRegressions, SqrtOfNegative)
{
TTree t("t", "t");
int x = 0;
t.Branch("x", &x, "x/I");
t.Fill();
t.GetEntry(0);
TTreeFormula tf("tf", "sqrt(-4.0)", &t);
EXPECT_TRUE(std::isnan(tf.EvalInstance()));
TTreeFormula tf2("tf2", "sqrt(x - 4)", &t);
EXPECT_TRUE(std::isnan(tf2.EvalInstance()));
TTreeFormula tf3("tf3", "sqrt(x + 9)", &t);
EXPECT_FLOAT_EQ(tf3.EvalInstance(), 3.);
}Also, this is a behavioral change that needs to be explained in the release notes, with an entry like this for example:
#### Behavior change: `sqrt()` of negative arguments in TTreeFormula now returns NaN
Since its introduction in 1995, the formula engine used by `TTree::Draw()`, `TTree::Scan()` and `TTreeFormula`
silently evaluated `sqrt(x)` as `sqrt(abs(x))` for negative arguments (or as `0` in the optimized evaluation path
of the legacy `ROOT::v5::TFormula`). This could produce silently wrong results, e.g. in selections involving
`sqrt` of an expression that can become negative. `sqrt()` now returns NaN for negative arguments, consistent
with `TMath::Sqrt()`, the standard C `sqrt()`, and the modern `TFormula` used by `TF1`.
Note that in a selection, a NaN evaluates as `false`, so entries where the `sqrt` argument is negative now fail
the cut instead of being selected based on `sqrt(abs(x))`.Full authorship of guitargeek
fully written by guitargeek
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.
return nan instead of sqrt(abs(value))
Fixes #22755
Was like that since the early beginnigns https://github.com/root-project/root/blame/dd588ebc2c01182af573ea9ac7ce2ddcbc734b6e/treeplayer/src/TTreeFormula.cxx