Gleitkommadatentyp mit mehr Stellen als long double?

diviner

Mitglied
Hallo :)

Ich habe eine Frage...

Gibt's da einen Datentypen, der mehr Nachkommastellen als long double bietet? :confused:

Ich möchte die Zahl Pi mit mehr Nachkommastellen speichern, aber long double kürzt die Zahl zu sehr ab. :(

Schon mal danke!

Gruß
diviner :)
 
Blicke da überhaupt nicht durch, wie man es einsetzt bzw. installiert...
Geschweige davon, wie man es in einem Programm benutzen kann. :confused:

Kennst du dich mit GMP aus, Tobias?
 
Hallo,

wie man es installiert kannst du hier finden:
http://www.cs.nyu.edu/exact/core/gmp/

wie man gmp anwendet und damit PI auf ca 20000 Stellen genau berechnen kann
zeigt dir folgender Code:

C:
#include <stdio.h>
#include <gmp.h>

#define PRECISION 65536

void init_a(mpf_t a){
	mpf_init2(a, PRECISION);
	mpf_set_ui(a, 1);
}

void init_b(mpf_t b){
	mpf_t sqrt2;
	mpf_init2(b, PRECISION);
	mpf_init2(sqrt2, PRECISION);
	mpf_sqrt_ui(sqrt2, 2);
	mpf_ui_div(b, 1, sqrt2);
}

void init_c(mpf_t c){
	mpf_init2(c, PRECISION);
	mpf_set_d(c, 0.25);
}

void init_alpha(mpf_t alpha){
	mpf_init2(alpha, PRECISION);
	mpf_set_ui(alpha, 1);
}

void calc_a(mpf_t a, mpf_t b){
	mpf_add(a, a, b);
	mpf_div_ui(a, a, 2);
}

void calc_b(mpf_t b, mpf_t y){
	mpf_t mul_res;
	mpf_init2(mul_res, PRECISION);
	mpf_mul(mul_res, b, y);
	mpf_sqrt(b, mul_res);
}


void calc_c(mpf_t c, mpf_t a, mpf_t alpha, mpf_t y){
	mpf_t sub_res;
	mpf_t mul_res;
	mpf_init2(sub_res, PRECISION);
	mpf_init2(mul_res, PRECISION);
	mpf_sub(sub_res, a, y);
	mpf_mul(sub_res, sub_res, sub_res);
	mpf_mul(mul_res, sub_res, alpha);
	mpf_sub(c, c, mul_res);
}

void calc_alpha(mpf_t alpha){
	mpf_mul_ui(alpha, alpha, 2);
}

void approx_pi(unsigned int n, mpf_t pi){
	mpf_t alpha, y, a, b, c, sum, mul;
	int i = 0;
	init_alpha(alpha);
	init_a(a);
	init_b(b);
	init_c(c);
	mpf_init2(y, PRECISION);
	mpf_init2(pi, PRECISION);
	mpf_init2(sum, PRECISION);
	mpf_init2(mul, PRECISION);
	for(i = 0; i < n; i++){
		mpf_set(y, a);
		calc_a(a, b);
		calc_b(b, y);
		calc_c(c, a, alpha, y);
		calc_alpha(alpha);
		mpf_add(sum, a, b);
		mpf_mul(sum, sum, sum);
		mpf_mul_ui(mul, c, 4);
		mpf_div(pi, sum, mul);

	}
}

int main(){
	mpf_t pi;
	int bytes_written = 0;
	approx_pi(20, pi);
	bytes_written = mpf_out_str(stdout, 10, 0, pi);
	printf("\nBytes written: %d\n", bytes_written);
}

Siehe dazu auch:
http://www.uni-leipzig.de/~sma/pi_einfuehrung/ausblick.html

Gruß,

RedWing
 
Zurück