运行时错误-UndefinedBehaviourSanitizer

Runtime error -UndefinedBehaviourSanitizer

  bool isPalindrome(int x) {
    
    if(x < 0){
        return false;
    }
    
    double log = log10(x);

  //below line is causing problem
  //I've tried this too  int totaldigits = floor(log) + 1;
 int totaldigits = floor( log +1 );
    
    int mask = pow(10,totaldigits-1);
    
    
    for(int i =0; i<(totaldigits / 2); i++){
        int atstart = x / mask;
        int atend = x % 10;
        
        if(atstart != atend){
            return false;
        }
       x %= mask;
        x /= 10;
        mask /= 100;
    }
    return true;
    
    
    
}

我在初始化 totaldigits 的地方遇到了一个奇怪的 Error.ON 行。 不太懂,有空帮我解决一下;

第 10 行:字符 24:运行时错误:-inf 超出类型 'int' (solution.cpp) 的可表示值范围 摘要:UndefinedBehaviorSanitizer:未定义行为 prog_joined.cpp:19:24

if(x < 0) 应该是 if(x <= 0).

C++ 计算 log10(0) 为 -infinity,因此

runtime error: -inf is outside the range of representable values of type 'int'

写一个输入为0的特例,例如:

if(x <= 0) {
    return !x;
}