symbols
-
- Posts: 3
- Joined: May 15, 2011 21:51
symbols
How do u write pi=3.14 on freebasic?
in C it would be "const float PI = 3.141592..."
in FB its almost the same:
in FB its almost the same:
Code: Select all
Const As Double PI = 3.14159265357989
or
Const As Single PI = 3.14159265
I use...
-Vince
Code: Select all
#Ifndef PI
#define PI ATN(1)*4
#Endif
-
- Posts: 3
- Joined: May 15, 2011 21:51
-
- Posts: 2428
- Joined: Jul 19, 2006 19:17
- Location: Sunnyvale, CA
- Contact:
You need additional parenthesis there, otherwise PI^PI won't evaluate correctly. Or you could just make it a const.vdecampo wrote:I use...-VinceCode: Select all
#Ifndef PI #define PI ATN(1)*4 #Endif
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.
I sometimes use the following in FB.
Code: Select all
Const As Double Pi = Atan2(0,-1)
-
- Posts: 2655
- Joined: Aug 28, 2008 10:54
- Location: new york
If you don't need anything manually deallocated, just use:
Code: Select all
if multikey(&h01) then end
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.rolliebollocks wrote:Won't using the ATN in a macro recall that slow trig func for every PI calculation?
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
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.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
-Vince
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
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.
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.
-
- Posts: 3
- Joined: May 15, 2011 21:51
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
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.
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.