Start New Discussion within our Software Development Community

Hi, I'm using GNU MP to get very precise square roots of numbers. I have three questions about my code below.

#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
 
int main(int argc, char *argv[])
{
	mpf_t num, sqrt;
	FILE *out;
	
	mpf_init2(num, 1000000);
	mpf_init2(sqrt, 1000000);
	
	printf("Enter a number to get its square root: ");
	mpf_inp_str(num, stdin, 10);
	
	printf("\nGetting square root...");
	mpf_sqrt(sqrt, num);
	printf("Done!");
	
	out = fopen("output.txt", "w+");
	
	if (!out)
	{
		printf("\nError opening output file!");
		getchar();
		return 1;
	}
	
	printf("\nWriting...");
	mpf_out_str(out, 10, 0, sqrt);
	printf("Done!");
	
	mpf_clear(num);
	mpf_clear(sqrt);
	getchar();
	return 0;
}

1. It writes the output in scientific notation. It's pointless because its just e1 usually. How would I make it so it's not in scientific notation?

2. It doesn't write all 1000000 digits. I should be getting a file that's about 977kb, but it's only 294. How can I fix that?

3. It allocates on the stack, so if I try to do 1 billion digits, I get a stack overflow. How can I make it allocate on the heap?

Hope that's not too much :P. Thanks in advance.
I'm on Windows XP using MinGW.

i used this library several years ago for a project... it's quite impressive.... however, i don't really remember anything to help you with.

so, i don't want to sound dismissive, but i think this sort of highly specialized library you'll probably find more precise help on their mailing list or archives

http://gmplib.org/#MAILINGLISTS

This article has been dead for over six months. Start a new discussion instead.