more spontaneous cha0s code

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

more spontaneous cha0s code

Post by cha0s »

Code: Select all

Enum var_id
  
  EVAR_i = 1
  EVAR_ui
  EVAR_b 
  EVAR_ub
  EVAR_sh
  EVAR_ush
  EVAR_l
  EVAR_ul
  EVAR_sn
  EVAR_d
  EVAR_s

End Enum

Type variance

  Union
  
    i As Integer
    ui As uInteger

    b As Byte
    ub As uByte
    
    sh As uShort
    ush As uShort
    
    l As LongInt
    ul As uLongInt
    
    sn As Single
    d As Double
    
    s As String
    
    z As zString Ptr
    
    
  End Union
  
End Type


Type matrix2

  id As Integer
  x As variance
  y As variance
  
  is_ptr As Byte
  
End Type
  
Type dummy_matrix2_handler

  elem As matrix2 Ptr
  elems As uInteger
  
End Type
    
Declare Sub matrix2_InitMatrices Overload ( m As dummy_matrix2_handler, id As Integer = 1, num As Integer = 0 )
Declare Sub matrix2_InitMatrices          ( m As matrix2 Ptr, id As Integer = 1, elements As Integer = 1 )

Declare Sub matrix2_DestroyMatrices Overload ( m As dummy_matrix2_handler )
Declare Sub matrix2_DestroyMatrices          ( m As matrix2 Ptr, elements As Integer = 1 )
Declare Function x_IsPowerOf_y( x As Integer, y As Integer ) As Integer




Dim Shared As Integer i

? "Listing powers of 7 in range 0-478893"

For i = 0 To 478893

  If x_IsPowerOf_y( i, 7 ) Then
    ? i
    
  End If
  
Next

  
  



Sleep



Sub matrix2_DestroyMatrices( m As matrix2 Ptr, elements As Integer = 1 )

  
  If m = 0 Then Exit Sub
  If elements = 0 Then Exit Sub

  Dim As Integer i
  
  For i = 0 To elements - 1
  
    Select Case m[i].id
      
      Case EVAR_s
        m[i].x.s = ""
        m[i].y.s = ""

      Case EVAR_z
        *( m[i].x.z ) = ""
        *( m[i].y.z ) = ""
        
    End Select
    
  Next
  
  If m->is_ptr <> 0 Then 
    Deallocate m
    m = 0
    
  End If
  
End Sub
  
Sub matrix2_DestroyMatrices( m As dummy_matrix2_handler )
  
  matrix2_DestroyMatrices( m.elem, m.elems )
  
End Sub
  
  
  
  
  







Sub matrix2_InitMatrices( m As matrix2 Ptr, id As Integer = 1, elements As Integer = 1 )
  
  
  If elements = 0 Then Exit Sub
  If id = 0 Then Exit Sub
  
  Dim As Integer i
  
  If m = 0 Then
    
    m = CAllocate( elements * Len( matrix2 ) )
    
    
    For i = 0 To elements - 1
      m[i].is_ptr = -1
      
    Next
    
  End If
  
  For i = 0 To elements - 1
    m[i].id = id
    
  Next
  
    
End Sub

Sub matrix2_InitMatrices( m As dummy_matrix2_handler, id As Integer = 1, num As Integer = 0 )

  If num <> 0 Then
    m.elems = num
  
  Else
    If m.elems = 0 Then m.elems += 1
    
  End If
  
  matrix2_InitMatrices( m.elem, id, m.elems )
  
End Sub



Function x_IsPowerOf_y( x As Integer, y As Integer ) As Integer

  Dim As Integer op

  

  Dim As dummy_matrix2_handler verify

  matrix2_InitMatrices( verify, EVAR_d )


    With *verify.elem

      .y.d = Log( x ) / Log( y )
      .x.d = Int( .y.d )

      If y And 1 Then
        op = ( .y.d - .x.d ) < .0000000001
        
      Else
        op = ( .y.d = .x.d ) 
        
      End If



      Function = op
      
    End With
    
  matrix2_DestroyMatrices( verify )


End Function
have fun =)
Last edited by cha0s on Mar 16, 2006 10:09, edited 1 time in total.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

edit: i was testing, and these wouldn't fully work with odd nums, but seem to with even nums with exact equality:

Code: Select all

Function x_IsPowerOf_y( x As Integer, y As Integer ) As Integer

  Dim As Integer op
  Dim As Double d

  
  d = Log( x ) / Log( y )

  If y And 1 Then
    op = ( d - Int( d ) ) < .0000000001
    
  Else
    op = ( d = Int( d ) ) 
    
  End If

  Function = op

End Function
Post Reply