Re: Getting greatest decimal accuracy out of G_PI



On Sat, 3 Feb 2007 01:46:08 +0100
David Nečas (Yeti) <yeti physics muni cz> wrote:

>On Fri, Feb 02, 2007 at 04:14:09PM -0800, Sergei Steshenko wrote:
>> 
>> You can still use explicit cast, i.e.
>> 
>> ((long double)G_PI)
>> 
>> , can't you?
>
>If you can demonstrate a program that gets the value of G_PI
>to a long double with the full precision, please enlighten
>me.
>Yeti

I'll post this in case anyone in the future searches the archives
for an answer.

This works, but it needs the mpfr library and gmp from
http://www.mpfr.org/mpfr-current/

/*  gcc -o mfpr-test mfpr-test.c  `pkg-config --cflags --libs gtk+-2.0` -lgmp -lmpfr */

#include <stdio.h>
#include <gmp.h>
#include <mpfr.h>
#include <gtk/gtk.h>

int main (void)
{
 
  mpfr_t pi,g_pi;

  mpfr_init2 (pi, 200);
  mpfr_init2 (g_pi, 200);
   
  mpfr_set_ld(pi, 3.1415926535897932384626433832795028841971693993751, GMP_RNDN);
  mpfr_set_ld(g_pi, G_PI, GMP_RNDN); 
 
  g_print("PI is ");
  mpfr_out_str (stdout, 10, 0, pi, GMP_RNDN);
  g_print("\n");
  mpfr_clear (pi);

  g_print("G_PI is ");
  mpfr_out_str (stdout, 10, 0, g_pi , GMP_RNDN);
  g_print("\n");
  mpfr_clear (g_pi);

  return 0;
}

__END__

Output:
PI is 3.1415926535897931159979634685441851615905761718750000000000000
G_PI is 3.1415926535897931159979634685441851615905761718750000000000000



However, to be fair to Tor's comments, I did a rudimentary calculation using the
15 decimal point version of G_PI ( which is available to the standard libraries).
and found the resolutions on the surface of the earth and the moon, to be
pretty good  
( from the center of the earth asssuming 0.0000000000000001 rad angular resolution )
earth res:  0.00000000035 meters
moon res: 0.00000003835 meters

So unless I need to find a quarter on Jupiter, I should be OK. :-)

One other point though, g_print won't accept the mpfr_t as a long double,
so the "%Lf" format won't work.

zentara


-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]