[ROOT-5290] Inline version of TMath::Nint Created: 17/Jun/13  Updated: 15/May/19  Resolved: 04/Oct/13

Status: Closed
Project: ROOT
Component/s: Math Libraries
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Medium
Reporter: Sandro Christian Wenzel Assignee: Lorenzo Moneta
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

PlannedEnd:
PlannedStart:
Development:
Actual End:

 Description   

A benchmark of the digitization algorithm in AliRoot for the ALICE experiment showed a considerable number of function calls to TMath::Nint(...) in inner loops.

It would be beneficial to have an inlined implementation of this function directly in the TMath.h header. Further, I suggest to provide this as a template function (see below) to avoid code duplication between Float_t and Double_t.
Another improvement could be the inversion of floating point operation and the test for evenness ( i & 1 && x + 0.5 == T )

// possible inline implementation of the Nint function
template<typename T>
inline Int_t TMath::Nint(T x)
{
// Round to nearest integer. Rounds half integers to the nearest
// even integer.
int i;
if (x >= 0)

{ i = int(x + 0.5); if ( i & 1 && x + 0.5 == T(i) ) i--; }

else

{ i = int(x - 0.5); if ( i & 1 && x - 0.5 == T(i) ) i++; }

return i;
}



 Comments   
Comment by Lorenzo Moneta [ 04/Oct/13 ]

Fixed now in 5.34 and master

Generated at Fri Nov 15 03:01:10 CET 2019 using Jira 8.3.4#803005-sha1:1f96e09b3c60279a408a2ae47be3c745f571388b.