运行时错误-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;
    
    
    
}

我收到一个奇怪的错误。在初始化总位数的行上。 如果您有空闲时间,请帮我解决这个问题;

第10行:Char 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;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...