floating-point - 是否可以使用 gmp 打印大量 float ?

标签 floating-point precision gmp

我要打印

1589128402970546000515214676475308953824987995059891920501615931630880

numbers of decimal expansion of ratio:

33877456965431938318210482471113262183356704085033125021829876006886584214655562/2371421987580235682274733772977928352834 96928595231875152809132048206089502588927

我尝试使用 gmp 库来做到这一点:

// gcc p.c -lgmp

#include <stdio.h>
#include <gmp.h>

int main (int argc, char **argv)
{       



     mpf_set_default_prec(1024); // set default precision in bits


    // declare   
    mpz_t zn;
    mpz_t zd;
    mpq_t q;
    mpf_t fn;
    mpf_t fd; 
    mpf_t f; // f=float(q=n/d)

    // init
    mpz_init (zn);
    mpz_init (zd);
    mpq_init (q);
    mpf_init2 (fn, 794564201485273000257607338237654476912493997529945960250807965815440 ); 
    mpf_init (fd); 
    mpf_init (f); 

    // set
    mpz_set_str(zn, "33877456965431938318210482471113262183356704085033125021829876006886584214655562", 10 ); // 
    // mpz_set_ui(n,33877456965431938318210482471113262183356704085033125021829876006886584214655562);   warning: integer constant is too large for its type [enabled by default]
    mpz_set_str(zd, "237142198758023568227473377297792835283496928595231875152809132048206089502588927", 10);
    mpq_set_str(q, "33877456965431938318210482471113262183356704085033125021829876006886584214655562/237142198758023568227473377297792835283496928595231875152809132048206089502588927", 10);

    mpf_set_z(fn, zn);
    mpf_set_z(fd, zd);



    //
    mpf_div(f, fn, fd);

    // print result 
    gmp_printf ("  decimal floating point number : %.Ff \n", f); //

    // clear  
    mpz_clear (zn);
    mpz_clear (zd);
    mpq_clear(q);
    mpf_clear (fn);
    mpf_clear (fd);
    mpf_clear (f);

    return 0;

}

但结果只有 22 位十进制数字:

十进制 float :0.142857142857142857143

我该怎么做?

最佳答案

您需要提高精度(即尾数位)以获得更多小数位。正如 GMP documentation 所说(强调我的):

The mantissa of each float has a user-selectable precision, limited only by available memory. Each variable has its own precision, and that can be increased or decreased at any time.

默认情况下,它选择 64 位作为尾数,基本上您得到的是:

dig10 = 地板(dig2/log2(10)),

因此,开头只有大约 19 位有效的十进制数字。通过要求更高的精度,您只会得到更多的精度:

#include <stdio.h>
#include <gmp.h>

int main(void)
{
    mpz_t zn, zd;
    mpf_t fn, fd, f;

    mpf_set_default_prec(1024);

    mpz_init_set_str(zn, "33877456965431938318210482471113262183356704085033125021829876006886584214655562", 10);
    mpz_init_set_str(zd, "237142198758023568227473377297792835283496928595231875152809132048206089502588927", 10);

    mpf_init(fn); mpf_init(fd); mpf_init(f);

    mpf_set_z(fn, zn);
    mpf_set_z(fd, zd);

    mpf_div(f, fn, fd);

    gmp_printf ("decimal floating point number: %.Ff\n", f);

    return 0;
}

结果:

decimal floating point number: 0.1428571428571428571428571428571428571428571428571428571428571428571428571428571470740220344350664325965724776508647459411274100902601628882480671861148592450467118250772869175909603098316903937670228712738977340379914830920623570643668473851880233962854802649343258544237378422095661611829666642751471587447213

如果您想要一些固定数量的十进制数字(并假设 zn/zd 商为零),只需使用上面(或一些更复杂的)对数方程即可。

关于floating-point - 是否可以使用 gmp 打印大量 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26429129/

相关文章:

math - 整数乘法是否使用 double 浮点实现精确到 2^53?

typescript - 如何在 typescript 中通过 websocket 接收 float 据

c++ - 如何在基数 256 中以其唯一的 xLen 数字表示形式写入整数 x?

performance - 定点算术值得我费心吗?

assembly - FMUL 不会清除 STATUS 寄存器中的溢出

json - 将数字从 float64 转换为 Int64 不正确

floating-point - 为什么 float 不正确?

c - 大浮点和的精度

c++ - GMP 和智能指针

c - GMP 将 char 数组转换为 float