VLU Compare (>, = or <)

Compare two VLU arrays for first one being greater than, equal to, or less than the second. The function returns 3 for greater than, 2 for equal to, 1 for less than and 0 for a leading zero error.

To include only the compare function in a DLL directly from this source file put the following lines in the DLL source:

%In_DLL = 1

#INCLUDE "CompareVLUs.bas" 
The #IF will skip the PBMAIN() for you.

Source Code

The function CompareVLUs should compile in PowerBASIC For Windows (PBWin) Versions 8, 9 or 10 and PowerBASIC Console Compiler (PBCC) Versions 4, 5 or 6. The DEC$() function is new in PBWin 10 and PBCC 6, for earlier versions change to STR$() or FORMAT$(). All PBCC versions will ignore $$CRLF and wrap to next line at console width. The earlier versions of PB will not recognize the double $$ which means use 16 bit characters. Just remove one $ to make 8 bit characters.


'File name  Compare_VLUs.bas

'for stand alone testing

#if not %def(%In_DLL)

  #compile exe

  #dim all

  function pbmain () as long

    local TestArrayA(), TestArrayB() as dword

    local R1, R2A, R2B, R3A, R3B, R3C as long ' "R" for result

    'test for leading 0 - - - - - - - - - - - - - - - - - - -

    dim TestArrayA(10)

    dim TestArrayB(10)

    TestArrayA(10) = 1

    TestArrayB(9) = 999 'element 10 is zero.

    R1 = Compare_VLUs(TestArrayA(), TestArrayB())

    'test unequal MSD digit numbers - - - - - - - - - - - - - - -

    redim preserve TestArrayB(11)

    TestArrayB(11) = 1

    R2A = Compare_VLUs(TestArrayA(), TestArrayB())

    redim preserve TestArrayB(9)

    R2B = Compare_VLUs(TestArrayA(), TestArrayB())

    'test equal length arrays - - - - - - - - - - - - - - - - - - - -

    redim TestArrayA(9)

    redim TestArrayB(9)

    '- - with equal value . . . . . . . . . . . . . . .

    TestArrayA(9) = 1

    TestArrayB(9) = 1

    TestArrayA(0) = 1

    TestArrayB(0) = 1

    R3A = Compare_VLUs(TestArrayA(), TestArrayB())

    '- - with less than value . . . . . . . . . . . . .

    TestArrayA(0) = 0

    R3B = Compare_VLUs(TestArrayA(), TestArrayB())

    '- - with greater than value . . . . . . . . . . .

    swap TestArrayA(0), TestArrayB(0)

    R3C = Compare_VLUs(TestArrayA(), TestArrayB())



    ? "Lead 0 test is- " + dec$(R1) + " (should be 0)" + $$crlf + _

      "unequal digit count test is- " + dec$(R2A) + " (should be 1)" + $$crlf + _

      "unequal digit count test is- " + dec$(R2B) + " (should be 3)" + $$crlf + _

      "Equal digit count test-" + $$crlf + _

      "     with equal value is- " + dec$(R3A) + " (should be 2)" + $$crlf + _

      "     with less than value is- " + dec$(R3B) + " (should be 1)" + $$crlf + _

      "     with greater than value is- " + dec$(R3C) + " (should be 3)"

  end function

#endif

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

'Function to be included in DLL.

function Compare_VLUs alias "CompareVLUs" (byref VLU1() as dword, _

                                           byref VLU2() as dword) export as long

  local UB1, UB2, CurDig as long



  UB1 = ubound(VLU1())

  UB2 = ubound(VLU2())

  if VLU1(UB1) = 0 or VLU2(UB2) = 0 then

    goto IsLead0Err

  elseif UB1 > UB2 then

    goto IsGreaterThan

  elseif UB1 < UB2 then

    goto IsLessThan

  else 'the UBs are equal, so check digit by digit

    for CurDig = UB1 to 0 step -1

      if VLU1(CurDig) = VLU2(CurDig) then

        if CurDig = 0 then

          goto IsEqualTo

        else

          iterate for

        end if

      elseif VLU1(CurDig) > VLU2(CurDig) then

        goto IsGreaterThan

      else 'must be less than

        goto IsLessThan

      end if

    next

  end if

  goto IsLead0Err

  IsGreaterThan:

    function = 3

    exit function

  IsEqualTo:

    function = 2

    exit function

  IsLessThan:

    function = 1

    exit function

  IsLead0Err:

    function = 0

end function


Created on 24 December 2021; last edit 24 Dec 2021.