Squares

General FreeBASIC programming questions.
Locked
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

Build an output string first using normal string functions and maybe the vb Format function. Then prefix that string with echo and Shell it out.

To print you should first work out how to print on your particular operating system, then use Shell to issue that command to the system.

You might consider writing to a text file first, then close and print that entire file to the console or a printer.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Hi Albert.
I am typing from my Linux box.
Have a good Christmas (Richard also)
I don't have Geany here so everything non graphic is on the terminal straight away.
I compile and run via the terminal and I can use gedit as an editor (Although gedit only shows C syntax for .c files).
Anyway, I'll let you mess with shell, but the crt functions write to the console while print of course writes to any graphics screen.
Same for Windows.

Code: Select all

  
#include "crt.bi"
screen 19
print "Hello from grahics screen"

puts ("Hello from console ")

printf(!"%d\n",234 xor 423)

sleep
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

printf() is faster than puts()..

How do you get a (new line) with printf ??? it prints it all together one after the next...

I need ( new line ) and comma formatting...

Code: Select all

 
#include "crt.bi"
screen 19
print "Hello from grahics screen"

puts ("Hello from console ")

printf(!"%d\n",234 xor 423)

dim as ulongint n = int(rnd*1e8 )

puts ( "n = " + str(n) )
 
for a as longint = 1 to 1000
	printf( "n = " + str(n) )  ' How do you get a new line?? 
next


sleep
end

albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I fumbled around like normal and discovered the answer..

Code: Select all

 
#include "crt.bi"

screen 19

print "Hello from grahics screen"

puts ("Hello from console ")

printf(!"%d\n",234 xor 423)

dim as ulongint n = int(rnd*1e8 )

puts ( "n = " + str(n) )
 
for a as longint = 1 to 1000
	printf( "n = " + str(a) + !"%\n") '  ' How do you get a new line??  (  "string" + ! "%\n" ) works to get a newline...
next

printf ( "Hello" + !"%\n" )
printf ("A string"+ !"%\n")

sleep
end

dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

You need the escape character ! and \n (for new line)
If you Google printf, you will see how to use it for C
fb also uses it the same way, but you must remember the escape character !

Code: Select all

  
#include "crt.bi"
screen 19
print "Hello from grahics screen"

puts ("Hello from console ")

printf(!"%d\n",234 xor 423)

dim as ulongint n = int(rnd*1e8 )

puts ( "n = " + str(n) )
 
for a as longint = 1 to 1000
   printf( !"%s\n  n = " , str(a) )  ' How do you get a new line??
next


sleep
end

dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Hi Albert.
I have a Linux question.
If I make a folder called bin in my home folder then any executables in it are on the system path.
So if I put a graphics executable inside it (graph1) say, then I can open a terminal anywhere, type graph1 and it will run.
But with a console (terminal) only file in the bin folder nothing happens.
Also clicking on a graphics executable (like in windows) will run it.
But clicking a non graphics executable does nothing.
How can you make a terminal fb program clickable?
How can you run a terminal fb program from another location, that is if I put it in the bin folder how can I run it from the desktop?
(I haven't done much fb work in Linux)
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Squares

Post by badidea »

dodicat wrote:...
How can you make a terminal fb program clickable?
...
Same question here: How to force opening a window terminal from program
No satisfying answer yet however. The problem probably is that there is not 1 terminal, but possibly multiple terminal-applications. FBC does not know which one to start. The program BTW does start in the background (and probably remains there until reboot or manual 'kill').

I, in this case, just open a terminal first (at the location of the program), and type ./program. Often, in the file manager, you can say open in terminal (here). Which reduces the use of 'cd location'.
Last edited by badidea on Dec 24, 2018 19:38, edited 1 time in total.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat
@Richard

Here's my XOR function...

Code: Select all


'====================================================

'Code designed for FreeBASIC compiler  ,  FreeBASIC.net
'Geany IDE with build command set to  (FreeBASIC path )  fbc.exe   -gen GCC -w all -O 3

' Richard From Australia Wrote the XOR function...
' Richard can be found on the FreeBASIC forum under (general) then under (squares)
'====================================================

#include "crt.bi"

screen 0

dim as double time1 , time2 , time3 , time4 

dim as double builtin_low = 1
dim as double Richard_low = 1

do
    
    dim as longint a = int( rnd*1e10 )
    dim as longint b = int( rnd*1e10 )
    
    time1 = timer
        dim as longint xors
        dim as longint ands
        for x as longint = 1 to 1e7 
            xors = a xor b
            ands = a and b
        next
    time2 =timer
    if time2-time1 < builtin_low then builtin_low = time2-time1
    
    time3 = timer
        dim as longint Richard_xor
        dim as longint Richard_and
        for x as longint = 1 to 1e7
            Richard_xor =      (a + b) - ( (a And b) shl 1 )
            Richard_and =  ( (a + b) -   (a xor b ) ) \ 2 
        next
    time4 = timer
    if time4-time3 < Richard_low then Richard_low = time4-time3
    
    
    dim as longint xor_diff = xors - Richard_xor 
    dim as longint and_diff = ands - Richard_and 
    
    printf ( !"%\n" )
    printf ( "n1 = " + str(a) + "     n2 = " + str(b) + !"%\n" )
    printf ( "built in XOR  = "  +  str(xors)           + !"%\n" )
    printf ( "Richard  XOR  = " + str(Richard_xor) + !"%\n" )
    printf (  !"%\n" )
    printf ( "builtin AND  = "    + str(ands) +  !"%\n" )
    printf ( "Richard AND  = " + str(Richard_and) +  !"%\n" )
    printf (  !"%\n" )
    printf ( "built in = "     + str((time2-time1)/1e7) + " Low = " + str(builtin_low   / 1e7) + !"%\n" ) 
    printf ( "Richard  = " + str((time4-time3)/1e7) + " Low = " + str(Richard_low / 1e7) + !"%\n" )
    
    if xor_diff  <> 0 then printf ( "ERROR" +  !"%\n" ) : sleep
    if and_diff  <> 0 then printf ( "ERROR" +  !"%\n" ) : sleep
    
    if inkey = " " then printf ( "press (esc) to exit..." + !"%\n" ) : sleep
    
loop until inkey = chr(27)

end

Heres the timings of my n XOR 2^??

Timings:

Code: Select all


XOR comparison of 500 sets of  1e7 loops

Builtin XOR avg = 2.599961590021848e-013   H = 4.699977580457926e-013   L = 4.999455995857716e-014
Albert XOR avg  = 2.799963112920523e-013   H = 5.199981387704611e-013   L = 3.999448381364345e-014                                                                                                                                              

Builtin XOR avg = 4.599976819008589e-013   H = 8.700008038431406e-013   L = 4.999455995857716e-014
Albert XOR avg  = 2.349959686398506e-013   H = 4.299974534660578e-013   L = 3.999448381364345e-014

==================================================================================
@Richard
' c = a Xor b
c = (a + b) - (a And b) shl 1

Your XOR is a cheat it uses logic (AND) ...I'm trying to rite the logic functions without using logic...
I'll work on a faster AND function...Then i can use that in your formula...
=================================================================================
Last edited by albert on Dec 25, 2018 1:43, edited 1 time in total.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

For the *.exe running , you have to open the folder , and click on the *.exe....or icon it to our desktop..

You can right click on a file , and select the program you want to open it with..

For *.bas files , you right click on the *.bas file and it will bring up a menu of programs, you search for the one you want and select it...
It will then set all *.bas file to open with the chosen program...

I'm not an expert on Linux..
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Squares

Post by srvaldez »

dodicat, I also am new to linux, but I have pickded up some basics, if you want the folder where you compile your FB programs to be on the path then you can add the the path of that folder to .bash_profile
launch the terminal then gedit .bash_profile, then on the line:
PATH=$PATH:$HOME/.local/bin:$HOME/bin
add a colon+the-path-to-your-folder and save, quit terminal.
restart
thereafter whenever you launch the terminal the executable files in that folder will be available
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Thanks all.
srvaldez
Instead of adding a folder to PATH=
If you right click inside your home folder, choose create folder, and call the new folder bin, you have all executables you may put into this bin folder on path.
This is where I keep my compiled fb programs.
I see that It works with both terminal and graphics programs.
For instance I can run any of them from a desktop terminal if I type the name.
My quest is to make compiled fb console (terminal) programs start on a click.(badidea also mentioned this).
I think some shell or system command is needed in the code to start a terminal and use it for output by the program via the mouse click.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Richard Logic Functions

Post by albert »

@Richard

I played around with your (xor) formula and figured that (xor) can be solved with (and) ,,, so maybe (and) can be solved with (xor)

It worked!! so i put your name to it...to give you credit....

Code: Select all


'====================================================

'Code designed for FreeBASIC compiler  ,  FreeBASIC.net
'Geany IDE with build command set to  (FreeBASIC path )  fbc.exe   -gen GCC -w all -O 3

' Richard From Australia Wrote the XOR function...
' Richard can be found on the FreeBASIC forum under (general) then under (squares)
'====================================================

#include "crt.bi"

screen 0

dim as double time1 , time2 , time3 , time4 

dim as double builtin_low = 1
dim as double Richard_low = 1

do
    
    dim as longint a = int( rnd*1e10 )
    dim as longint b = int( rnd*1e10 )
    
    time1 = timer
        dim as longint xors
        dim as longint ands
        for x as longint = 1 to 1e7 
            xors = a xor b
            ands = a and b
        next
    time2 =timer
    if time2-time1 < builtin_low then builtin_low = time2-time1
    
    time3 = timer
        dim as longint Richard_xor
        dim as longint Richard_and
        for x as longint = 1 to 1e7
            Richard_xor =      (a + b) - ( (a And b) shl 1 )
            Richard_and =  ( (a + b) -   (a xor b ) ) \ 2 
        next
    time4 = timer
    if time4-time3 < Richard_low then Richard_low = time4-time3
    
    
    dim as longint xor_diff = xors - Richard_xor 
    dim as longint and_diff = ands - Richard_and 
    
    printf ( !"%\n" )
    printf ( "n1 = " + str(a) + "     n2 = " + str(b) + !"%\n" )
    printf ( "built in XOR  = "  +  str(xors)           + !"%\n" )
    printf ( "Richard  XOR  = " + str(Richard_xor) + !"%\n" )
    printf (  !"%\n" )
    printf ( "builtin AND  = "    + str(ands) +  !"%\n" )
    printf ( "Richard AND  = " + str(Richard_and) +  !"%\n" )
    printf (  !"%\n" )
    printf ( "built in = "     + str((time2-time1)/1e7) + " Low = " + str(builtin_low   / 1e7) + !"%\n" ) 
    printf ( "Richard  = " + str((time4-time3)/1e7) + " Low = " + str(Richard_low / 1e7) + !"%\n" )
    
    if xor_diff  <> 0 then printf ( "ERROR" +  !"%\n" ) : sleep
    if and_diff  <> 0 then printf ( "ERROR" +  !"%\n" ) : sleep
    
    if inkey = " " then printf ( "press (esc) to exit..." + !"%\n" ) : sleep
    
loop until inkey = chr(27)

end

Here's a sample time output...

Code: Select all


n1 = 9179292635     n2 = 1769888733
built in XOR  = 9837656070
Richard  XOR  = 9837656070

builtin AND  = 555762649
Richard AND  = 555762649

built in = 2.400018274784088e-013 Low = 5.00003807246685e-014
Richard  = 1.100008375942707e-013 Low = 3.999448381364345e-014

@Richard
I don't know how your code is faster , since you use a logic function in each???? Makes me wonder???
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

Merry Christmas to all the squares.

@Albert.
When an addition is performed, the XOR produces the sum of the bits while AND is used to generate the carry. That is why (a + b) - ( a and b ) shl 1 removes the carry and so leaves only the XOR function. It is not my formula, I have seen it around since the 1960's.

The logical COMPlement function is fundamental to digital computers. The AND and OR functions come next.
From COMP, AND and OR you can make the XOR function and do comparisons.
Once you have XOR, with the AND, you can do ADDition and SUBtraction of binary numbers.
Shifting and adding then make it possible to multiply.
Shifting and subtracting make DIVision and REMainder possible, which are the \ and MOD functions in FB.
I cannot see why you are using the slowest possible processes, DIV and MOD, that are fundamentally based on XOR to perform XOR.

Maybe you should turn face and try to write an addition that uses only the bitwise logical functions.
Sum = a XOR b
Carry = ( a AND b ) Shl 1
Total = Sum + Carry
But how can you propagate carry without using the arithmetic function + ?
I will let you experiment with the loop needed for that.

It was 34°C yesterday, but cooler today, so I'm going down to the beach for a swim before Christmas dinner.

Code: Select all

Randomize
Dim As Ushort a = Rnd * 1024, b = Rnd * 1024, c, d, e
Dim As Integer sum, carry, total
sum = a XOR b
carry = ( a And b ) Shl 1
total = sum + carry
Print total, a + b
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

A sink or swim addition to Albert's chest.

Code: Select all

 Randomize

function add(a as integer,b as integer) as integer
  if a then add= add((a and b) shl 1,a xor b) else add= b
end function

Dim As Ushort a = Rnd * 1024, b = Rnd * 1024, c, d, e
Dim As Integer sum, carry, total
sum = a XOR b
carry = ( a And b ) Shl 1
total = add(sum,carry)
Print total, a + b,sum + carry

sleep
 
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Richard

I'm trying to write all the logic functions... you can't use logic in them unless you create a logic function.

I created a mod function.. So you can create logic functions with + - / \ * and mod ( since i created a mod function, you can use it.)

I'm trying to write:
OR
AND
XOR

and all the rest... Nand , nor , xnor ,xnand , etc...
Locked