www.digitalmars.com         C & C++   DMDScript  

c++ - Using the fldpi instruction for PI (3.1415...)

If anyone has been trying to get the cool tip for using pi
using the fldpi instruction rather than defining it as
a constant from the May 2003 C++ User Journal on DMC, here
is the trick.  You need to declare x as static below.  Enjoy!

I don't know if there is any advantage to getting pi this way,
but it's interesting.

#include <stdio.h>

class CPi
operator double () const
static double x;   // the "static" is necessary for DMC!  
// otherwise, the asm block seems to do nothing.
fldpi;         // push the constant pi to the x87 stack
fst x;         // store to x
return x;

static CPi PI;

double x;
printf("Hello \n");

x = PI;     // cool.  Looks like a constant, but it's a function call.
printf("Pi = %f\n", x);
Apr 06 2003