www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.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
{
public:
operator double () const
{
static double x;   // the "static" is necessary for DMC!  
// otherwise, the asm block seems to do nothing.
asm
{
fldpi;         // push the constant pi to the x87 stack
fst x;         // store to x
}
return x;
}
};

static CPi PI;

main()
{
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