Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#5515 closed defect (fixed)

Fix ambiguous uses of abs [-Wabsolute-value]

Reported by: historic_bruno Owned by: historic_bruno
Priority: Should Have Milestone: Alpha 24
Component: Core engine Keywords:
Cc: Patch: Phab:D2091

Description

There are a handful of ambiguous abs calls in the source:

../../../source/lib/tex/tex_bmp.cpp:106:17: warning: absolute value function 'abs' given an argument of type 'const long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
        const long h = abs(h_);
                       ^
../../../source/lib/tex/tex_bmp.cpp:106:17: note: use function 'std::abs' instead
        const long h = abs(h_);
                       ^~~
                       std::abs

../../../source/lib/tex/tex_bmp.cpp:106:17: warning: absolute value function 'abs' given an argument of type 'const long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
        const long h = abs(h_);
                       ^
../../../source/lib/tex/tex_bmp.cpp:106:17: note: use function 'std::abs' instead
        const long h = abs(h_);
                       ^~~
                       std::abs

In file included from ../../../source/simulation2/components/tests/test_HierPathfinder.cpp:17:
/Users/user/GitHub/0ad/source/simulation2/components/tests/test_HierPathfinder.h:440:13: warning: using integer absolute value function 'abs' when argument is of floating point type [-Wabsolute-value]
                TS_ASSERT(abs(euclidian(goal.x.ToInt_RoundToNegInfinity(), goal.z.ToInt_RoundToNegInfinity(), pi, pj)-20) < 1.5f);
                          ^
/Users/user/GitHub/0ad/source/simulation2/components/tests/test_HierPathfinder.h:440:13: note: use function 'std::abs' instead
                TS_ASSERT(abs(euclidian(goal.x.ToInt_RoundToNegInfinity(), goal.z.ToInt_RoundToNegInfinity(), pi, pj)-20) < 1.5f);
                          ^~~
                          std::abs

In file included from ../../../source/simulation2/components/tests/test_HierPathfinder.cpp:17:
/Users/user/GitHub/0ad/source/simulation2/components/tests/test_HierPathfinder.h:441:13: warning: using integer absolute value function 'abs' when argument is of floating point type [-Wabsolute-value]
                TS_ASSERT(abs(euclidian(goal.x.ToInt_RoundToNegInfinity(), goal.z.ToInt_RoundToNegInfinity(), oi, oj) - euclidian(pi, pj, oi, oj)) < 22.0f);
                          ^
/Users/user/GitHub/0ad/source/simulation2/components/tests/test_HierPathfinder.h:441:13: note: use function 'std::abs' instead
                TS_ASSERT(abs(euclidian(goal.x.ToInt_RoundToNegInfinity(), goal.z.ToInt_RoundToNegInfinity(), oi, oj) - euclidian(pi, pj, oi, oj)) < 22.0f);
                          ^~~
                          std::abs

The problem is the compiler might select either the C or C++ version of abs. The C version is int-only, while the C++ version has overloads for various types. Building on OS X 10.11 actually warns about these (10.14 doesn't), but I believe the calls aren't well-defined anyway and should be fixed.

Change History (3)

comment:1 by historic_bruno, 5 years ago

Patch: Phab:D2091

comment:2 by historic_bruno, 5 years ago

Resolution: fixed
Status: assignedclosed

In 22500:

Fixes some ambiguous calls of abs().

Fixes #5515.
Reviewed By: elexis
Differential Revision: https://code.wildfiregames.com/D2091

comment:3 by elexis, 5 years ago

Milestone: BacklogAlpha 24
Note: See TracTickets for help on using tickets.