Recent

Author Topic: extended to str with maximal precision  (Read 5840 times)

BeniBela

  • Hero Member
  • *****
  • Posts: 928
    • homepage
extended to str with maximal precision
« on: October 10, 2012, 08:07:01 pm »
I need to print an extended value with maximal precision, but none of these functions worked:

Code: [Select]
var b: extended;
begin
  b := 999999999999999999;
  writeln(b:25:25);
  writeln(FloatToStr(b));
  writeln(FloatToStrF(b, ffFixed, 25, 0));
  writeln(FloatToStrF(b, ffGeneral, 25, 0));
  writeln(FloatToStrF(b, ffFixed, 25, 25));
  writeln(FloatToStrF(b, ffGeneral, 25, 25));
  writeln(FloatToStrF(b, ffNumber, 25, 25));
  writeln(FloatToStrF(b, ffCurrency, 25, 0));
  writeln(FloatToStrF(b, ffNumber, 25, 0));

they all print print 1000000000000000000 :(


This is not a storing/rounding problem, since the debugger shows the correct value.  As does writeln(round(b)) or writeln(trunc(b)),

BeniBela

  • Hero Member
  • *****
  • Posts: 928
    • homepage
Re: extended to str with maximal precision
« Reply #1 on: October 23, 2012, 03:28:19 pm »
*ping*

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: extended to str with maximal precision
« Reply #2 on: October 23, 2012, 03:30:34 pm »
FPC ML perhaps?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BeniBela

  • Hero Member
  • *****
  • Posts: 928
    • homepage
Re: extended to str with maximal precision
« Reply #3 on: October 23, 2012, 03:42:42 pm »
Perhaps. But I'm not subscribed to that list only to the Lazarus one.

That's why I always ask fpc things in the forum

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: extended to str with maximal precision
« Reply #4 on: October 23, 2012, 03:56:06 pm »
Procedure str_real in rtl\inc\real2str.inc limits the precision to 25-6-2=17 characters. If you think this is wrong, please create a bug report. Delphi help http://docwiki.embarcadero.com/Libraries/XE3/en/System.SysUtils.FloatToStrF specifies 18 characters  ;)

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: extended to str with maximal precision
« Reply #5 on: October 23, 2012, 04:28:49 pm »
If you want unlimited precession.
You could use the GMP unit. ->
http://wiki.freepascal.org/gmp

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: extended to str with maximal precision
« Reply #6 on: October 23, 2012, 05:39:17 pm »
For a bit of fun I thought I'd see what the GMP unit was like.
I modified the printf_example in the fpcsrc\packages\gmp\examples to using 8192 bit precession, and also added an example were I squared the 999999999999999999 from @BeniBela post, and Sqrt's it for good measure.
btw. 8192 bit precession is not the max, eg. I did 81920 bit precession, but the numbers get very very large. :)

