Squares

General FreeBASIC programming questions.
Locked
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Squares

Post by dodicat »

Hi Richard.
If you byval your input strings then a switch from original to bin back to original then = original.

Albert, not as fast as Richard's, but I have a new method.
dec to hex then hex to bin.
The dec to hex I have to speed up.
The hex to bin is very fast.
I have yet to make hex to dec.

Code: Select all

Function DecToHex(DecimalNumber As String) As String
    Dim As String starter=DecimalNumber,ans,m,b=String(Len(DecimalNumber),0)
    Dim As Ubyte main,carry,pt,c
    Dim As Long lens
    #macro reverse(s)
    lens=Len(s)
    For n As Integer=0 To Int((lens-1)/2):Swap s[n],s[lens-1-n]:Next
        #endmacro
        #macro d16(s,m,c) ''divide by 16
        carry=0:ans=s
        For z As Long = 0 To Len(s)-1
            pt=carry+s[z]-48
            main=(pt) Shr 4
            carry=( (pt ) And 15)*10
            ans[z]=main+48
        Next z
        c=carry\10
        m=Trim(Ltrim(ans,"0"))
        #endmacro
        Dim As Long idx
        Do
            c=0
            d16(starter,m,c)
            Select Case As Const c
            Case 10:c=Asc("A")
            Case 11:c=Asc("B")
            Case 12:c=Asc("C")
            Case 13:c=Asc("D")
            Case 14:c=Asc("E")
            Case 15:c=Asc("F")
            Case Else:c+=48
            End Select
            b[idx]=c
            starter=m
            idx+=1
        Loop Until m="" Orelse m="1"
        b=Rtrim(b)
        reverse(b)
        b=Str(m)+b
        Return b
    End Function
    
    Function hextobin(Byval hx As String) As String'hex string 
        Dim As String acc
        For n As Long=0 To Len(hx)-1
            Var g=Chr(hx[n])
            acc+=Bin(Vallng("&h"+g),4)
        Next n
        Return Ltrim(acc,"0")	
    End Function
    
    Function bintohex(Byval b As String) As String'bin string
    	Dim As String hx
    	Var z=4-Len(b) Mod 4
        b=String(z,"0")+b 'pad to *4
        For n As Long=1 To Len(b) Step 4
            Var g=Mid(b,n,4)
            hx+=Hex(Vallng("&b"+g))
        Next
        Return Ltrim(hx,"0")
    End Function 
    Print "Short number test"
    
    Dim As String g="12345678987654"
    
    Dim As String hx= DecToHex(g)
    Print hx
    Print Hex(Vallng(g))  
    
    Print hextobin(hx)     
    Print Bin(Vallng(g))  
    Print
    '===================
    Print "Big number test"
    g=String(1000,"9")
    hx=DecToHex(g) 'step 1 get hex
    Print hx
    Print
    Dim As String b=hextobin(hx) 'step 2  get bin from hex
    Print b
    Print
    print "return hex"
    Print bintohex(b)
    
    sleep
       
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

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

Re: Squares

Post by dodicat »

Hi Albert.
The highest power of two I can use is 32.
The crt goes to 36 but that means using mod and div which is bound to slow things.
So this type of reduction to a base will be too slow anyway I suppose.
Here is the base 32 anyway:

Code: Select all



#include "crt.bi"

function ULongToBase(N as ulongint,_base as byte) as string 
    dim as zstring * 50 buffer
    _ultoa(n,buffer,_base)
    return (buffer)
end function

function ULongFromBase(N as string,_base as byte) as ulong
    return strtoul(n,0,_base)
end function

