关于折纸问题

#include <stdio.h>
#include <stdlib.h>
double paperFold(double thickness,int time){
    int i;
    double res=thickness;
    for(i=0;i<time;i++){
        res*=2;
    }
    return res;
}

int main()
{
    double thickness = 7e-8;
    printf("%f\n",paperFold(thickness,51));
    return 0;
}

double的精确度确实好很多,使用float得出的都是约数

C++

#include <iostream>

using namespace std;

class PageFold{
   private:
        long double thickness;
        int times;
   public:
        PageFold(double thickness,int times){
                thickness = thickness;
                times = times;
        }

        long double calculate(){
            long double result = thickness;
            for(int i=0;i<times;i++){
                  result*=2.0;//±ØÐëÓÃ*=
            }
            return result;
        }
};
int main()
{
    long double thickness = 0.000000007;
    int times = 51;
    PageFold example(thickness,times);
    cout <<example.calculate()<< endl;
    return 0;
}


你可能感兴趣的:(C语言)