Welcome, Guest |
You have to register before you can post on our site.
|
Forum Statistics |
» Members: 483
» Latest member: aplus
» Forum threads: 2,804
» Forum posts: 26,432
Full Statistics
|
|
|
Again Help Chapter 2 |
Posted by: BloodyHash - 05-19-2024, 01:25 PM - Forum: Help Me!
- Replies (3)
|
|
So let me show a demo
HP= 0
PRINT " A MONSTER APPEARD"
PRINT
PRINT (1) GERMAN SUPLEX
INPUT X
IF X = 1 THEN HP = 0
what i want is if people press other then 1 i wanna put "ERROR" and then put back to PRINT (1) GERMAN SUPLEX
|
|
|
a very accurate gamma function |
Posted by: Jack - 05-19-2024, 04:16 AM - Forum: Programs
- Replies (1)
|
|
this implementation is actually very accurate and well behaved, it's from https://www.researchgate.net/publication...properties
it took me some time to figure out how to use the tables because they are presented using the convolution operator which was new for me https://en.wikipedia.org/wiki/Convolution and https://youtu.be/KuXjwB4LzSA?t=346, I first converted the table to a power series and then used Maple to simplify the expression
Code: (Select All)
_Title "gamma function test"
Dim As Long i
For i = 1 To 20
Print i, gamma(i)
Next
Function gamma# (x_in As Double)
Static As Double d(20), f, sum, x, z
x = x_in
d(1) = 0.9999999999999999
d(2) = 0.08333333333338228
d(3) = 0.003472222216158396
d(4) = -0.002681326870868177
d(5) = -0.0002294790668608988
d(6) = 0.0007841331256329749
d(7) = 6.903992060449035E-05
d(8) = -0.0005907032612269776
d(9) = -2.877475047743023E-05
d(10) = 0.0005566293593820988
d(11) = 0.001799738210158344
d(12) = -0.008767670094590723
d(13) = 0.01817828637250317
d(14) = -0.02452583787937907
d(15) = 0.02361068245082701
d(16) = -0.01654210549755366
d(17) = 0.008304315532029655
d(18) = -0.00284326571576103
d(19) = 0.0005961678245858015
d(20) = -5.783378931872318E-05
z = 2.506628274631001 * x ^ (x - .5) * Exp(-x)
f = x * x: f = f * f: f = f * f * x: f = f * f * x 'f = x^19
sum=(d(20)*z+(d(19)*z+(d(18)*z+(d(17)*z+(d(16)*z+(d(15)*z+(d(14)*z _
+(d(13)*z+(d(12)*z+(d(11)*z+(d(10)*z+(d(9)*z+(d(8)*z+(d(7)*z+(d(6)* _
z+(d(5)*z+(d(4)*z+(d(3)*z+(x*z*d(1)+z*d(2))*x)*x)*x)*x)*x)*x)*x)*x) _
*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)/f
gamma = sum
End Function
|
|
|
Loop variables |
Posted by: Kernelpanic - 05-18-2024, 08:13 PM - Forum: GitHub Discussion
- Replies (7)
|
|
I have a question regarding loop variables.
A loop variable should actually only be declared before or direct in use, something like in C (all loop variables are local, related to the loop):
Code: (Select All)
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i;
for (int i = 0; i < 5; i++)
{
printf("\n%2d", i);
}
printf("\n\n");
for (int i = 0; i < 5; i++)
{
printf("\n%2d", i);
}
printf("\n\n%2d", i = 10);
return(0);
}
"i" is now really 10!
In Basic, everything is usually declared at the beginning of the program, and there is rarely something like this:
Code: (Select All)
Option _Explicit
'. . .
Dim As Integer i
For i = 1 To 5
Print i
Next
Print
For i = 1 To 3
Print i
Next
Print
Print i + 2
And the last "i" will be taken. Of course, because it's not really local.
As mentioned, is it possible to do it in Basic like in C, without much effort, or does it not fit into Basic's grammar at all?
|
|
|
Simplify Fractions |
Posted by: SMcNeill - 05-18-2024, 06:46 AM - Forum: Utilities
- No Replies
|
|
As promised with the last example utility:
Code: (Select All)
ReDim PF(0) As Long
ReDim Shared As Long Prime_Factors(1 To 168)
Dim As Long num1, num2
Screen _NewImage(1280, 900, 32)
_Delay .5
_ScreenMove _Middle
Init
Do
Input "Give me the top number of your fraction (from 1 to 1016064. 0 quits) =>"; num1
If num1 <= 0 Or num1 >= 1016064 Then System
Input "Give me the bottom number of your fraction (from 1 to 1016064. 0 quits) =>"; num2
If num2 <= 0 Or num2 >= 1016064 Then System
Simplify num1, num2
Print "Simplied those two values become: "; num1; "/"; num2
Loop
Sub Simplify (num1 As Long, num2 As Long)
ReDim PF(0) As Long
GetPF num1, PF()
For i = 1 To UBound(PF)
If num2 Mod PF(i) = 0 Then
num1 = num1 \ PF(i)
num2 = num2 \ PF(i)
End If
Next
End Sub
Sub GetPF (num, PF() As Long)
ReDim PF(1000) As Long
If num < 1 _Orelse num > 1016064 Then Exit Sub
limit = Int(Sqr(num))
PF(0) = 1
If num > 1 Then
count = 1
Do
factor_on = 0
Do
factor_on = factor_on + 1
temp = Prime_Factors(factor_on)
If IsPrime(num) Then
PF(count) = num
finished = -1
Exit Do
ElseIf num Mod temp = 0 Then
PF(count) = temp
count = count + 1
num = num \ temp
Exit Do
End If
Loop
Loop Until finished
End If
ReDim _Preserve PF(count) As Long
End Sub
Function IsPrime (num)
'to check for any given number less than 1,016,064, we only have to do a maximum of 170 comparisons
'as the max value we ever have to check against is the SQR of our number.
'for example, no value higher than 10 could ever be a factor in 100 and be prime!
'so for numbers less than 1,000,000, all we need do is compare them against factors less than 1,000.
'and there's not that many of them, as you can see below!
If num < 2 _Orelse num > 1016064 Then Exit Function
Static As Long Prime_Array_Init, Factor_Count(1 To 10)
Dim As Long factor_on, i, j, factor
If Prime_Array_Init = 0 Then
Prime_Array_Init = -1
Restore Prime_Factor_Count
For i = 1 To 10: Read Factor_Count(i): Next
End If
IsPrime = -1
For j = 1 To 10 'broken down to give 10 quick exit points so we don't check every value for an answer.
For i = 1 To Factor_Count(j)
factor_on = factor_on + 1
factor = Prime_Factors(factor_on)
If num <= factor Then
Exit Function
Else
If num Mod factor = 0 Then IsPrime = 0: Exit Function
End If
Next
If num < factor * factor Then Exit Function
Next
Exit Function
Prime_Factor_Count: 'each value here represents the number of prime factors in number increments of 100
'for example, there are 25 prime numbers between 1 and 100, 21 primes between 101 and 200, ect...
Data 25,21,16,16,17,14,16,14,15,14
End Function
Sub Init
Restore prime_data:
For i = 1 To 168: Read Prime_Factors(i): Next
Exit Sub
prime_data:
Data 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97: 'factors up to 10,000 (100 ^ 2)
Data 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199: 'up to 44,100 (210 ^ 2)
Data 211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293: 'up to 93,636 (306 ^ 2)
Data 307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397: 'up to 160,000 (400 ^ 2)
Data 401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499: 'up to 252,004 (502 ^ 2)
Data 503,509,521,523,541,547,557,563,569,571,577,587,593,599: 'up to 360,000 (600 ^ 2)
Data 601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691: 'up to 490,000 (700 ^ 2)
Data 701,709,719,727,733,739,743,751,757,761,769,773,787,797: 'up to 652,864 (808 ^ 2)
Data 809,811,821,823,827,829,839,853,857,859,863,877,881,883,887: 'up to 820,836 (906 ^ 2)
Data 907,911,919,929,937,941,947,953,967,971,977,983,991,997: 'up to 1,01,064 (1008 ^ 2)
End Sub
Give this two numbers, such as 12 and 16 (to represent 12/16, without me having to parse to get those values), and it simplifies them down to the smallest representation you can have with those numbers. (3/4, in this case.)
|
|
|
Extended KotD #8: _READFILE$ |
Posted by: SMcNeill - 05-18-2024, 06:17 AM - Forum: Keyword of the Day!
- Replies (4)
|
|
This is another of the latest commands, which came into the language back in version 3.12. Let's take a moment and work out what it does for us, and how much coding it can save for us, easily.
Before this command was added into the language, if we wanted to read a whole file into a string, we'd have to do something similar to the following:
Code: (Select All)
Open "temp.txt" For Binary As #1 'open the file
temp$ = Space$(LOF(1)) ' set a string to the proper size to hold the whole file
Get #1, 1, temp$ ' get the data from the file
Close #1 ' close that file and free that filehandle
So this isn't exactly the longest, or hardest code to write. The main thing one has to watch for here is:
1) Does the file exist? If not, then you probably just created a "temp.txt" of size 0 on your drive. Whoops!
2) Are you trying to open the file with an existing handle? If so, error....
3) Did you set the string to the proper size to get all the data? If not...
4) Did you close the file when finished with it? If not...
All little things that we can code and work our way around with a little simple error checking...
OR....
Code: (Select All)
temp$ = _ReadFile$("temp.txt")
We simply use the _ReadFile$ function to read the file into any given string, and let the function handle existence checks, sizing, opening handles, closing handles, and all that good stuff.
_READFILE$ -- it really is the simplest way to read a file into a string!! And even better.... IT REALLY IS THAT SIMPLE TO USE!!!
|
|
|
Prime Factors |
Posted by: SMcNeill - 05-18-2024, 03:32 AM - Forum: Utilities
- Replies (15)
|
|
Code: (Select All)
ReDim PF(0) As Long
ReDim Shared As Long Prime_Factors(1 To 168)
Screen _NewImage(1280, 900, 32)
_Delay .5
_ScreenMove _Middle
Init
Do
Input "Give me a number from 1 to 1016064 (0 quits) =>"; num
If num <= 0 Or num >= 1016064 Then System
GetPF num, PF()
For i = 0 To UBound(PF)
Print PF(i);
Next
Print
Loop
Sub GetPF (num, PF() As Long)
ReDim PF(1000) As Long
If num < 1 _Orelse num > 1016064 Then Exit Sub
limit = Int(Sqr(num))
PF(0) = 1
If num > 1 Then
count = 1
Do
factor_on = 0
Do
factor_on = factor_on + 1
temp = Prime_Factors(factor_on)
If IsPrime(num) Then
PF(count) = num
finished = -1
Exit Do
ElseIf num Mod temp = 0 Then
PF(count) = temp
count = count + 1
num = num \ temp
Exit Do
End If
Loop
Loop Until finished
End If
ReDim _Preserve PF(count) As Long
End Sub
Function IsPrime (num)
'to check for any given number less than 1,016,064, we only have to do a maximum of 170 comparisons
'as the max value we ever have to check against is the SQR of our number.
'for example, no value higher than 10 could ever be a factor in 100 and be prime!
'so for numbers less than 1,000,000, all we need do is compare them against factors less than 1,000.
'and there's not that many of them, as you can see below!
If num < 2 _Orelse num > 1016064 Then Exit Function
Static As Long Prime_Array_Init, Factor_Count(1 To 10)
Dim As Long factor_on, i, j, factor
If Prime_Array_Init = 0 Then
Prime_Array_Init = -1
Restore Prime_Factor_Count
For i = 1 To 10: Read Factor_Count(i): Next
End If
IsPrime = -1
For j = 1 To 10 'broken down to give 10 quick exit points so we don't check every value for an answer.
For i = 1 To Factor_Count(j)
factor_on = factor_on + 1
factor = Prime_Factors(factor_on)
If num <= factor Then
Exit Function
Else
If num Mod factor = 0 Then IsPrime = 0: Exit Function
End If
Next
If num < factor * factor Then Exit Function
Next
Exit Function
Prime_Factor_Count: 'each value here represents the number of prime factors in number increments of 100
'for example, there are 25 prime numbers between 1 and 100, 21 primes between 101 and 200, ect...
Data 25,21,16,16,17,14,16,14,15,14
End Function
Sub Init
Restore prime_data:
For i = 1 To 168: Read Prime_Factors(i): Next
Exit Sub
prime_data:
Data 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97: 'factors up to 10,000 (100 ^ 2)
Data 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199: 'up to 44,100 (210 ^ 2)
Data 211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293: 'up to 93,636 (306 ^ 2)
Data 307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397: 'up to 160,000 (400 ^ 2)
Data 401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499: 'up to 252,004 (502 ^ 2)
Data 503,509,521,523,541,547,557,563,569,571,577,587,593,599: 'up to 360,000 (600 ^ 2)
Data 601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691: 'up to 490,000 (700 ^ 2)
Data 701,709,719,727,733,739,743,751,757,761,769,773,787,797: 'up to 652,864 (808 ^ 2)
Data 809,811,821,823,827,829,839,853,857,859,863,877,881,883,887: 'up to 820,836 (906 ^ 2)
Data 907,911,919,929,937,941,947,953,967,971,977,983,991,997: 'up to 1,01,064 (1008 ^ 2)
End Sub
Building upon the IsPrime function, we now have a means to break a number down into its prime factors.
For example:
12 = 2 * 2 * 3
124 = 2 * 2 * 31
Next step for what I'm working on and playing around with will be fraction simplification.
Take a value like 12 / 16... that can simplify down to 3 / 4. This should now have me on the route to do that easily.
(Break the nominator (top number) down to its prime factors. Check those values against the denominator (bottom number). If they divide evenly (mod = 0) then reduce both numbers by that factor.)
|
|
|
setting program to display always on top of other windows without having the focus? |
Posted by: madscijr - 05-17-2024, 08:13 PM - Forum: Help Me!
- Replies (20)
|
|
Is there a command or way to set your program to stay on top of other windows (and keep running), even if it doesn't have the focus?
If there is, then I want to make line 31 enable always on top in the programs below, and line 34 disable always on top.
Is this possible? Any info appreciated!
Program #1:
Code: (Select All) Const cProgName = "Program #1": _Title cProgName
Const FALSE = 0: Const TRUE = Not FALSE
' TEXT MODE COLORS:
Const cBlack = 0: Const cBlue = 1: Const cGreen = 2: Const cLtBlue = 3
Const cRed = 4: Const cPurple = 5: Const cOrange = 6: Const cWhite = 7
Const cGray = 8: Const cPeriwinkle = 9: Const cLtGreen = 10: Const cCyan = 11
Const cLtRed = 12: Const cPink = 13: Const cYellow = 14: Const cLtGray = 15
' COUNT UNTIL USER HITS ESC
Screen 12 ' SCREEN 12 can use 16 color attributes with a black background. 256K possible RGB color hues. Background colors can be used with QB64.
Cls , cBlue
Color cWhite, cBlue
Dim iLoop As Integer
Dim bOnTop As Integer: bOnTop = FALSE
Locate 5, 20 ' y,x where 1,1 = top left
Print cProgName
Locate 10, 20
Print "Press A to stay on top, B for background"
Locate 25, 20 ' y,x where 1,1 = top left
Print "Hold down ESC to exit."
For iLoop = 0 To 10000
Locate 15, 20
Print IIFSTR$(bOnTop, "STAY ON TOP", "BACKGROUND ")
Locate 20, 20 ' y,x where 1,1 = top left
Print _Trim$(Str$(iLoop)) + " "
While _DeviceInput(1): Wend ' clear and update the keyboard buffer
If _KeyDown(Asc("a")) Or _KeyDown(Asc("A")) Then
bOnTop = TRUE
'(ENABLE ALWAYS STAY ON TOP)
ElseIf _KeyDown(Asc("b")) Or _KeyDown(Asc("B")) Then
bOnTop = FALSE
'(DISABLE ALWAYS STAY ON TOP)
End If
If _KeyDown(27) Then Exit For ' leave loop when ESC key pressed
_Limit 4 ' keep loop at 4 frames per second
Next iLoop
End
Function IIF (Condition, IfTrue, IfFalse)
If Condition Then IIF = IfTrue Else IIF = IfFalse
End Function
Function IIFSTR$ (Condition, IfTrue$, IfFalse$)
If Condition Then IIFSTR$ = IfTrue$ Else IIFSTR$ = IfFalse$
End Function
Program #2:
Code: (Select All) Const cProgName = "Program #2": _Title cProgName
Const FALSE = 0: Const TRUE = Not FALSE
' TEXT MODE COLORS:
Const cBlack = 0: Const cBlue = 1: Const cGreen = 2: Const cLtBlue = 3
Const cRed = 4: Const cPurple = 5: Const cOrange = 6: Const cWhite = 7
Const cGray = 8: Const cPeriwinkle = 9: Const cLtGreen = 10: Const cCyan = 11
Const cLtRed = 12: Const cPink = 13: Const cYellow = 14: Const cLtGray = 15
' COUNT UNTIL USER HITS ESC
Screen 12 ' SCREEN 12 can use 16 color attributes with a black background. 256K possible RGB color hues. Background colors can be used with QB64.
Cls , cRed
Color cWhite, cRed
Dim iLoop As Integer
Dim bOnTop As Integer: bOnTop = FALSE
Locate 5, 20 ' y,x where 1,1 = top left
Print cProgName
Locate 10, 20
Print "Press A to stay on top, B for background"
Locate 25, 20 ' y,x where 1,1 = top left
Print "Hold down ESC to exit."
For iLoop = 10000 To 0 Step -1
Locate 15, 20
Print IIFSTR$(bOnTop, "STAY ON TOP", "BACKGROUND ")
Locate 20, 20 ' y,x where 1,1 = top left
Print _Trim$(Str$(iLoop)) + " "
While _DeviceInput(1): Wend ' clear and update the keyboard buffer
If _KeyDown(Asc("a")) Or _KeyDown(Asc("A")) Then
bOnTop = TRUE
'(ENABLE ALWAYS STAY ON TOP)
ElseIf _KeyDown(Asc("b")) Or _KeyDown(Asc("B")) Then
bOnTop = FALSE
'(DISABLE ALWAYS STAY ON TOP)
End If
If _KeyDown(27) Then Exit For ' leave loop when ESC key pressed
_Limit 4 ' keep loop at 4 frames per second
Next iLoop
End
Function IIF (Condition, IfTrue, IfFalse)
If Condition Then IIF = IfTrue Else IIF = IfFalse
End Function
Function IIFSTR$ (Condition, IfTrue$, IfFalse$)
If Condition Then IIFSTR$ = IfTrue$ Else IIFSTR$ = IfFalse$
End Function
|
|
|
|