-
-
Notifications
You must be signed in to change notification settings - Fork 673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
STYLE: Use std::abs
, instead of doing if (x < 0) x = -x
manually
#4943
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
#include "vnl/algo/vnl_determinant.h" | ||
|
||
#include <algorithm> // For copy_n. | ||
#include <cmath> // For abs. | ||
|
||
namespace itk | ||
{ | ||
|
@@ -237,12 +238,7 @@ TriangleCell<TCellInterface>::DistanceToLine(PointType x, | |
denom += static_cast<double>(v21[i] * v21[i]); | ||
} | ||
|
||
// trying to avoid an expensive fabs | ||
double tolerance = 1.e-05 * num; | ||
if (tolerance < 0.0) | ||
{ | ||
tolerance = -tolerance; | ||
} | ||
double tolerance = std::abs(1.e-05 * num); | ||
Comment on lines
-240
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment "trying to avoid an expensive fabs" appears to be introduced by commit cc69c19 "ENH: Added EvaluatePosition()...", Julien Jomier (@jjomier), Nov 16, 2004. double Call_fabs(double x)
{
return fabs(x);
}
double Call_std_abs(double x)
{
return std::abs(x);
}
double ManuallyEstimateAbs(double x)
{
if (x < 0)
{
return -x;
}
return x;
} Output: Call_fabs(double):
andpd xmm0, XMMWORD PTR .LC0[rip]
ret
Call_std_abs(double):
andpd xmm0, XMMWORD PTR .LC0[rip]
ret
ManuallyEstimateAbs(double):
pxor xmm2, xmm2
movapd xmm3, xmm0
movapd xmm1, xmm0
xorpd xmm1, XMMWORD PTR .LC2[rip]
cmpltsd xmm0, xmm2
andpd xmm1, xmm0
andnpd xmm0, xmm3
orpd xmm0, xmm1
ret
.LC0:
.long -1
.long 2147483647
.long 0
.long 0
.LC2:
.long 0
.long -2147483648
.long 0
.long 0 |
||
if ((-tolerance < denom) && (denom < tolerance)) // numerically bad! | ||
{ | ||
closestPoint = p1; // arbitrary, point is (numerically) far away | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
#include "itkNeighborhoodAlgorithm.h" | ||
#include "itkProgressReporter.h" | ||
|
||
#include <cmath> // For abs. | ||
|
||
namespace itk | ||
{ | ||
namespace Testing | ||
|
@@ -165,12 +167,8 @@ ComparisonImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData(const Out | |
InputPixelType t = valid.Get(); | ||
|
||
// Assume a good match - so test center pixel first, for speed | ||
RealType difference = static_cast<RealType>(t) - test.GetCenterPixel(); | ||
if (NumericTraits<RealType>::IsNegative(difference)) | ||
{ | ||
difference = -difference; | ||
} | ||
auto minimumDifference = static_cast<OutputPixelType>(difference); | ||
RealType difference = std::abs(static_cast<RealType>(t) - test.GetCenterPixel()); | ||
auto minimumDifference = static_cast<OutputPixelType>(difference); | ||
Comment on lines
-168
to
+171
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @N-Dekker great! |
||
|
||
// If center pixel isn't good enough, then test the neighborhood | ||
if (minimumDifference > m_DifferenceThreshold) | ||
|
@@ -182,12 +180,8 @@ ComparisonImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData(const Out | |
{ | ||
// Use the RealType for the difference to make sure we get the | ||
// sign. | ||
RealType differenceReal = static_cast<RealType>(t) - test.GetPixel(i); | ||
if (NumericTraits<RealType>::IsNegative(differenceReal)) | ||
{ | ||
differenceReal = -differenceReal; | ||
} | ||
auto d = static_cast<OutputPixelType>(differenceReal); | ||
RealType differenceReal = std::abs(static_cast<RealType>(t) - test.GetPixel(i)); | ||
auto d = static_cast<OutputPixelType>(differenceReal); | ||
if (d < minimumDifference) | ||
{ | ||
minimumDifference = d; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@N-Dekker Are you sure that this has the same behavior, in particular with
-0
and0
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for bringing 0 versus -0 to the attention, Matt. In this particular case,
FloatDifferenceULP
returns an integer type. For integer types 0 and -0 are bit-wise equal, in C++. So exactly the same behavior, in this particular case.