GMP mpf 精度到有效位数的硬性规则是什么

问题描述 投票:0回答:1

GMP 目前开箱即用,精度为 64 位精度(我认为这就是 mp_bitcnt_t 的名称)。

对我来说,我认为这最多可以达到大约 1x10e-22 的值(我使用数字 1.123...所以小数点左边有 1 个有效数字,右边有 22 个有效数字)。如果我尝试在此之后添加更多数字,例如 1x10e-23,那么事情就会开始偏离正轨。因此,我当然可以将精度位提高到任意值,例如 128,这样就可以了。

但我想知道是否有一种不那么武断的方法来找出一个更精确的值放置在那里,而不是仅仅将其加倍到 128,因为我不知道还可以放在那里。

我想我基本上是在问 mp_bitcnt_t 与有效十进制数字的关系是什么?

precision gmp
1个回答
0
投票

通过实验,GMP 似乎以 64 位精度增量工作。前 64 位提供大约 22 位小数位的精度。有趣的是,最初无法获得低于 64 位的精度。 (我希望能够用它来减少 CPU 时间)。

GMP 以 64 位增量向上跳跃。因此,即使您设置 58,它仍然会设置为 64。

因此,每个 64 精度位块将为您提供大约 18-20 位十进制数字。

我用来确定以下内容的代码(在 C (Swift) 和 awk 中):

