我有一个double
打印为文本的。有没有办法将该源文本编号传递给某些实用程序以获得实际数字,即最接近的实际可表示双精度值?十进制/二进制/十六进制?
例如,文本3.472727272727276
实际上是3.47272727272727621539161191321909427642822265625
更新:
我正在寻找一种方法来获取最接近的浮点数3.472727272727276
,而不是四舍五入到它的任何一个浮点数
答案1
这样可以吗?
dash$ printf "%.99f\n" 3.472727272727276
3.472727272727276215391611913219094276428222656250000000000000000000000000000000000000000000000000000
或者
dash$ printf "%.99f\n" 3.472727272727276 | sed -e 's/0*$//'
3.47272727272727621539161191321909427642822265625
这是 Dash 的输出,它与我使用 Zsh、一个简单的 C 程序(*)或 Perl ( perl -e 'printf "%.99f\n", 3.472727272727276'
) 得到的输出一致。另一方面,Bash 似乎提供了更高的精度,我没有研究那里的 printf 用于浮点数的内容。
(* 在 x86_64 Debian 上使用默认选项编译,因此它使用 SSE 进行浮点运算。)
宽度99
当然是从帽子中拉出来的。双精度数中有 53 个有效位,17 个以 10 为基数的数字应该足以获得明确的表示,但要获得准确的值,对于整数部分约为 1 的数字,您再次需要 53 个以 10 为基数的数字。如果按比例放大或缩小,您将需要更多的数字,可能超过 99,或指数表示法(%g
也许)。
这超出了我的想象,可能会有错误或极端情况。我不打算深入研究浮点数学,这里应该有比我聪明的人写的关于浮点的文章。
答案2
概括
简短的回答精确的数字是(使用破折号表示双浮子):
$ dash -c 'printf "%a\n" 3.472727272727276'
0x1.bc8253c8253dp+1
在 bash 中我们得到扩展双浮子(80 位),因此更多的数字:
$ bash -c 'printf "%a\n" 3.4727272727272760001'
0xd.e4129e4129e7c1fp-2
awk 和 zsh 中没有%a
格式,在 ksh 中转换可能会失败。
如果您必须进行十进制转换,则位数可能高达 16450 位精确的转换。该命令必须是:
$ printf '%.16450g\n' 3.472727272727276
3.47272727272727600006906045759791368254809640347957611083984375
它利用了格式删除所有尾随零的事实g
(但大多数时候它使用科学指数)。
请注意,向下至 1e-4931(向零)是有效的扩展浮点数该数字确实会生成 16445 位f
数字1
该位数接近二进制小数位数:
$ printf '%a\n' 1e-4931
0xb.e5b66ecbce0b7b1p-16384
11516 位数字(最后 6 个字符是指数),g
格式为:
$ printf '%.16445g\n' 1e-4931 | wc -c
11522
详细描述
@ilkkachu 的答案不精确//不完整
是的,我们可以通过要求更多(在某些情况下)来获得更多数字(小数数字,或非整数数字):
$ printf '%.10f\n' 0.5; printf '%.25f\n' 0.5
0.500000
0.5000000000000000000000000
但为什么要停在 25 或 99 或 250 或 2500 呢?极限在哪里?极限应该在哪里?
双倍的
对于 Linux 上的 GNU awk 中实现的 printf,我们最多可以获得 53 位数字 { 对于 0.5=<x<1 范围内的数字或:[0.5,1)。因此尾数的第一位是 1,指数为“0”(零)}。
$ awk 'BEGIN{ printf "%.56f\n", 0.6 }'
0.59999999999999997779553950749686919152736663818359375000
123456789_123456789-123456789¯123456789|123456789_123456789-
这是因为 awk 默认使用的 C 中的普通 double 具有 53 位的尾数(是的,小数十进制数字与小数二进制数字一样多)。
加长浮动
但在 bash(以及其他一些实现)中,双浮点数的大小与数学协处理器内的大小一样大,80 位浮点数的尾数为 64 位。
$ printf "%.70f\n", 0.6
0.6000000000000000000216840434497100886801490560173988342285156250000000
123456789-123456789-123456789-123456789-123456789-123456789-123456789-
但要小心,dash 使用 C 双浮点数,ksh 将小数部分截断为 18 位:
$ dash -c 'printf "%.70f\n" 0.6
0.5999999999999999777955395074968691915273666381835937500000000000000000
123456789-123456789-123456789-123456789-123456789-123456789-123456789-
$ ksh -c 'printf "%.70f\n" 0.12345678901234567890'
0.1234567890123456790000000000000000000000000000000000000000000000000000
123456789-123456789-123456789-123456789-123456789-123456789-123456789-
更长的小数
如果要打印的数字超出范围[0.5,1)
。好吧,实际上,如果数字低于这个范围, printf
(准确地说)生成的位数必须增加,一个小指数-12
足以得到超过 99 位小数:
$ printf '%.99f\n%.110f\n' 1e-12 1e-12
0.00000000000100000000000000000000260071239141640525192192084057152268
1727856252109631896018981933594
0.00000000000100000000000000000000260071239141640525192192084057152268
172785625210963189601898193359375000000000
法线
如果调整尾数为第一个二进制位为1
,遵循 IEEE-754 规范,指数可能与(负大)二进制 1022(或十进制 308 左右)一样大,这将需要精确打印 1022 + 53 = 1075 个十进制数字(使用破折号表示 C 语言 double漂浮):
$ dash -c 'printf "%.1080f" 3e-308'
0.00000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000030000000000000002220455218529919301629802590703175094461
8147776231970806960868915174651286204183765077257126691146784826970001182
3377333217966211682361285986313366061022263946269900386622216365532374597
7178280811925489426908651787672688372272916552149728525251396407136469620
5940554355008967838740787733222562854087120536535403422962140580707820507
1667528534455025639283299437942142025335784795886380202690541431449127657
8638018769522816220159645085115105872872349792947642929427853637000376104
4766449778622710769965927963092807311828491559718885849370616714002362927
2391454493281372654969535173285036362816688696655266375211946090175421780
9466790102733774958189714097426504991570081907927842237039897170724535065
396560619562470184629177083479589782655239105224609375000000
但是使用以下格式会容易得多,并且具有固定宽度%a
:
$ dash -c 'printf "%a" 3e-308'
0x1.59283684dba77p-1022
完全相同的数字,但比上面所需的 1080 位少很多。
次正规
然而,IEEE754 也允许次正常它将小数范围扩展到更小的数字(接近零,而不是负数)。在双浮点数中,可能达到 2 -1074。即 1022(最大指数)+ 52(尾数中的二进制位)
$ dash -c 'printf "%a\n" 0x1p-1074'
0x0.0000000000001p-1022
该数字至少需要 1075 位十进制数字(尝试dash -c 'printf "%.1080f\n" 0x1p-1074'
数学
需要这么多数字的原因只是数学原因。
0x1p-60(小数点分隔符右侧 60 个位置的 1 个二进制位)的确切结果就是 1/2^60 或 2 -60:
$ echo 'scale=65;1/2^60' | bc
.00000000000000000086736173798840354720596224069595336914062500000
数学迫使这个结果。再次强调:十进制小数位数必须与二进制小数位数一样多。
精确的?
但为了给你一个简短的方法来计算数字应该产生的浮点数:尝试:
$ number=3.472727272727276;
$ echo "x=$number"'; f=floor(x*2^53); l=f/2^53; u=(f+1)/2^53;l;x-l;u;u-x' | bc
3.47272727272727599334 # one ulp below
.00000000000000000666
3.47272727272727610436 # one ulp above
.00000000000000010436
并选择误差较小的那个,在本例中为 3.47272727272727599334。这与确切的 IEEE754 表示形式不同。但你可以确定这个数字会给你准确的结果:
$ dash -c 'printf "%.100g\n" "3.47272727272727599334"'
3.472727272727275771302402063156478106975555419921875
ulp 的意思是:最后一位的单位。
IEEE-754
上面的大部分内容都假设浮点表示符合 IEEE-754 规则“舍入到最接近的数字(更接近 0.5 的数字)并与偶数相关(如果到最近的数字的距离上下都相等,则舍入为偶数(简单来说:最后一位0
))。
因此,根据您问题中的数字:
$ arr=(
3.4727272727272765
3.4727272727272764
3.472727272727276
3.472727272727275994
3.472727272727275993
); dash -c 'printf "%a\n" "$@"' _sh "${arr[@]}"
0x1.bc8253c8253d1p+1
0x1.bc8253c8253dp+1
0x1.bc8253c8253dp+1
0x1.bc8253c8253dp+1
0x1.bc8253c8253cfp+1
从上面 1 位的数字(以 结尾3d1
)到下面 1 位的数字(以 结尾3cf
)。在 bash 中,对于 64 位尾数,数字是不同的。
arr=(
3.47272727272727600018
3.47272727272727600017
3.472727272727276
3.47272727272727599997
3.47272727272727599996
); bash -c 'printf "%a\n" "$@"' _sh "${arr[@]}"
0xd.e4129e4129e7c2p-2
0xd.e4129e4129e7c1fp-2
0xd.e4129e4129e7c1fp-2
0xd.e4129e4129e7c1fp-2
0xd.e4129e4129e7c1ep-2
笔记1
请注意,向下(朝向零)到 1e-4931 是有效的扩展浮点数这个数字确实在f
格式上生成了 16445 位数字,稍微向前向下:
位数接近于二进制小数位数:
$ printf '%a\n' 1e-4931
0xb.e5b66ecbce0b7b1p-16384
11516 位数字(最后 6 个字符是指数),g
格式为:
$ printf '%.16445g\n' 1e-4931 | wc -c
11522
printf '%.16450f\n' 1e-4931 | fold -w 73
0.00000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000010000000000000000000082071899211
7338292331024160232487820585348161247770186041314033840500051545008634353
6341360688983242486478141174400757940479009877109594292525504227462920421
2893412961654118443870700320472323231587805993105589329896961394013763260
4659993978247248517682208361484994779731694489944630063321405732960902413
2163826064625704556138246247291713512226186038110522361712282167677484499
4853485029854756356981823466893512864152476213489101964761352653855914067
5407404554688679242643694555765705916896595658042111013963304042194443434
2454458747320616647767965588654942541534222361345871520329192555602083886
7547668572049684141329499463147471364250757050352758439157993233018887765
3022453676305812302609741465722408673611869206684449107816484217162193316
0451417277376429998715700300103481261322468543881150650589114548116214409
0282610286947933274302831898960425132335516929267570658616807675594145055
7933527421286297306761087201890796722576805103111314970478492002310801919
5121110163059112312817278127472248186627786955410431004987964809727320826
3836867323863772424448077582741295606931866923958341785438440563570256923
3967214120760775046056499093488836286605925343014122920758357587042372478
4396732691590332416323376272386092941667490819065164058104132980059594910
2359008778669430615206773524319389966040554373094729579913188959958627665
0147054467689808335379318394367256072691585884193608098888834608988382517
9555400409255856681227954996219960904103899921767062227771904748352771827
3303956905161469311714970152690453526924382380410750656562412220963352392
0138329683948383338163097529599320269353721943944454940376358960836594051
3477809670299461167943887751256622044357084988532778474147865932282204513
2633031930514791856749172748713829709085669263868478956239297247880404451
7368621008068442797986335305249411008216146904415412953950001882685540890
1556780918561125527285831756180981771417450660976541919429826985921645962
9201276409873470500605529929901418584423540341991427848245574167168319010
4166469700540637579260581881947825466347482642862250106630069466021160621
5708653897225676975925511960349151350001134049757024302430257175193997151
4982583219749199092690545023085582866284162001345019227038307956196555313
3519516264189415251526792337107425827902360506088057955371784256616880268
3481205336046002789486284348178440170365034867338331652138861540971130253
9542269303050756935715270218806051136916996994319794858917896049906836482
7098398097827598580501664683202268902944648499801054993782628057786176349
9449403495561979111666771243943855744591594357175824358242664803240527503
7148981242949373077459055909120180426976328575611957482241886416886211456
4930533398315196939976469817186321830188058540785650068798616643153690897
1940003879624967405611779261257525540023345461636845524670403087434346354
2300902544572850469518439699765019550766539135275097854278939353265696979
8834439096861503266152867799080072157759748288907251319272957731038577344
6431535245545698319281988357996617254365469308202774764740270854428603109
0658615753163113372339326411333232695409430506796247862594824568714833737
6674586371616805578466172913102227164198256143554413339537206357013935647
0629147463681431162377855171292192075881285675544153979212279809633985848
6334720344048971403943003525729450686409883850490551307376237219794673145
1653521240236521969952971585422347256435489320408561433650553103621455311
4825379034920452163728194090871162565598571949541087264160366180879574884
7819973416435197872598418339953363758686913035927347605455007312010618225
2828781277896065251773210967393707756631839487624825752098569131518756381
8690024525152574157236464871652272646121213102628343423336658149133356868
0369390118251770450805020724331465525880295410904068122148550596391967976
0316372115721602906220401313546472893783885616487703074861532346434272567
6224887474718310372062434586017607508533640557298220265750588802657706884
1693715814060119176032809782368663807174910909867486843554494925790524702
2031454968860083834785048633719298029480406551563759326376458111133240080
9698676957085570003118431611007159904209095042061060056423179809759196141
4108228361780274366531731138323313273681795576174095447144561045234599507
6194227134059621083310577760480057291678454291609721473582990228292540584
7220152621542912717003056021400306862489029876623758468497347264800874607
0497564237799785255002204618467705426645994152603385213467462034868618759
6633615825771097366076405047236201921116428307785583745881849366988656930
4302932168354392028644558853067798145554385777127229140625369490000350171
7559648569526198722677883475465020803420776799839602027139286574802455505
5877425049471921303153011216333972815390192361918768620432643150220777026
9680664979099411443029479808238132526688405016406158058997404062685329277
8241393302275315995842312417759250272227533486142904065678940422293350276
7203185521092408789420562060743369693606635936966347020642524897059008713
0158900278592357012894977200992010323826650352236074791326375411649637745
3519624557227329410191105559631041359100689648672626758464563528279434070
6558861509034283932042786689140310681834581009319560349264357315869377969
0302342784950817743454555744064071757290206549636942369479300063807726601
4973130615209027727652018171049564772625669399319459667802043887151818927
0913007155076328707170438989171553671079133742732822879041887229643704411
7994537547047353258871121305054842843542796775044150453846116399521780197
9624014103419501566178264222606070672010510722876673981273167332243256347
4806399532467024709317233368476276538450476543327702712741827328531530516
8027452699430898999586455447912321706037983393869869116869533400495367526
9546599949891779321156363085470874912518167220155265668782927447585394430
4589389441354100333269682720945176587402534578669914176804302733519255081
6995225641510029537563527890130860311540130715201023031728098017613991470
3483399577777809438470383159919342130015156112047027093916390919511507067
9670949411987546768987778079492478658451834978153443354874815200900094231
4958501308259918525527660731683142370346393328300310341022636916428566795
7624045891594912799639484227530275255452455097007726048348405587238026906
8634839251501318761932980321595999925102874807697110350629814584949360001
0680569268640879656258950821472425241292977629113054752520768368498712813
8774779740352315078536495546752896630365075412806083752886940393508156073
2275973501778358814827394939018156615658996586168025320784231072816933142
1012119309903361777338434095371972077777452601237743248745297771758443641
6968057282662265405821486417797838733581015224793213178771040499907889109
8963868657262075556880875732945083109532749608889962171094671623215548952
5306729982647609691564425884156297847144585709595292678579425356237357661
1599905758458570672049159672577629038125316394764910688253665918240477217
2459880682984941647880958671566709581394666870839203949370147168699057682
4144213210355871252797074724369111002103225755201311770129872114061047250
3572583472994177568157818016370472655636868696163564156841497932751415551
4054399238261660551508979593580236452432644011215789164004824799421288575
6997136597512053635021414518486207889687047868791414291468455916652931744
6682385590455472216331449166098060976877012505305265332308885737370798411
2725626373268176421826458747928372841263266637656660438845686727544294945
2690409328436471588375041890680077843753361526503346420772132736189763619
0743125914239769186593726307040846501626923902778436228935545472737248075
9837098497713009771388513415495825241853072828615345985426683350304637479
3241759111233728652179997966456508271393109708213930113893292248134528947
1864720250066111635407231630503132446789566208148505683562744658068944141
2176763349569613208625955031082770724859616752930500433914086810162010332
8465544727575357452056287590038912201729249559136253934801272421518080981
3482497161481781427803465758781721485086990522812778083440253206147373153
8203190179060590427820681162660018061978736914735502525227260018995153233
4548744879049725174666244415047143525206158723933135742603063097731323001
9188477305926749422574968513760489776845793866735248660626517733066291276
9521699068103623420436933705434785080757836075993330683487467266964453873
6041680104189957969298117847857451608257496710247702743091623329036849828
2474848502495408070287264413464278241453971937423643215298338277231386632
6535804834059400142159998762478268182576630301631284608460319558650432439
7688697130946098414046196391821710541882311331763705241923212390150611868
8382878259522225960440343867915599168453706644187027504744329285460718970
9401423772209288751463874247042841877765178129971845107461934158634348157
7604782604324648156665366186743460902192919583923668275936193147254234835
3187828117397892973176526172961616396094409907459519671456596657956236025
7401140687132852748716608964429415825316417570402144690186097773166585181
5244604234308190093946630362184441850826684698698051645445312186068907062
7747013328711255353328059749477357400479282173802131826411440029181903503
7642889730442208783021486746051763738418541632162894842300184029117102598
7676300955574169711628720697151542322143558873329293629652673727281786564
9027103118642421603958290605283732674929235464650604072669705986934327957
4075211371759701017127636026867567013347746367072700059587018284856132864
0603722970670691579369151252734445464940156185578309071940283023400634249
3197631830698256169063269720324505427514591791691710637291716980761237752
1190446177555454831085057970699801769297580670961946286158353530479993518
9994158988703723706693241887541252760548498993853198982508802119679398436
8475841280472034059193462912965691282055673940207226669422761933873980890
3924874969239271549878166762474786850994394225482471626471215800710963517
8181247863830954360607641366578301895367549124331764944461762189065453644
5989425740478921174999111964495482782370655137527056146182445621663389820
5384785827250536885617707904791700750980086566655659828536506480170049592
4961909321581267958692430947172417095953440484887539558329471582318012707
8386678196042147650662800983106000230357490132344860626176658696008989847
0076556287596629652818855103814345376715161441991252866232721070835845827
1508346190487940553908756085264037077637032418626064298317239500267475797
0832165547139152633344499837604073185037992646476800301052096864917050261
6193735444674272010048722584970917704976440854009997555675729981901025377
0175837813545335548742484568580361302734737863811644913004930626198447295
7384004372181671677617373464890753787837631800814040379317934407502212060
0538663391885863933785814489604984908211973633635434683306924801097885769
0043030319076078838022212269137579914972477155817617608658275145902737104
4238345871797750951221233673324503360895728299779461034741047557521429449
4460368001069027669242117982221568073292320372854649034794646767407536970
2208793986432395527861092876058703741893206338205857836251867009420808654
7871983682029963788845367410649878293896713671520949635535984162032177787
1936102468137705442800457558532609694236313360798713636061527283622983764
6616763247230283561579958054279369454755161009643889534807265004319636567
6155935065884876198961972374410893199540128857327754010864181509959101002
3687953152830468752077945559092362782483458343838727884626485906456312211
6423993561968165983510511297069817964719989022981611988669061612451909740
5229476488947169595602453426354812106997125656955014329072733974629875789
2524141933958367922861260290466997220510844327675246435982447934520678245
462477207183837890625000000