zoukankan      html  css  js  c++  java
  • 浮点数(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 达到 大数据处理问题,浮点数可以用指数来表示,这点很重要。

  • 相关阅读:
    uva111 History Grading
    UVA 10100Longest Match
    UVA 147Dollars
    归并排序模板
    找礼物(find)
    水流(water)dfs
    细菌(disease) 位运算
    单词接龙
    关于jquery的each遍历,return只终止当前循环,不好使的解决办法
    jquery中ajax回调函数使用this
  • 原文地址:https://www.cnblogs.com/scottding/p/3661962.html
Copyright © 2011-2022 走看看