void check_digits_to_gmp_prec(void)
{
    //pi to 2,000 digits
    char *pi = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009946576407895126946839835259570";
    
    for (int i = 32; i < 4096; i++)
    {
        mpf_t val;
        mpf_set_default_prec(i);
        mpf_init(val);
        mpf_set_str(val, pi, 10);
        gmp_printf("bits: %ld precision: %ld pi-digits: %.Ff\n", i, mpf_get_default_prec(), val);
        mpf_clear(val);
    }
    /*
     % cat outp | awk '{if (length(PREV) != length($6)) {print $1,$2,":",$2-PREV_F2,"-",$3,$4,":",$4-PREV_F4,"-",$5, length($6),":",length($6)-PREV_LEN; PREV_F2 = $2; PREV_F4 = $4};PREV_LEN=length($6);PREV = $6;}'
     bits: 32 : 32 - precision: 64 : 64 - pi-digits: 22 : 22
     bits: 65 : 33 - precision: 128 : 64 - pi-digits: 41 : 19
     bits: 129 : 64 - precision: 192 : 64 - pi-digits: 60 : 19
     bits: 193 : 64 - precision: 256 : 64 - pi-digits: 80 : 20
     bits: 257 : 64 - precision: 320 : 64 - pi-digits: 99 : 19
     bits: 321 : 64 - precision: 384 : 64 - pi-digits: 118 : 19
     bits: 385 : 64 - precision: 448 : 64 - pi-digits: 137 : 19
     bits: 449 : 64 - precision: 512 : 64 - pi-digits: 157 : 20
     bits: 513 : 64 - precision: 576 : 64 - pi-digits: 176 : 19
     bits: 577 : 64 - precision: 640 : 64 - pi-digits: 195 : 19
     bits: 641 : 64 - precision: 704 : 64 - pi-digits: 214 : 19
     bits: 705 : 64 - precision: 768 : 64 - pi-digits: 234 : 20
     bits: 769 : 64 - precision: 832 : 64 - pi-digits: 253 : 19
     bits: 833 : 64 - precision: 896 : 64 - pi-digits: 271 : 18
     bits: 897 : 64 - precision: 960 : 64 - pi-digits: 291 : 20
     bits: 961 : 64 - precision: 1024 : 64 - pi-digits: 311 : 20
     bits: 1025 : 64 - precision: 1088 : 64 - pi-digits: 330 : 19
     bits: 1089 : 64 - precision: 1152 : 64 - pi-digits: 349 : 19
     bits: 1153 : 64 - precision: 1216 : 64 - pi-digits: 369 : 20
     bits: 1217 : 64 - precision: 1280 : 64 - pi-digits: 388 : 19
     bits: 1281 : 64 - precision: 1344 : 64 - pi-digits: 407 : 19
     bits: 1345 : 64 - precision: 1408 : 64 - pi-digits: 426 : 19
     bits: 1409 : 64 - precision: 1472 : 64 - pi-digits: 446 : 20
     bits: 1473 : 64 - precision: 1536 : 64 - pi-digits: 465 : 19
     bits: 1537 : 64 - precision: 1600 : 64 - pi-digits: 484 : 19
     bits: 1601 : 64 - precision: 1664 : 64 - pi-digits: 502 : 18
     bits: 1665 : 64 - precision: 1728 : 64 - pi-digits: 523 : 21
     bits: 1729 : 64 - precision: 1792 : 64 - pi-digits: 542 : 19
     bits: 1793 : 64 - precision: 1856 : 64 - pi-digits: 561 : 19
     bits: 1857 : 64 - precision: 1920 : 64 - pi-digits: 580 : 19
     bits: 1921 : 64 - precision: 1984 : 64 - pi-digits: 600 : 20
     bits: 1985 : 64 - precision: 2048 : 64 - pi-digits: 619 : 19
     bits: 2049 : 64 - precision: 2112 : 64 - pi-digits: 637 : 18
     bits: 2113 : 64 - precision: 2176 : 64 - pi-digits: 658 : 21
     bits: 2177 : 64 - precision: 2240 : 64 - pi-digits: 676 : 18
     bits: 2241 : 64 - precision: 2304 : 64 - pi-digits: 696 : 20
     bits: 2305 : 64 - precision: 2368 : 64 - pi-digits: 715 : 19
     bits: 2369 : 64 - precision: 2432 : 64 - pi-digits: 735 : 20
     bits: 2433 : 64 - precision: 2496 : 64 - pi-digits: 754 : 19
     bits: 2497 : 64 - precision: 2560 : 64 - pi-digits: 773 : 19
     bits: 2561 : 64 - precision: 2624 : 64 - pi-digits: 792 : 19
     bits: 2625 : 64 - precision: 2688 : 64 - pi-digits: 812 : 20
     bits: 2689 : 64 - precision: 2752 : 64 - pi-digits: 831 : 19
     bits: 2753 : 64 - precision: 2816 : 64 - pi-digits: 850 : 19
     bits: 2817 : 64 - precision: 2880 : 64 - pi-digits: 869 : 19
     bits: 2881 : 64 - precision: 2944 : 64 - pi-digits: 889 : 20
     bits: 2945 : 64 - precision: 3008 : 64 - pi-digits: 908 : 19
     bits: 3009 : 64 - precision: 3072 : 64 - pi-digits: 927 : 19
     bits: 3073 : 64 - precision: 3136 : 64 - pi-digits: 947 : 20
     bits: 3137 : 64 - precision: 3200 : 64 - pi-digits: 966 : 19
     bits: 3201 : 64 - precision: 3264 : 64 - pi-digits: 985 : 19
     bits: 3265 : 64 - precision: 3328 : 64 - pi-digits: 1004 : 19
     bits: 3329 : 64 - precision: 3392 : 64 - pi-digits: 1024 : 20
     bits: 3393 : 64 - precision: 3456 : 64 - pi-digits: 1043 : 19
     bits: 3457 : 64 - precision: 3520 : 64 - pi-digits: 1061 : 18
     bits: 3521 : 64 - precision: 3584 : 64 - pi-digits: 1081 : 20
     bits: 3585 : 64 - precision: 3648 : 64 - pi-digits: 1101 : 20
     bits: 3649 : 64 - precision: 3712 : 64 - pi-digits: 1119 : 18
     bits: 3713 : 64 - precision: 3776 : 64 - pi-digits: 1139 : 20
     bits: 3777 : 64 - precision: 3840 : 64 - pi-digits: 1158 : 19
     bits: 3841 : 64 - precision: 3904 : 64 - pi-digits: 1178 : 20
     bits: 3905 : 64 - precision: 3968 : 64 - pi-digits: 1197 : 19
     bits: 3969 : 64 - precision: 4032 : 64 - pi-digits: 1216 : 19
     bits: 4033 : 64 - precision: 4096 : 64 - pi-digits: 1236 : 20
     %

     */
}
© www.soinside.com 2019 - 2024. All rights reserved.