Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;?_C++_C++11_Digit - Fatal编程技术网

C++ 如何在C++;?

C++ 如何在C++;?,c++,c++11,digit,C++,C++11,Digit,我曾尝试从双精度值中获取数字,但无法正常工作。我试过这个: int main() { double value = 123456.05; std::cout<<"number of digit::"<<((int)std::log10(value ) + 1); } intmain() { 双值=123456.05; std::cout您可以将双精度值转换为字符串: double value = 123456.05; std::string s = st

我曾尝试从双精度值中获取数字,但无法正常工作。我试过这个:

int main()
{
    double value = 123456.05;
    std::cout<<"number of digit::"<<((int)std::log10(value ) + 1);
}
intmain()
{
双值=123456.05;

std::cout您可以将双精度值转换为字符串:

double value = 123456.05;
std::string s = std::to_string(value);
之后,您需要删除尾随的zeroez(因为现在有可能
s==“123456.050000”

然后获取此字符串的多个字符:

std::cout<<"number of digit::"<< s.length();

std::cout假设您只需要将其用于双文本,那么下面的方法就可以了

编辑:添加了一个适用于double子集的等效函数。它使用穷举搜索所有合理的方法以十进制显示double,如果您需要此函数,可能有一些方法可以使其更有效

    #include <iostream>
    #include <string.h>
    #include <assert.h>
    #include <cmath>

    struct double_literal {
            const char* string_value;
            double double_value;
            size_t num_digits;
    };

    #define DOUBLE_LITERAL(x) {#x, x, strlen(#x)};

size_t num_digits(double value){
        //Double gives around 15 accurate digits usually.
        //Disregarding exponential notation, there are hence only 15 reasonable
        //ways to display value, ranging from 15 places in front of the decimal
        //to 15 behind. Figure out which of these is best in terms of error,
        //and then at the end print out how many digits are needed to display
        //the number by removing unecessary zeros.

        //Routine currently only handles values within these bounds
        //If your value is outside, can scale it with a power of ten before
        //using. Special cases for zero and negative values can also be added.

        double window_stop = std::pow(10.0, 15);
        double window_start = 1 + std::pow(10.0, -15);
        assert(value < window_stop);
        assert(value > window_start);

        size_t best_window = 0;
        double best_error = INFINITY;

        double pow_ten_window = 1;
        for(size_t window = 0; window <= 15; window++, pow_ten_window *= 10){
               double rounded = fmod(
                    std::round(value * pow_ten_window),
                    window_stop
               ) / pow_ten_window;
               double error = std::abs(rounded - value);
               if (error < best_error){
                    best_error = error;
                    best_window = window;
               }
        }

        unsigned long long best_rounding = std::llround(
                fmod(
                        value * std::pow(10.0, best_window),
                        window_stop
                )
        );

        size_t best_digits = std::llround(std::log10(best_rounding) + 1);

        //Representation has an integer part => just figure out if we
        //need a decimal point
        if (best_window > 0){
                best_digits++;
        }

        std::cout << best_window << std::endl;

        return best_digits;

}

int main(int argc, char** argv){
        struct double_literal literal DOUBLE_LITERAL(123456.05);
        std::cout << "number of digit::" << literal.num_digits << std::endl;

        //As a function
        std::cout << "numbr of digit::" << num_digits(literal.double_value);
    }
#包括
#包括
#包括
#包括
结构双_字面值{
常量字符*字符串值;
双u值;
数字大小;
};
#定义双#u字面值(x){#x,x,strlen(#x)};
大小\u t数字\u位数(双倍值){
//Double通常给出大约15个精确数字。
//不考虑指数表示法,因此只有15个合理值
//显示值的方式,从小数点前15位开始
//到后面15。找出其中哪一个在错误方面是最好的,
//然后在最后打印出需要显示的数字
//通过删除不必要的零来删除数字。
//例程当前仅处理这些边界内的值
//如果您的值在外部,则可以在之前以10的幂进行缩放
//使用。还可以添加零值和负值的特殊情况。
双窗口停止=标准::功率(10.0,15);
双窗口启动=1+std::pow(10.0,-15);
断言(值<窗口\停止);
断言(值>窗口开始);
最佳窗口大小=0;
双最佳_误差=无穷大;
双功率十窗口=1;
对于(size_t window=0;window,请确定
//需要小数点吗
如果(最佳窗口>0){
最佳数字++;
}

std::cout问题是,两个相等的双值的位数可能不相同浮点值没有“位数”之类的概念这个任务有缺陷,因为浮点值是.<代码> 1.1 和<代码> 1 + 1 / 10 < /COD>由于舍入错误而不会显示相同的位数。您需要使用基10小数,而当有几种语言时,我不认为C++是这样的。除非DECI,否则我看不出<代码> 123456.05 < /C> >中有9位数字。错误计数为数字。浮点值的存储方式与您可能认为的不同。小数点前的位数非常简单,小数点后的位数不可能按您的方式正确(可能)想要。你想让
1.23
1.2300000
有不同的输出吗?我认为当双精度值为123456.00时,该代码将失败?@CrazyCoder你有办法找到
双精度a=123456.00;
双精度b=123456.0;
之间的差异吗?这些值的内部表示形式将是same.所以程序在两种情况下都返回6。这不是你需要的吗?@llya,我已经把这个值作为一个例子,但是这个值可能是123.45或123.00009或123.00或123.0。任何东西都会出现。所以,我没有在一种情况下找到解决方案。@CrazyCoder Ok,但是这个例子的预期结果是什么?当值123456出现时,它会出现6.00.它在所有情况下都在运行,但有没有办法在函数中写入此代码。@CrazyCoder否,这是不可能的。因为双值
1.0
1.00
的内部表示形式是相同的。您需要捕捉这种差异,这真的很奇怪。如果您需要函数,可以在中找到它
void print_nb_digits(double value) {
  std::string s = std::to_string(value);
  s.erase(s.find_last_not_of('0') + 1, std::string::npos);
  if (!s.empty() && !std::isdigit(s.back()))
    s.pop_back();
  std::cout<<"number of digit::"<< s.length();
}

double value = 123456.05;
print_nb_digits(value);
    #include <iostream>
    #include <string.h>
    #include <assert.h>
    #include <cmath>

    struct double_literal {
            const char* string_value;
            double double_value;
            size_t num_digits;
    };

    #define DOUBLE_LITERAL(x) {#x, x, strlen(#x)};

size_t num_digits(double value){
        //Double gives around 15 accurate digits usually.
        //Disregarding exponential notation, there are hence only 15 reasonable
        //ways to display value, ranging from 15 places in front of the decimal
        //to 15 behind. Figure out which of these is best in terms of error,
        //and then at the end print out how many digits are needed to display
        //the number by removing unecessary zeros.

        //Routine currently only handles values within these bounds
        //If your value is outside, can scale it with a power of ten before
        //using. Special cases for zero and negative values can also be added.

        double window_stop = std::pow(10.0, 15);
        double window_start = 1 + std::pow(10.0, -15);
        assert(value < window_stop);
        assert(value > window_start);

        size_t best_window = 0;
        double best_error = INFINITY;

        double pow_ten_window = 1;
        for(size_t window = 0; window <= 15; window++, pow_ten_window *= 10){
               double rounded = fmod(
                    std::round(value * pow_ten_window),
                    window_stop
               ) / pow_ten_window;
               double error = std::abs(rounded - value);
               if (error < best_error){
                    best_error = error;
                    best_window = window;
               }
        }

        unsigned long long best_rounding = std::llround(
                fmod(
                        value * std::pow(10.0, best_window),
                        window_stop
                )
        );

        size_t best_digits = std::llround(std::log10(best_rounding) + 1);

        //Representation has an integer part => just figure out if we
        //need a decimal point
        if (best_window > 0){
                best_digits++;
        }

        std::cout << best_window << std::endl;

        return best_digits;

}

int main(int argc, char** argv){
        struct double_literal literal DOUBLE_LITERAL(123456.05);
        std::cout << "number of digit::" << literal.num_digits << std::endl;

        //As a function
        std::cout << "numbr of digit::" << num_digits(literal.double_value);
    }