So if you ever wanted to know what the sqrt of 2 , to 8192 bit's is, here is is.  Also at the end @BeniBela test.
Code: [Select]
Sqrt(2) to 2466 digits (8192 bits) = 1.41421356237309504880168872420969807856967
18753769480731766797379907324784621070388503875343276415727350138462309122970249
24836055850737212644121497099935831413222665927505592755799950501152782060571470
10955997160597027453459686201472851741864088919860955232923048430871432145083976
26036279952514079896872533965463318088296406206152583523950547457502877599617298
35575220337531857011354374603408498847160386899970699004815030544027790316454247
82306849293691862158057846311159666871301301561856898723723528850926486124949771
54218334204285686060146824720771435854874155657069677653720226485447015858801620
75847492265722600208558446652145839889394437092659180031138824646815708263010059
48587040031864803421948972782906410450726368813137398552561173220402450912277002
26941127573627280495738108967504018369868368450725799364729060762996941380475654
82372899718032680247442062926912485905218100445984215059112024944134172853147810
58036033710773091828693147101711116839165817268894197587165821521282295184884720
89694633862891562882765952635140542267653239694617511291602408715510135150455381
28756005263146801712740265396947024030051749531886292563138518816347800156936917
68818523786840522878376293892143006558695686859645951555016447245098368960368873
23114389415576651040883914292338113206052433629485317049915771756228549741438999
18802176243096520656421182731672625753959471725593463723863226148274262220867115
58395999265211762526989175409881593486400834570851814722318142040704265090565323
33398436457865796796519267292399875366617215982578860263363617827495994219403777
75368142621773879919455139723127406689832998989538672882285637869774966251996658
35257761989393228453447356947949629521688914854925389047558288345260965240965428
89394538646625744927556381964410316979833061852019379384940057156333720548068540
57586799967012137223947582142630658513221740883238294728761739364746783743196000
15921888073478576172522118674904249773669292073110963697216089337086611567345853
34833295254675851644710757848602463600834449114818587655554286455123314219926311
33251797060843655970435285641008791850076036100915946567067688360557174007675690
50961367194013249356052401859991050621081635977264313806054670102935699710424251
05781749531057255934984451126922780344913506637568747760283162829605532422426957
53452902883876844642917328277088831808702533985233812274999081237189254072647536
78503048215918018861671089728692292011975998807038185433325364602110822992792930
728717807998880991767418
Squared back = 2
Minus 2 = -1.8336038675548471656214123920484831659563649713585672372826389505365
83840000711992410640218596712179594335698107817974197011177209630798395615538564
59827371376065660299569402515271235578816777173842568113379050870797846264223806
88921772203293864853874151945393106543137924435757979175016453999750242906748831
88833878389782628119818853904403803808660158822401456197445726044525360571951789
21757990596359223496949655715724677092585880906977009293267771352644573800455265
34466387904170422125479363391730635962953483077410165049672359735928955745463723
05721239614176262441386734381374022650503368256329698100766046151929617286674248
95294010736352683374212102295659695944625074841324221167225382905301794214058679
29945813703182330724177043335229098988320026776048781468572794356396705813426391
64194574821146279252617228754569853512930346011009782592900168310525464637140708
66499181562069992356951249562038172372027077641686884043616923298947486137443570
05119737673778181097396808918377190615601534305378858875109803578829530183698873
23207979668184533354275023241009846972819429328181833703679531204490462378898935
76974957942138791091787568336047488778709818337029243352461232567745553121264270
88760085279372667200098526993637024223579698528909359180898854722604896032344418
54367204863486517248296173551553049316288617775152952906691901830867687275687826
54131393972721727575815481272733118397055005602792636168890991999570033707402374
60377385736704575043355519264004766353251407567096291902615629527181703423528211
51382217172184626875154472648516789197525649411375099051145686249696468087587739
34709531944863066212404424469570895972260694221411447267284660655059376470663887
55260099609325302763117116916601954051764662058319443955140281989077454224438474
61611733571313351033522985476845764156344394535522173665450124796468183142943411
41122999469702875056605608425435075436895433872582204795016109187162458560783731
89858060167022779184010761717076378721835057432876881882332544430926597343646685
15174092320424433627826763690934204956582706229528322857377027839817477157196746
45133636823632918531632337624630836071785185957511490697605264438253649316317822
47109240902543684361686244964512837406983471975686949522373067405664967691219676
13145174219931934326223662043135267467867075590936330890711375765764618838038673
21243003666337805376370480822506354037267708351625495227172835577304138568158165
712163506688824727670219195671776354721589554524747764612888344054013492700531e-
2466
BeniBela = 999999999999999999
BeniBela Sqr = 999999999999999998000000000000000001
BeniBela Sqrt = 999999999999999999

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: extended to str with maximal precision
« Reply #7 on: October 23, 2012, 05:51:56 pm »
That is roughly 2465 wrong digits in "Minus 2"  :D

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: extended to str with maximal precision
« Reply #8 on: October 23, 2012, 08:04:18 pm »
That is roughly 2465 wrong digits in "Minus 2"  :D

Oh Crumbs!!. :) 

Forgot to show the result rounded to 2464 decimal places.

BeniBela

  • Hero Member
  • *****
  • Posts: 928
    • homepage
Re: extended to str with maximal precision
« Reply #9 on: October 24, 2012, 12:30:25 am »
Procedure str_real in rtl\inc\real2str.inc limits the precision to 25-6-2=17 characters. If you think this is wrong, please create a bug report. Delphi help http://docwiki.embarcadero.com/Libraries/XE3/en/System.SysUtils.FloatToStrF specifies 18 characters  ;)

I made one: http://bugs.freepascal.org/view.php?id=23196

But is there really not some little function for that somewhere?
It's so basic functionality...

(btw the 999999999999999999 from the example is not the problem, since you can just round it to int64 and print that. But I need it for non-integers as well)

If you want unlimited precession.
You could use the GMP unit. ->
http://wiki.freepascal.org/gmp

I don't need that.

I need exactly extended / 18 digit precision

 

TinyPortal © 2005-2018