symbols

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

symbols

Postby sleeping_sun » May 15, 2011 21:55

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

Postby Gonzo » May 15, 2011 22:51

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: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » May 15, 2011 23:29

I use...

Code: Select all

#Ifndef PI
     #define PI     ATN(1)*4
#Endif


-Vince
sleeping_sun
Posts: 3
Joined: May 15, 2011 21:51

Postby sleeping_sun » May 15, 2011 23:51

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

Postby KristopherWindsor » May 16, 2011 2:22

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: 3038
Joined: Jan 15, 2007 20:44
Location: Australia

Postby Richard » May 16, 2011 3:12

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

Postby rolliebollocks » May 16, 2011 23:44

@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

Postby qbworker » May 16, 2011 23:47

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

Postby pestery » May 17, 2011 2:04

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: 1156
Joined: May 08, 2006 21:58
Location: Crewe, England

Postby jevans4949 » May 17, 2011 13:22

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: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » May 17, 2011 14:00

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

Postby MichaelW » May 17, 2011 16:50

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

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

Postby jevans4949 » May 17, 2011 16:57

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

Postby sleeping_sun » May 17, 2011 23:54

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

Postby jevans4949 » May 18, 2011 8:36

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.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 8 guests