Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abs() and an alternative simple if statement does not seem to be working

Tags:

c++

I want to check if the length of 2 strings differ by more than 2. This is what I wrote.

#include <cmath>
#include <string>
if (abs(str1.size() - str2.size()) >= 2)
  return false;

Compiler error:

1-5.cpp:8:9: error: call to 'abs' is ambiguous
    if (abs(str1.size() - str2.size()) >= 2)
        ^~~
/Applications/Xcode.app/Contents/Develop
        /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdlib.h:129:6: note: 
          candidate function
    int      abs(int) __pure2;
             ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstdlib:167:44: note: 
          candidate function
    inline _LIBCPP_INLINE_VISIBILITY long      abs(     long __x) _NOEXCEPT {return  labs(__x);}
                                               ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstdlib:169:44: note: 
          candidate function
    inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) _NOEXCEPT {return llabs(__x);}
                                               ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:664:1: note: 
          candidate function
    abs(float __lcpp_x) _NOEXCEPT {return fabsf(__lcpp_x);}
    ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:668:1: note: 
          candidate function
    abs(double __lcpp_x) _NOEXCEPT {return fabs(__lcpp_x);}
    ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:672:1: note: 
          candidate function
    abs(long double __lcpp_x) _NOEXCEPT {return fabsl(__lcpp_x);}
    ^
    1 error generated.

Unable to solve the problem, I changed the if statement to,

if (str1.size() - str2.size() >= 2 || str1.size() - str2.size() <= -2)
  return false;

Even this statement does not seem to be working correctly. No matter what strings I input, the if statement always returns false.

like image 605
pranavhgupta Avatar asked Jan 06 '23 14:01

pranavhgupta


1 Answers

The ambiguous call1 is not the real problem here: std::string::size returns a std::size_t, which is an unsigned integer types, so:

str1.size() - str2.size()

Will always be positive or zero (a negative value would be "wrapped" around by arithmetic modulo 2n, so you will get an enormous number instead of a negative one).

You need to change the way you compare the sizes:

if (str1.size() > str2.size() + 2 || str2.size() > str1.size() + 2) {
    // Do whatever you want
}

Note: With the code you gave, clang warns you about this (in a sense):

warning: taking the absolute value of unsigned type 'unsigned long' has no effect [-Wabsolute-value]

1 There is an ambiguous call here for abs because std::size_t is unsigned and there is no abs for unsigned integer types (why would you want the absolute value of something that is always positive?), so you need a conversion from this unsigned integer type to either int, long or long long which makes the call ambiguous.

like image 133
Holt Avatar answered Feb 01 '23 21:02

Holt