symbols

New to FreeBASIC? Post your questions here.
Post Reply
sleeping_sun
Posts: 3
Joined: May 15, 2011 21:51

symbols

Post by sleeping_sun »

How do u write pi=3.14 on freebasic?
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

in C it would be "const float PI = 3.141592..."
in FB its almost the same:

Code: Select all

Const As Double PI = 3.14159265357989
or
Const As Single PI = 3.14159265
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

I use...

Code: Select all

#Ifndef PI
     #define PI     ATN(1)*4
#Endif
-Vince
sleeping_sun
Posts: 3
Joined: May 15, 2011 21:51

Post by sleeping_sun »

Thank u guys :)
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

vdecampo wrote:I use...

Code: Select all

#Ifndef PI
     #define PI     ATN(1)*4
#Endif
-Vince
You need additional parenthesis there, otherwise PI^PI won't evaluate correctly. Or you could just make it a const.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

If you use Atan2(0,-1) instead of 4 * Atn(1) then you don't need the brackets!
I sometimes use the following in FB.

Code: Select all

Const As Double Pi = Atan2(0,-1)
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

@Vince

Won't using the ATN in a macro recall that slow trig func for every PI calculation?
qbworker
Posts: 73
Joined: Jan 14, 2011 2:34

Post by qbworker »

If you don't need anything manually deallocated, just use:

Code: Select all

if multikey(&h01) then end
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

rolliebollocks wrote:Won't using the ATN in a macro recall that slow trig func for every PI calculation?
No. The compiler evaluates constants when compiling, so in the code you see (Atn(1)*4) but when the program runs it has been simplified by the compiler to 3.141... Or at least that's what the speed test I just did suggested.
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Post by jevans4949 »

You could make use of the fact that the Intel Floating Point Maths Unit will return you the value of pi.

Code: Select all

function pi as double
    asm
        fldpi
        fstp qword ptr [function]
    end asm
end function
  
    
print pi
Dim pi2 As Double = 2*pi
Print pi2
Sleep
    
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

jevans4949 wrote:You could make use of the fact that the Intel Floating Point Maths Unit will return you the value of pi.

Code: Select all

function pi as double
    asm
        fldpi
        fstp qword ptr [function]
    end asm
end function
  
    
print pi
Dim pi2 As Double = 2*pi
Print pi2
Sleep
    
Yes but the function call overhead would kill any speed.

-Vince
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

For CRTs, math.h almost invariably defines it as the 21-digit constant:

#define M_PI 3.14159265358979323846
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Post by jevans4949 »

You could rename my function "setpi" and then

dim pi as double = setpi

However, I just checked whether there were any differences, and with doubles the results of all three formulae are identical. I guess any difference is lost in the rounding from extended precision, which in our case we have not got.
sleeping_sun
Posts: 3
Joined: May 15, 2011 21:51

Post by sleeping_sun »

why dim pi as double?
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Post by jevans4949 »

Single-precision floating point has a precision of about 6 decimal digits. Double precision has about 15 decimal digits. Most people prefer the longer precision for calculation, especially if the variables in question are subject to multiple calculations.

Single precision is appropriate if you are storing results derived from outside sources which are not recorded with such accuracy - e.g., weather temperatures.

A while back, I worked on a database where we stored data in published company accounts, which are usually only reported to this sort of precision. If we tried to reproduce the balance sheets, they rarely balanced.
Post Reply