Function DecTo32(DecimalNumber As String) As String
    Dim As String starter=DecimalNumber,ans,m,b=String(Len(DecimalNumber),0)
    Dim As long main,carry,pt,c
    Dim As Long lens
    #macro reverse(s)
    lens=Len(s)
    For n As Integer=0 To Int((lens-1)/2):Swap s[n],s[lens-1-n]:Next
        #endmacro
        #macro d32(s,m,c) ''divide by 32
        carry=0:ans=s
        For z As Long = 0 To Len(s)-1
            pt=carry+s[z]-48
            main=(pt) Shr 5
            carry=( (pt ) And 31 )*10
            ans[z]=main+48
        Next z
        c=carry\10
        m=Trim(Ltrim(ans,"0"))
        #endmacro
        Dim As Long idx
        Do
            c=0
            d32(starter,m,c)
            '======  real 32 bit output ====
            '''c+=65
             '''if c>90 then c-=41
             '===== 32 bit output to suit the crt functions ======
            if c>9 then c+=87 else c+=48
          '===============
            b[idx]=c
            starter=m
            idx+=1
        Loop Until m="" Orelse m="1"
        b=Rtrim(b)
        reverse(b)
        b=Str(m)+b
        Return b
    End Function
    
    Function B32tobin(Byval b32 As String) As String 'base32 (via crt) string 
        Dim As String acc
        For n As Long=0 To Len(b32)-1
            Var g=Chr(b32[n])
            acc+=bin(ULongFromBase(g,32),5)
        Next n
        Return Ltrim(acc,"0")	
    End Function
    
    Function binto32(Byval b As String) As String   'bin string
    	Dim As String b32
    	Var z=5-Len(b) Mod 5
        b=String(z,"0")+b 'pad to *5
        For n As Long=1 To Len(b) Step 5
            var s= Mid(b,n,5)
            b32+=ULongToBase(valulng("&b"+ s),32)
        Next
        Return Ltrim(b32,"0")
    End Function
    
    randomize
    print "Within ulong range"
    dim as string s=str(valulng(str(rnd*1e14)))
      print "number = ";valulng(s)
    var tt= decto32(s)
    print tt
    var b= b32tobin(tt)
    print b
    print bin(val(s))
    print binto32(b)
    print
    print
    'sleep
    Dim As String g=String(10000,0)
     #define range(f,l) Int(Rnd*((l+1)-(f))+(f))
       g[0]=range(49,57)
       for n as long=1 to len(g)-1
           g[n]=range(48,57)
           next
        Dim As Double t,acc
        print "big range"
        For z As Long=1 To 5
            t=Timer
            Var b32= decto32(g)
            print "time for decto32 ";timer-t
           
            t=timer
            b= b32tobin(b32)
            print "time for b32tobin ";timer-t
           
            t=timer
            var newb32= binto32(b)
            print "time for binto32 ";timer-t
            
            print iif(newb32=b32,"OK","ERROR")
            print

        Next z
        Print
        
       
      
        Sleep
        print b
        sleep
    
    
    
   
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Dodicat

I'm working on a program to make a model of an inductive electro-static generator...

I need a disk spinning , a flat disk with metal flat rounds around the circle , on both sides.
on each side of the disk is a metal brush to pick up the voltage from the rounds of metal around the disk.
The brush on each side goes to a floating metal round on the opposite side.
The brush on each side picks up the voltage on one side and induces the voltage to the other side of the disk by the floating round of inducing metal on the opposite side.

The voltage keeps climbing up and up as the disk spins..then you got a spark-gap to arc the voltage to the output when it reaches a certain voltage , like 100,000 - 300,000 volts..

A small motor ( maybe solar powered. ) could power thousands of disks to generate voltage..
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

You may be describing a Wimshurst Machine. https://en.wikipedia.org/wiki/Wimshurst_machine
Albert wrote:A small motor ( maybe solar powered. ) could power thousands of disks to generate voltage..
Power is the rate of flow of energy, which is voltage * current. But the machine produces a voltage only when the current is zero. When current flows the voltage is zero; So no matter what, no energy, no real power.
The total energy generated will be significantly less than produced by the solar panel or the motor that spins it.
There is no such thing as perpetual motion, nor free energy. If there was, the economy would have crashed long ago because energy and money are both exchangeable currency.
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: Squares

Post by dafhi »

Richard wrote:There is no such thing as perpetual motion, nor free energy. If there was, the economy would have crashed long ago because energy and money are both exchangeable currency.
unless the currency is compromised.
also the neural nets of most people are compomised.

creating something from nothing is .. so easy.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Squares

Post by grindstone »

dafhi wrote:creating something from nothing is .. so easy.
So you assume your power of imagination to be "nothing"? I don't know if I should agree... <ponder>
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: Squares

Post by dafhi »

grindstone wrote:So you assume your power of imagination to be "nothing"? I don't know if I should agree... <ponder>
Actually that's not the platform I was referring to, but in fact with what you are talking about, based upon my own thinking and information I just happened to come across, I came up with the following: "The energy that creates worlds and human imagination are one in the same."

My original post was about physics.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

With the disk spinning , the brush on one side and the floating plate on the other side , the brush will pick up voltage and multiply it.
You don't need to start out with a charge..
A charge will build up to thousands of volts from nothing (induction) , then you have a spark-gap set to the voltage you want and it will arc to the output when the voltage reaches that level.

So you cascade many of these spinning disks, so it arcs off every so many times a second.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Post by Richard »

@Albert. Why are you interested in a magicians prop and party toy from over 100 years ago? The Wimshurst Mchine, WM, is a parametric capacitance amplifier powered by rotation of the shaft. It has an energy conversion efficiency of less than 1 part in 1000. The sparks make hot air and the sound of clicks, but no usable energy. The WM is in the same class of toy as Crook's Mill which is actually driven by heating of the inert gas inside the glass bulb.
Scienceworld.wolfram.com gets it right “Wimshurst machines can occasionally be found in science museums, where they are frequently broken as a result of too vigorous winding by museum patrons. Digrams of these devices can be found in physics textbooks prior to the mid-1930s.”
What do you gain by simulating the WM? If the simulation is messed up, the bad model will spread the false idea that the WM provides free energy. If the simulation is “honest” it will just consume all the shaft energy you can provide, and so make your CPU hot.
There is a partial WM simulation here; https://en.wikipedia.org/wiki/Wimshurst ... #Operation

