I need to get the square root of a 210 digit number accurately, I thought GMP was the right tool for the job, what am I doing wrong?
#include <stdlib.h>
#include <stdio.h>
#include "gmp.h"
int
main (int argc, char *argv[])
{
mpz_t sq_me, sq_out, test;
mpz_init(sq_me);
mpz_init(sq_out);
mpz_init(test);
mpz_set_str (sq_me, argv[1], 10);
mpz_sqrt(sq_out, sq_me);
mpz_mul(test,sq_out,sq_out);
gmp_printf ("%Zd\n\n", sq_out);
gmp_printf ("%Zd\n\n", test);
return 0;
}
Input:
24524664490027821197651766357308801846702678767833275974341445171506160083003858 72169522083993320715491036268271916798640797767232430056005920356312465612184658 17904100131859299619933817012149335034875870551067
Output:
49522383313031109809242226159886283348695660460381271324714928680654813093947239 9634016783775955618921028
24524664490027821197651766357308801846702678767833275974341445171506160083003858 72169522083993320715491034366358025027526868495267716284867043049443779615862887 47102011391915422793532619329760963626718900576784
Here's the code you need for floating point square root, you can see that the initial input and final output are identical.
Here's the output with your parameter:
I am using a libgmp-3.dll
In vb.net I wrote this function for a high precision square root. I haven't tested it thoroughly but it gets me the Square Root of 3 to any precision I need
You're getting the result as an integer and then squaring that. The input number must not be a perfect square, so it is truncating the decimals and decreasing the precision of the number. Look into the 'mpf' category of functions for floats rather than 'mpz' for integers.