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 达到 大数据处理问题,浮点数可以用指数来表示,这点很重要。

  • 相关阅读:
    OC NSSet
    iOS 杂笔-23(区分各种空值)
    OC NSNumber NSValue
    OC NSArray 数组
    OC NSString(字符串)
    OC 内存管理
    OC 动态类型,动态绑定,动态加载
    OC 多态
    微信小程序开发5-WXML
    微信小程序开发4-JSON
  • 原文地址:https://www.cnblogs.com/scottding/p/3661962.html
Copyright © 2011-2022 走看看