• PicŪ Basic


  • Maximum, Minimum and Square Root Routines

    I would like to share some of work around with everyone especially the MAXimum and MINinimum keywords that were removed from the compiler as a result of upgrades and also square root routines.

    you can copy the codes in part or include as a whole anyway you want. I have it in an include file called maths.inc so i just include the file then I make a call to any function i want.

    Code:
    GoTo Over_Define
    
    $define GetMAxMin(BufferSize, Buffer, MaxVal,MinVal) MaxVal = Buffer[0]'
                                                         MinVal = Buffer[0]'
                                                         For MacIndex = 0 To (BufferSize - 1)'
                                                         If Buffer[MacIndex] > MaxVal Then'
                                                         MaxVal = Buffer[MacIndex]'
                                                         ElseIf Buffer[MacIndex] < MinVal Then'
                                                         MinVal = Buffer[MacIndex]'
                                                         EndIf'
                                                         Next
    
    
    
    
    $define GetMax (BufferSize, Buffer, MaxVal) MaxVal = Buffer[0]'
                                                For MacIndex = 0 To (BufferSize - 1)'
                                                    If Buffer[MacIndex] > MaxVal Then'
                                                       MaxVal = Buffer[MacIndex]'
                                                    EndIf'
                                                Next
    
    
    
    
    $define GetMin (BufferSize, Buffer, MinVal) MinVal = Buffer[0]'
                                                For MacIndex = 0 To (BufferSize - 1)  '
                                                    If Buffer[MacIndex] < MinVal Then'
                                                       MinVal = Buffer[MacIndex]'
                                                    EndIf'
                                                Next
    
    
    
    
    $define SquareRootFunct(Snumber,Sqroot) Square = 1'
                                            Delta = 3  '
                                            While Square <= Snumber'
                                            Square = Square + Delta'
                                            Delta = Delta + 2      '
                                            Wend                    '
                                            Sqroot = (Delta/2 ) - 1
    
    
    
    
    Over_Define:
    Okay, firstly all the variables needs to be declared to specific type you want, have only tried to use Byte and word type variable, I have not used Floating type and Dword in the Square Root function.


    Maximum and Minimum


    Buffersize is the size of your array, which you can declare as a variable or a constant. e.g


    Dim Buffersize as byte = 128 or
    symbol Buffersize = 128


    Buffer is the name of the array. e.g Dim MyArray[Buffersize] as byte


    MaxVal is the maximum value in the array


    MinValue is the minimum value in the array


    Example of calling the function.


    GetMaxMin (Mybuffersize, Mybuffer, Maxvalue, MinValue)


    And for the Square Root Function same procedure, declare your variables appropriately, then call the function




    SquareRootFunct (Mynumber, Myresult)


    Snumber is the number you re finding the square root.


    Sqroot is the result that will be returned


    Hope this Help someone


    Tunde