浮点数(double)的优势

用 double 处理大数据 , 即使字符串位数超百位,同样可以用 sscanf 或 atof 来处理,却不会变成负数,原因是浮点数可以用指数表示,但精度会丢失,下面介绍利用这个性质解决的一些问题:

  如何判断一个字符串表示的数值是否超出整型范围;

  int 取值范围:最大2147483647

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<string.h>

using namespace std ;

int main()      {
        char s1[1000] , s2 , s3[1000] ;
        while(scanf("%s %c %s" , s1 , &s2 , s3 )!=EOF)  {
                printf("%s %c %s
" , s1 , s2 , s3) ;
                double a , b ;
                a = atof(s1) ;
                b = atof(s3) ;
                if(a > 2147483647)
                        printf("first number too big
");
                if(b > 2147483647)
                        printf("second number too big
") ;
                if(s2 == '+' && a+b > 2147483647)
                        printf("result too big
") ;
                else if(s2 == '*' && a * b > 2147483647)
                         printf("result too big
") ;
        }
        return 0 ;
}

 在对精度要求不高的数据处理中,可以用 double 达到 大数据处理问题,浮点数可以用指数来表示,这点很重要。

原文地址:https://www.cnblogs.com/NYNU-ACM/p/4237443.html