KevCaz's Website

SageMath has tons of powerful features (I explain how to install the latest version on Debian buster in a previous note), one relatively simple and very handy function I’ve recently used is numerical_approx() (see the documentation) that returns (suspense = 0) a numerical approximation for any number of digits!

In my case, I was looking for a 40 digits approximation of 4*pi/(6+sqrt(38)). Using a programming language such as R, I would have had to tweak the printing format:

1
2
3
4
4*pi/(6+sqrt(38))
[1] 1.033044
R> format(4*pi/(6+sqrt(38)), digits = 22)                                  
[1] "1.033043647749253723944"

and I would actually have been limited to 22 digits.

1
2
3
R> format(4*pi/(6+sqrt(38)), digits = 23)                                  
Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L,  :
  invalid 'digits' argument

SageMath is actually built to do number theory and handling tons of digits is actually what t does! So I can easily get a 40 digits approximation.

1
2
sage: numerical_approx(4*pi/(6+sqrt(38)), digits = 40)
1.033043647749253740591407364288254970944

There is a very short alias n() for this function:

1
2
sage: n(4*pi/(6+sqrt(38)), digits = 40)
1.033043647749253740591407364288254970944

and I can get way more digits!

1
2
sage: n(4*pi/(6+sqrt(38)), digits = 4000)
1.033043647749253740591407364288254970944109133424748801547378259072396980381009839140666652799845659619674815697997441649372750454772329937208733973788101823573933513419098604619125846812804045788367926723074572465829930802219213189847671289780008644879814571790658446508642003075647023615868909425286835695207851589951199272708097168184976768873711166440903083765837119266191826644607844653853352542002921344214323865125751905036994960918496724067650169946345900849450610139260086639900478956997800603576543649539275886614548386915821469548582810454734966892585809088812955172873828355268056214530115641841747424108677020131753984639236530953948313163206385724725470314984838945588279964602934888840735542643108280519337957362499124354259366545734514502570060142836452771224586352503132064854564632157115785308867394772472863719865419229291469170091411154110713097198031026583948356277596749559354866713893327549233003471031125680142008846692398147740595929970511257187002243030635324166787468981534452476072024906521279884551913101013452324929419079509575511612493183118342622891873539314573553839183616907131362223224790465540196226303017619558559939984357697833895766254214721529639852771995950425167647548991041100851031740078618654286426734209017908082181539491688997315942134835448616475324457434026572151955837287603315148142043450631453241311690223205597585222571007237153116233118528087534097151578435284438405976382275496203679571827826028372253100738848702846518471794140116062193110767163222244585179477984398560407073503681153603965334568570586819954601040073778827445959244326717645632214880255709628862994253473539301314566537761922852571373316618536188739502773074743770162229356127303526208702820052350778053092846945966531452542022058385423943430917613310154429623271999763985617488436627732225934018367784195766504796757424899019991692373725299057467798916507123962418095481310578745661356575037635745689956975007974481549405053957804411420590623144100866996712236476411124444129665000425962610500112716620148477508037155697679199831588160073752495591446413162943749988967190913261647140077786919641293166082389801483895077086383043851618883190173628897755029284742686837635997108823370702406873188524872397354369770617692223266194275033825889326641709558184216637212271805081090080748423925521286516281867450826119455922999577277431012976432829725448834553224436156082875785019583332617182239501689851440428131240055631385849659248664010967689746609501273573095323836674834187584992332469064411370481029690293766192249284844395220663510002979381912385246034284107295199408244715099187616437340544754251729371933377108433764038399887810281231035475305182460924412720729184217493698608715102849960281715235079368226778010197724234872598700137340843473155080416447882448451331531776614443494293325789867343416604628924485670907853301443284740352737113054714798494398275601314741140143879242531819046932273023703801405785350080823997705570191770999140511140461808989469110879035616816145588360350046488159315194875478274668194282154758412417307323923057674375243026102993531746157115564917619602284918211285933499607303268825629375791763172822305719039435826954550968418033541867276857512709691754044951478108347983293016591692962207127505657472975020964394928468181656742385920316045914233443228386168787186519130970906214394932950801977030703274476181635405735020305106214509961191128470279850642571470763060041281504906424648188661214442682238893104672529707070624352096559933829630320066842473345280606180459742026469695768268323796055723386970898250155510695938474263346708877095169248292014610008693035529456565371446006247409699538975878216235022511814314881523351454050636122865587664748605462210145658780671008057508686330995370371612551992154815962986874177450825282198262260080831516254773463568611812642558138417133160377578470682891284179649754332315508681873862399862580866728345710997338609082956180563754274979318486323914104799495547869012110302300833121673314083617

Sweet 😎!

I tend to use Sage more than Octave but I would like to mention that you can also use Octave symbolic (use apt-get install octave octave-syboclic for with apt on Debian and Debian derivatives).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
pkg load symbolic
digits(100)
vpa(`pi`)
3.14159265358979323846264338327950288419716939937510582097494459230781640
  6286208998628034825342117068
% OK
vpa(4*pi/(6+sqrt(38)))
1.03304364774925372394420719501795247197151184082031250000000000000000000
  0000000000000000000000000000
% Not OK similar to R approx
vpa('4*pi/(6+sqrt(38))')
1.03304364774925374059140736428825497094410913342474880154737825907239698
  0381009839140666652799845660
% OK quotations marks matter, similar approx as SageMath!

That’s all folks!