Skip to content

TTreeFormula: sqrt() evaluates sqrt(abs(x)) for negative arguments #22755

Description

@grahamwwilson

ROOT version:
6.40.02 and 6.32.12 (Initially found on 6.32.12 - verified to still be an issue with 6.40.02)

Platform:
Ubuntu 24.04

The built-in sqrt() function used by TTree::Scan (and presumably TTreeFormula)
appears to evaluate sqrt(abs(x)) rather than sqrt(x) for negative arguments.

Minimal reproducer:

void test_sqrt(){

// Reproduce sqrt of a negative number bug/feature when using TTree Scan
// Instead of producing a NaN, sqrt(x) as evaluated by TTreeFormula gives sqrt(|-x|)
// when x is negative. TMath:Sqrt(x) gives the correct behavior.

TTree t("t","t");
int dummy = 0;
t.Branch("dummy", &dummy, "dummy/I");
t.Fill();

cout << "buggy sqrt of negative number test" << endl;
t.Scan("sqrt(-4.0):TMath::Sqrt(-4.0)");

}

Output:
$ root -l
root [0] .x test_sqrt.C
buggy sqrt of negative number test


  • Row * sqrt(-4.0 * TMath::Sq *

  •    0 *         2 *      -nan *
    

Expected output:
$ root -l
root [0] .x test_sqrt.C
buggy sqrt of negative number test


  • Row * sqrt(-4.0 * TMath::Sq *

  •    0 *       -nan *      -nan *
    

The same behaviour is observed for arbitrary negative expressions, e.g.

sqrt(-(4))
sqrt(0-4)
sqrt(-1*4)

all evaluate to 2.

The problem also appears when evaluating expressions involving tree variables using sqrt, e.g.

sqrt(Spara1**2-13.69)

returns sqrt(abs(Spara1**2-13.69)) whenever the argument is negative.

TMath::Sqrt() behaves correctly.

thanks,
Graham

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions