Skip to content

Commit d0ebf19

Browse files
committed
Adding proper calculation ability in C
0 parents  commit d0ebf19

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

Formula.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.5628174515246319097920037345755921749158240158494036500429325486329392219847724818569444673481237844175822402105052395571147633575634946538565470855719335798954668524357587541849323450395232795115821588311616940118758594049822961297385338599579711141011744312111543025740695689831455804818309891707048709831014810204482081830193378409140281071988541759464768204840339552005540395019518486600397665509148538134016430172001596465189133584135441969430850529875469280251520393092914640577387719960101946393907626021874965451215443711698774817801442872407674858677296103520211436100729601800112936176890344169351181892512483309505393108759954596196047356107270376733111897628936300834056177158735661140977034943966179944067850252353262865887932072917270909088986693381267807668444071526240590881708333575167337670632776254353689068094688871326495738328108578079310525790435129640523852602121113355346778567996220012279449183952257901351509902945839281708723610331266566553579465110116556650243398658986649745301599593044384089621187277449892009575182821238091453578219781417953940035890873218485591512705713819215415755137641779451607678700326714309799347178851851452850844078937601763773066086927401228472706689496079325697099115577813398881272544584201560271634537218181120974678342315403296611987821242827644048639695497842136940960875180202275252118642478625824124348436782439170338161428731554803143602004347966365429812339053800553572989087988733097033416644058017362399760081337207300273601035903295110457577370575287242812269328625965144132217454163040909785080891987998226866931167929655933108636268627880881794753980885155511278604162116188652468475322882340526147045732260308748300290268440430059659991708618728908480511249156286042740142576842282451386260855156159094388751638392973690705260644284996096182815022276067499628989831672971196436062739896526422920316649609123777682779453397865402954269537353620415443451717865723712005420203662821068216183486366830404364485450666524112868333640573975999915922745251436565059955810860367133641205420098875397889076347314013609625232985683139042039285604109829695360397709343070802130136176308483546708133471685764363831206447079169108581783356582936206301338745706305235029323141761523205044946376881074347845782404041158796316665864949277725602616126708232822887237375983964035594592634285574227539729494319025399091485178357290199879782400417904493345659591932507655572953428360899824882794463908090697396067063114689024870940429176793961573242019444871336531011925899615813697726288603478849742232370851083293202423150470247333085234504860806285930543222376460024118540586255913803164970003149532502101807558511615287574218055684758634405857444278032698937164732960357455766924044920498782280181947602085055343293559664427941797392062504043770667076051776941655157739773299104324701596178612445266637296973425591230398039628362816490035082140480089630087188918377044250474534903132246748469098851319126136043019060494265240916155058304225616539539396441015479629717822617857729176830789124861674385064142962998671132402002014697194960355691892304317978766963960351342336897544610353146902663127516044647395526007901330653345965632580676964164184244578038231723223891712537599448141316327449924854800354620383008710695319546865323402450441737366563950660798332965416688136934843728351992956671063063376148655112769359934245673452979074776818468762114119554424941763882920418510637425550953302657693465698542821496654864685512084423726711735690514722878192922092550580517983772102609906326074131266669060107821610848899385628647683487054885139977233534903782744436644878219529348958120244797610718638164677664337959627912248510504990714263166025476679249871903690158997305244216315641216854349066004305688445792398249012160184295498411868372593981394290069334665527869148864236052442717594619078115657822169929938523476553579641680620824839750059804515330086978198671883784424772693247515409616454654340874765705286549823650224780896598012496913736145714632958678032417964694808247379788352904792464655539035467613801212756677478847401423601419317494904523607334446314761235957571744013265355736924194898971416752696242614474183884629912292324237205643980358657855391261034295009031125187881221257899891572266191401703076133602292616802810647562977174798634827731888945126522108759357935705088192111999705689159397408165938547236524995017553277158644599254791888181847757610791083299206340317709960220670455724980767504188631473441139767690038017240813164196886867515925605903106823527741768853994196282533434531026565289729206336295955363416905694527175519160394271499803643814799121505780565950442402469217436447185925167322983206516523714454902607803771291896267132802613439715432524195263614820084891250907446069147574917959166103827351708563125205551321528204598781241097924674982768455760118302604309681497793708867799343167528276617936860398645252810144662490664337624008651564144909805548610127054760954337930951963093649538252860244828295835196431955181244112975150951463245044166344548899396574501441328279193687639387576914016983724721720372462732293194575628154036983666025640272425472524668054796071665627621299744434095367647887058725095768284516020821149657455205743153023755448146362012841753617266620752366152738434202627198881949997328315161225519181466450308542632214133633019814167189270505705329541192973295875935103279231387457944985865903384230311197564284633944251913076251379764376179911640812941676385956313355300023833180661717983083509416458526593232009514670654106044851184314200362552239054943410393132589727305390245556586641150938818734250395303351799977025328298408951887893804013127913160032259476307754040029107703401313220901757900728986696915775163856927372819339105033711748812650558377777805545147984378967015361550398408627605866017867875126715767974431944229771155066776515818659911203760022000825937201069156235148770210120777711674352674694885670385880088602381012833854804460087193438123630436148010833984548094889747060902472599028149806156002368263387046519476766482419980534944646322103907339324863989231043733044952035221466807838314532383749108508748122897511341690149666955796988733058123031876914294755812853601829663517712024722048103992981431557452422201010700815913466921816528303073231529033963379226670201520514954866263381230329994944289607787482920114698409506610521549162598259816361593328849168154441546868180910124685959800919134752956690137531716512408532129673886759623038896918817709287616141140566568610465439320264616183655002117885252250779918476734672744372334464243886148214626212194698768877171515495324962171658836564208464056400981073097050258302182802142725704065178348410451588652773556970607327535595688951002746578115916883008222003542977578101356731039534129061332199705032463968472622598538570531930162000906429241247228513062769442106380978417804028142479988382981041510721250070913280823755671368697359421680033072538023826757266584969861719213357685629822163654218485955398775135353505701890586244069435926396456491916193227432954063427042566449276259704870978565798929475904563158054108114008460505877869408915883549165828867561651088697275176747166531473892685158426998408186979677886274398376854616129981576960000583961561914030997555323783412268232477507217441559778300605723033684404599396595359342655350282762789923748227814921460232434810952061663581882043217195842908136441187323986644646072944577510930812353585488269658484237714900312272622206426979433464662219279579806126694417227827945053351924776172942568912507021948258711742653883644009202834947529161731757274070360670994936299713229388626815248209592071205589707308409555053599082011154762926602512597280758713212717207858865157363982807774097855842960787863769912291849180335435577989617644077035191613631942225691176755414863649704129081796113499012688755544197071018920608959654018247702614948283914443574759244678498206084494796958779977453889456697089943793301256110567838991146937687731930063063539824938570639860386640510425329140173511646904467050565299960163779444825607232115970586332794665852633811584024744981493558119135429859881462797522939672124279987053545521951143141642106639401378157291493381211405868413899172212745306710325143943220109251304140914143675274604049838941564184984363071848034883928650412110377527651044370193555250015763430579894979191484252862302552759988270583093991755616870930694133786126651019248522262692213560935118048085855540951266581367884756405018240324109822191076221173061312122619678241943863343960537500910050710172547690844954223390400624836828836560375638531785698771554096810332208184792688493510619453960137540249095588749706397231196487335293742924638949515845056151249655861804100369747911916080951242628071706444699809939058741319798507942477996393712354859922389007238667220171051099736572314114746996928924494112254727697297032670502812727579063552388126160921870174761023200854276012097844914260463498426437106184453249545744528469750232127211419712954173332898038893736604365296932524218634873207347287816821651052450346638457255309380042378066046307897963743620294495339806524468491632636878758438619207940699566696953595101817693860911552082582103457568021189708244780888374709598449275189952844956337571980475872750639796743442188989310182919128751947427370707460339464588614958406549483944481368721680273843842156716380321012497879055135532882733440995785995329174051715285553113382049851724281939562567115559506512746779599996607856239991445863071638037635818948401441541142061577725966292490587974006010520823881998430572794317171589923164690329789316145041558607439689199079482013226444259135672299015603926809940217862687227004311010712050581274430253361579497127554981750516812326170636645137101959259309597050864487264796896738246521290168182929656886751319068999386505691030850100541321354786777408211003555547593904499189212611696309325525358558837189686018950576110517260958912015167080438826677598753027436967748068315299

main.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include <mpfr.h>
3+
4+
5+
int main(int argc, char const *argv[])
6+
{
7+
mpfr_t x, a, b, c, d;
8+
unsigned i = 0;
9+
mpfr_set_default_prec(16384);
10+
mpfr_inits(x, a, b, c, d, NULL);
11+
mpfr_set_ui(x, 0, 0);
12+
printf("Precision: %li\n", mpfr_get_default_prec());
13+
while (i < 400000)
14+
{
15+
i++;
16+
mpfr_add_ui(a, x, 1, 0);
17+
mpfr_div(b, x, a, 0);
18+
mpfr_pow(c, b, x, 0);
19+
mpfr_set(x, c, 0);
20+
}
21+
FILE *file = fopen("Formula.txt", "w+");
22+
mpfr_fprintf(file, "%1.10000Rf", x);
23+
mpfr_printf("x = %1.10000Rf\n", x);
24+
return 0;
25+
}

thread64.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <mpfr.h>
4+
#include <time.h>
5+
#include <assert.h>
6+
#include <pthread.h>
7+
#include <unistd.h>
8+
9+
#define STEP 25
10+
11+
struct do_calc_args
12+
{
13+
double *times;
14+
int i;
15+
int max;
16+
int nproc;
17+
};
18+
19+
void *do_calc(void *vargp);
20+
long long unsigned run_calc(int);
21+
22+
int main(int argc, char const *argv[])
23+
{
24+
long nproc = sysconf(_SC_NPROCESSORS_ONLN);
25+
pthread_t *threads;
26+
double *times;
27+
28+
if (argc != 2 || nproc <= 0)
29+
exit(1);
30+
int max = strtol(argv[1], NULL, 10);
31+
32+
printf("Using %ld threads\n", nproc);
33+
FILE *file = fopen("number.csv", "w");
34+
fprintf(file, "");
35+
fclose(file);
36+
37+
times = calloc(max, sizeof(double));
38+
if (times == NULL)
39+
exit(1);
40+
41+
threads = calloc(nproc, sizeof(pthread_t));
42+
if (threads == NULL)
43+
exit(1);
44+
45+
for (int i = 0; i < nproc; i++)
46+
{
47+
struct do_calc_args *args = malloc(sizeof(struct do_calc_args));
48+
assert(args != NULL);
49+
args->i = i;
50+
args->max = max;
51+
args->times = times;
52+
args->nproc = nproc;
53+
pthread_create(&threads[i], NULL, &do_calc, args);
54+
}
55+
56+
for (int i = 0; i < nproc; i++)
57+
pthread_join(threads[i], NULL);
58+
59+
free(times);
60+
free(threads);
61+
62+
return 0;
63+
}
64+
65+
void *do_calc(void *vargp)
66+
{
67+
struct do_calc_args *args = (struct do_calc_args *)vargp;
68+
double *times = args->times;
69+
int j = args->i;
70+
int max = args->max;
71+
int nproc = args->nproc;
72+
for (int i = j; i < max; i += nproc)
73+
{
74+
times[i] = (double)run_calc(STEP * (i + 1)) / CLOCKS_PER_SEC;
75+
printf("Time for %ddp: %F\n", STEP * (i + 1), times[i]);
76+
}
77+
mpfr_free_cache();
78+
free(vargp);
79+
return NULL;
80+
}
81+
82+
long long unsigned
83+
run_calc(int dp)
84+
{
85+
mpfr_t x, xold, tmp1, tmp2, precision_bits, e;
86+
long long unsigned wallclock;
87+
long long unsigned i = 0;
88+
long unsigned precision;
89+
FILE *file = fopen("number.csv", "a");
90+
fprintf(file, "%d, ", dp);
91+
92+
/* Calculate precision */
93+
mpfr_init(precision_bits);
94+
mpfr_set_ui(precision_bits, 10, MPFR_RNDU);
95+
mpfr_pow_ui(precision_bits, precision_bits, dp, MPFR_RNDU);
96+
mpfr_log2(precision_bits, precision_bits, MPFR_RNDU);
97+
precision = mpfr_get_ui(precision_bits, MPFR_RNDU) + 1;
98+
printf("Using %lu precision bits (%.4FKB)\n", precision, (double)precision / (8 * 1024));
99+
100+
/* Init X, tmp1, tmp2 */
101+
mpfr_inits2(precision, x, xold, tmp1, tmp2, e, NULL);
102+
mpfr_set_ui(x, 0, 0);
103+
mpfr_set_ui(xold, 1, 0);
104+
mpfr_set_ui(e, 10, 0);
105+
mpfr_pow_si(e, e, -(dp), 0);
106+
107+
/* Perform the calculation */
108+
clock_t start = clock();
109+
while (1)
110+
{
111+
mpfr_sub(tmp1, x, xold, 0);
112+
if (mpfr_cmpabs(e, tmp1) >= 0)
113+
break;
114+
115+
i++;
116+
mpfr_set(xold, x, 0);
117+
mpfr_add_ui(tmp1, x, 1, 0);
118+
mpfr_div(tmp2, x, tmp1, 0);
119+
mpfr_pow(x, tmp2, x, 0);
120+
}
121+
clock_t end = clock();
122+
wallclock = ((end - start));
123+
fprintf(file, "%llu, ", i);
124+
fprintf(file, "%.4F\n", (double)wallclock / CLOCKS_PER_SEC);
125+
fclose(file);
126+
127+
mpfr_clears(x, xold, tmp1, tmp2, precision_bits, e, NULL);
128+
129+
return wallclock;
130+
}

0 commit comments

Comments
 (0)