Author Topic: QuickBASIC 4.5 Backslash \ Integer Division  (Read 504 times)

Robert

  • Hero Member
  • *****
  • Posts: 1119
    • View Profile
QuickBASIC 4.5 Backslash \ Integer Division
« on: February 24, 2023, 11:11:49 PM »
While having a look at Peter Baumís formula for converting a Gregorian calendar date to a Julian Day Number

Code: [Select]
IF M < 3 THEN
  M = M + 12
  Y=Y-1
END IF

JD = D + (153 * M - 457) \ 5 + 365 * Y + [Y / 4] - [Y / 100] + [Y / 400] + 1721118.5

I noticed that the BASIC \ backslash integer division operator was used in the calculation.

Having forgotten the details of operation of this operator, I dug out my QuickBASIC 4.5 manual where I found the following

Quote
Integer division retains the integer (whole-number) part of the division and discards the fractional part. Integer division is represented by \ (the backslash)....

Code: [Select]
Operation          Result
PRINT 7 \ 3          2
PRINT 9.6 \ 2.4      5
Quote
Both integer division and the remainder operator round off the numbers to be operated on before the calculation is performed. The number 9.6 becomes 10 and 2.4 becomes 2. Therefore, the result of the second example above is 5, not 4.

Keep this in mind if you are porting some old QuickBASIC code that uses the backslash integer operator.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1850
    • View Profile
Re: QuickBASIC 4.5 Backslash \ Integer Division
« Reply #1 on: February 25, 2023, 08:35:22 AM »
To help round out Robert's tip, the following BCX functions can be used to achieve the QB results:

PRINT INT (7 / 3)                           ' 2
PRINT INT (ROUND(9.6, 0) / ROUND(2.4, 0))   ' 5


This can be further simplified with a user function like this:

PRINT QB_INTDIV (9.6, 2.4)                     ' 5


FUNCTION QB_INTDIV (dividend AS DOUBLE, divisor AS DOUBLE) AS INTEGER
 '********************************************************
 ' Replicates the QBasic integer division operator algorithm
 '********************************************************
  FUNCTION = INT (ROUND(dividend, 0) / ROUND(divisor, 0))
END FUNCTION


« Last Edit: February 26, 2023, 09:13:14 AM by MrBcx »

Robert

  • Hero Member
  • *****
  • Posts: 1119
    • View Profile
Re: QuickBASIC 4.5 Backslash \ Integer Division
« Reply #2 on: February 25, 2023, 07:06:24 PM »
In QuickBASIC, the MOD remainder operator, like the backslash operator, also uses integer division with rounding in calculations.

Keep this in mind if you are porting some old QuickBASIC code that uses the MOD remainder operator.

Code: [Select]
DIM AS DOUBLE Dbl1 = 19.0
DIM AS DOUBLE Dbl2 = 6.7
DIM AS DOUBLE RetDbl

PRINT 
PRINT " MOD Remainder Operator"
PRINT " 19.0 MOD 6.7"

' BCX MOD function
RetDbl = MOD(Dbl1, Dbl2)
PRINT RetDbl, " BCX MOD function "

' BCX IMOD function
DIM Int1%
DIM Int2%
DIM RetInt%
Int1% = INT(Dbl1)
Int2% = INT(Dbl2)
RetInt% = IMOD(Int1%, Int2%)
PRINT RetInt%, " BCX IMOD function "

' QuickBASIC MOD function as Brute Force BCX
DIM QBMOD%
Int1% = INT(ROUND(Dbl1, 0))
Int2% = INT(ROUND(Dbl2, 0))
QBMOD% = IMOD(Int1%, Int2%)
PRINT QBMOD%, " Brute Force BCX QuickBASIC MOD equivalent"

' QuickBASIC MOD function as BCX equivalent
' derived from rpgfan3233 post @
' https://www.tapatalk.com/groups/qbasic/modulo-and-remainder-functions-t31093.html

QBMOD% = Dbl1 - INT(Dbl1 / Dbl2) * Dbl2

PRINT QBMOD%, " rpgfan3233 QuickBASIC MOD equivalent"

Results:
Code: [Select]
MOD Remainder Operator

 5.6 BCX MOD function
 1 BCX IMOD function
 5 Brute Force BCX QuickBASIC MOD equivalent
 5 rpgfan3233 QuickBASIC MOD equivalent

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1850
    • View Profile
Re: QuickBASIC 4.5 Backslash \ Integer Division
« Reply #3 on: February 26, 2023, 08:57:21 AM »
Thanks again Robert.

I appreciated the pithy explanation in your linked post.

Here's your code made into a reusable function for our toolboxes:


FUNCTION QB_MOD(dividend AS DOUBLE, divisor AS DOUBLE) AS INTEGER
    '********************************************************
    ' Replicates the QBasic modulus function algorithm
    '********************************************************
    DIM AS INTEGER idividend, idivisor
    idividend = INT(ROUND(dividend, 0))
    idivisor = INT(ROUND(divisor, 0))
    FUNCTION = IMOD(idividend, idivisor)
END FUNCTION