You have been wanting a fast ASCII base ten ↔ two converter.
You now have one.
Now I want to see what you will use it for.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@Richard

Sorry for the passover of your dec/bin converters.. I'm working on speeding them up..
My fastest multiplier does mill digits x mill digits in 22 seconds..
Your bin converter takes 60 seconds to convert mill digits to binary.
So i got to figure out how to make it convert a mill digits in under 10 seconds...( got to speed up the code.)

It might take me a couple months or never , to get you converters sped up to under 10 seconds per mill.


The electro-static inductive capacitance disks are for a post to the U.S.A. DARPA
I wanted to email them a computer program that shows the workings of the cascaded electro-static generator..
Its similar to the Van DeGaff generators, but works by induction , instead of wool on rubber friction.
With my idea the only friction is the bearings of the inductive disks..You can have a multitude of inductive disks on a single spindle..
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Squares

Post by jj2007 »

dodicat wrote:Here is a slightly faster base2

Code: Select all

 Function base2(DecimalNumber As String) As String
Is there still interest in this? Have you tried using the CRT conversions? I've added a test loop below, is that what you want to optimise?

Code: Select all

#include "Windows.bi"
 Function base2(DecimalNumber As String) As String
            Dim As String starter=DecimalNumber,ans,m,b=string(4*len(DecimalNumber),32)
             static as byte a(48 to 57)={0,10,0,10,0,10,0,10,0,10}
            Dim As long c,lens
            #macro reverse(s)
                lens=Len(s)
                For n As Integer=0 To Int((lens-1)/2):Swap s[n],s[lens-1-n]:Next
                #endmacro
                #macro div2(s,m,c)
                c=0:ans=s
                For z As long=0 To Len(s)-1
                     ans[z]=  (c+s[z]) shr 1 +24
                      c=a(s[z])
                Next z
                m= Ltrim(ans,"0")
                #endmacro
                dim as long idx
                Do
                    div2(starter,m,c)
                    b[idx]=c\10+48
                    starter=m
                    idx+=1
                Loop Until m="1" orelse m=""
                b=rtrim(b)
                reverse(b)
                b=Str(m)+b
                Return b
         End Function

	Dim as integer ct, ticks
	Dim as string res
	ticks=GetTickCount()
	For ct=1 To 100000
		res=base2("12345")
	Next
	ticks=GetTickCount()-ticks
	print ticks; " ms for 100k conversions"
	print "sample result: "; res
	sleep()
Results for a Core i5:

Code: Select all

 1787 ms for 100k conversions
sample result: 11000000111001
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@jj2007

your code is too slow...

Code: Select all

#include "Windows.bi"

 Function base2(DecimalNumber As String) As String
            Dim As String starter=DecimalNumber,ans,m,b=string(4*len(DecimalNumber),32)
             static as byte a(48 to 57)={0,10,0,10,0,10,0,10,0,10}
            Dim As long c,lens
            #macro reverse(s)
                lens=Len(s)
                For n As Integer=0 To Int((lens-1)/2):Swap s[n],s[lens-1-n]:Next
                #endmacro
                #macro div2(s,m,c)
                c=0:ans=s
                For z As long=0 To Len(s)-1
                     ans[z]=  (c+s[z]) shr 1 +24
                      c=a(s[z])
                Next z
                m= Ltrim(ans,"0")
                #endmacro
                dim as long idx
                Do
                    div2(starter,m,c)
                    b[idx]=c\10+48
                    starter=m
                    idx+=1
                Loop Until m="1" orelse m=""
                b=rtrim(b)
                reverse(b)
                b=Str(m)+b
                Return b
         End Function

screen 19

   Dim as double time1,time2
   Dim as string res=""
   
   time1 = timer
        For a as longint = 1 To 10000
            res+=str(int(rnd*10))
        Next
    time2 = timer
    
    print time2-time1 , "time to build number."
    
    time1= timer
       dim as string ans = base2(res)
    time2 = timer
   
   print time2 - time1 , "time to conveert to base2"
   
   sleep()
   
   end
   
I set it to 10,000 digits , cause it was too slow to do 100,000 digits..longer than 100 seconds.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Squares

Post by jj2007 »

Code: Select all

        For a as longint = 1 To 10000
            res+=str(int(rnd*10))
        Next
Ok, so it seems you want to measure the speed of the string builder, not the speed of the conversion? Btw that's Dodicat's original code.
albert
Posts: 6000
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Post by albert »

@jj2007

I measured the speed of both the "number creator" and the "conversion"..
The number creator was .02 and the conversion was 1.98 for 10,000 digits.

Richards code was far faster than Dodicat..
( Dodicat uses 1 digit conversion , Richards uses 9 digit conversion. so Richards is 9 times faster than Dodi's)

But Richards code is still to slow to use ,
( I've got to make Richards code do million digit conversion in under 10 seconds.)
Then i can spend 10 seconds on the multiplying and back to base10 conversion..
Locked