【问题标题】:How am I suppose to use mpfr::mpfr_fac_ui function?我应该如何使用 mpfr::mpfr_fac_ui 函数?
【发布时间】:2019-05-26 08:50:18
【问题描述】:

我尝试在互联网上找到一个如何使用mpfr::mpfr_fac_ui 的示例,但我无法做到,所以我决定在这里提问。

我有自己的迭代阶乘

boost::multiprecision::mpfr_float factorial(int start, int end)
{
    boost::multiprecision::mpfr_float fact = 1;

    for (; start <= end; ++start)
        fact *= start;

    return fact;
}

但我想试试内置的factorial

我不知道我做错了什么,因为当我像这样测试它时

mpfr_t test;
mpfr_init2(test, 1000);

std::cout << mpfr_fac_ui(test, 5, MPFR_RNDN) << std::endl;
std::cout << factorial(1, 5) << std::endl;

mpfr_fac_ui 不返回任何错误(返回 0),test 是 0 而应该是 120。

是我做错了什么还是我遗漏了什么?

【问题讨论】:

    标签: c++ mpfr boost-multiprecision


    【解决方案1】:

    在 C 语言中,我得到 120 的预期值:

    #include <stdio.h>
    #include <mpfr.h>
    
    int main (void)
    {
      mpfr_t test;
      mpfr_init2 (test, 1000);
      mpfr_fac_ui (test, 5, MPFR_RNDN);
      mpfr_printf ("%Rg\n", test);
      mpfr_clear (test);
      return 0;
    }
    

    在您的程序中,您没有显示如何打印test 的值。你所做的就是打印mpfr_fac_ui的返回值,即0。

    【讨论】:

    • 嗯,奇怪,现在我按照你的方式打印它,它也向我显示 120,之前我正在检查 mpfr_t 的内部,但我想我看错了。
    猜你喜欢
    • 2020-06-03
    • 2017-10-23
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2011-09-06
    相关资源
    最近更新 更多