Exemplo n.º 1
0
void mpq_manager<SYNCH>::display_decimal(std::ostream & out, mpq const & a, unsigned prec) {
    mpz n1, d1, v1;
    get_numerator(a, n1);
    get_denominator(a, d1);
    if (is_neg(a)) {
        out << "-";
        neg(n1);
    }
    mpz ten(10);
    div(n1, d1, v1);
    display(out, v1);
    rem(n1, d1, n1);
    if (is_zero(n1))
        goto end; // number is an integer
    out << ".";
    for (unsigned i = 0; i < prec; i++) {
        mul(n1, ten, n1);
        div(n1, d1, v1);
        SASSERT(lt(v1, ten));
        display(out, v1);
        rem(n1, d1, n1);
        if (is_zero(n1))
            goto end; // number is precise
    }
    out << "?";
 end:
    del(ten); del(n1); del(d1); del(v1);
}
Exemplo n.º 2
0
Arquivo: add.c Projeto: 198767/cyy3
//add the two numbers
char* add_nums(const char* num1,const char* base1,const char* num2,const char* base2)
{
	const char* base_str[2];
	const char* num_str[2];
	char *p,*q;
	int base_gcd;
	int i,test_res;
	int base[2];
	ln numerator[3],denominator[3];
	vector digits;

	//test bases
	base_str[0]=base1;
	base_str[1]=base2;
	for(i=0;i<2;i++)
	{
		base[i]=test_base(base_str[i]);
		if(base[i]==-1)
		{
			printf("Error: invalid base [%s]!\n",base_str[i]);
			return NULL;
		}
		else if(base[i]==-2)
		{
			printf("Error: base [%s] is out of range "MACRO2STR(MIN_BASE)"-"MACRO2STR(MAX_BASE)"!\n",base_str[i]);
			return NULL;
		}
	}

	//test numbers 
	num_str[0]=num1;
	num_str[1]=num2;
	for(i=0;i<2;i++)
	{
		test_res=test_num(num_str[i],base[i]);
		if(test_res==0)
		{
			printf("Error: invalid number [%s],the number should match the regular expression [0-9]+(.[0-9]+)? !\n",num_str[i]);
			return NULL;
		}
		else if(test_res==2)
		{
			printf("Error: invalid number [%s] in base %d !\n",num_str[i],base[i]);
			return NULL;
		}
	}

	//convert the two numbers into fraction form
	for(i=0;i<2;i++)
	{
		denominator[i]=get_denominator(num_str[i],base[i]);
		numerator[i]=get_numerator(num_str[i],base[i]);
	}

	for(i=0;i<2;i++)
	{
		p=ln2str(numerator[i]);
		q=ln2str(denominator[i]);
		printf("operand in fraction:%s/%s\n",p,q);
		fflush(stdout);
		free(p);
		free(q);
	}
	//compute the fraction
	denominator[2]=ln_multiply(denominator[0],denominator[1],newln);
	numerator[0]=ln_multiply(numerator[0],denominator[1],firstln);
	numerator[1]=ln_multiply(numerator[1],denominator[0],firstln);
	numerator[2]=ln_add(numerator[0],numerator[1],newln);
	//free unused numbers
	for(i=0;i<2;i++)
	{
		ln_free(&(numerator[i]));
		ln_free(&(denominator[i]));
	}


	p=ln2str(numerator[2]);
	q=ln2str(denominator[2]);
	printf("result in fraction:%s/%s\n",p,q);


	//simplifying the result fraction
	base_gcd=gcd(base[0],base[1]);
	if(base_gcd !=1)
	{
		while(ln_divideable_num(numerator[2],base_gcd)==1 && ln_divideable_num(denominator[2],base_gcd)==1)
		{

			ln_divide_int(numerator[2],base_gcd,0,trunc_res,firstln);
			ln_divide_int(denominator[2],base_gcd,0,trunc_res,firstln);
		}
	}

	p=ln2str(numerator[2]);
	q=ln2str(denominator[2]);
	printf("result in fraction:%s/%s\n",p,q);
	fflush(stdout);
	if(strcmp(q,"1")==0)
	{
		free(q);
		q=(char*)malloc(strlen(p)+4);
		if(!q)
		{
			ln_free(&(numerator[2]));
			ln_free(&(denominator[2]));
			printf("Error: coverting fraction to decimal number failed at line %d",__LINE__);
			return NULL;
		}
		sprintf(q,"%s 10",p);
		free(p);
		return q;
	}




	free(p);
	free(q);
	digits=get_decimal_digit(numerator[2],denominator[2]);
	if(!digits)
	{
		ln_free(&(numerator[2]));
		ln_free(&(denominator[2]));
		printf("Error: coverting fraction to decimal number failed at line %d",__LINE__);
		return NULL;
	}

	p=get_decimalstr(digits,denominator[2]);
	if(!p)
	{
		ln_free(&(numerator[2]));
		ln_free(&(denominator[2]));
		free_digit(digits);
		printf("Error: coverting fraction to decimal number failed at line %d",__LINE__);
		return NULL;
	}
	return p;
}