' Example program to show you
'  RapidQ's Ceil, Round, and Floor functions
'  are flawed. hence the reason I created
'  the CRF function to solve these flaws.
' The CRF function is located in the Math.inc file.
'   You can port the coding in mnuTestA_OnClick
'   subroutine to suit your needs.
' CRF solves 3 problems...
'   1) It's more accurate than RapidQ.
'   2) You can specify how many decimal places
'      the function should perform to.
'   3) It can handle numbers larger than what RapidQ can.

$Option EXPLICIT
$Include "RapidQ.inc"
$Include "Math.inc"

Const GWL_HWNDPARENT = (-8)
Const HWND_DESKTOP   = 0

Declare Function SetWindowLong _
        Lib "user32" Alias "SetWindowLongA" _
        (ByVal hWnd As Long, ByVal nIndex As Long, _
         ByVal dwNewLong As Long) As Long

Declare Sub MainForm_OnShow
Declare Sub MainForm_OnClose

Declare Sub mnuTestA_OnClick
Declare Sub mnuTestB_OnClick

Create MainForm As QForm
  Caption = " CRF vs RapidQ"
  Width   = 640
  Height  = 480
  Left    = (Screen.Width\2)-(MainForm.Width\2)
  Top     = (Screen.Height\2)-(MainForm.Height\2)
  OnShow  = MainForm_OnShow
  OnClose = MainForm_OnClose
  Create MainMenu As QMainMenu
    Create mnuTestA As QMenuItem
      Caption = " Test CRF "
      OnClick = mnuTestA_OnClick
    End Create
    Create mnuTestB As QMenuItem
      Caption = " Test RapidQ "
      OnClick = mnuTestB_OnClick
    End Create
    Create mnuExit As QMenuItem
      Caption = " Exit "
      OnClick = MainForm_OnClose
    End Create
  End Create
  Create Panel1 As QPanel
    Width   = (MainForm.ClientWidth\2)
    Height  = MainForm.ClientHeight
    Left    = 0
    Top     = 0
    Create RichEdit1 As QRichEdit
      Align         = alClient
      ScrollBars    = ssBoth
      PlainText     = True
      Font.Name     = "Courier New"
      Font.Size     = 10
      Font.Bold     = False
      WordWrap      = False
      HideSelection = False
    End Create
  End Create
  Create Panel2 As QPanel
    Width   = (MainForm.ClientWidth\2)
    Height  = MainForm.ClientHeight
    Left    = (MainForm.ClientWidth\2)
    Top     = 0
    Create RichEdit2 As QRichEdit
      Align         = alClient
      ScrollBars    = ssBoth
      PlainText     = True
      Font.Name     = "Courier New"
      Font.Size     = 10
      Font.Bold     = False
      WordWrap      = False
      HideSelection = False
    End Create
  End Create
End Create

SetWindowLong(MainForm.Handle, GWL_HWNDPARENT, HWND_DESKTOP)
SetWindowLong(Application.Handle, GWL_HWNDPARENT, MainForm.Handle)

MainForm.ShowModal

Sub MainForm_OnShow
End Sub

Sub MainForm_OnClose
    Application.Terminate
End Sub

' The coding in the following 2 subroutines are virtually
'  identical (just so you know there's no tricks (or treat))
'  d stands for double, p for positive, n for negative
'  c for ceil, r for round, and f for floor
'  bCeil, bRound, bFloor, iPrec are used for clarity

Sub mnuTestA_OnClick
    DefByte bFloor = 0, bRound = 1, bCeil = 2
    DefInt i = 0, iPrec = 2
    DefDbl dp = 2.0001, dn = -2.0001
    DefDbl dpf = 0, dpr = 0, dpc = 0, dnf = 0, dnr = 0, dnc = 0
    Mainform.Caption = " Please be patient. This takes a minute to complete."
    mnuTestA.Enabled = False
    mnuTestB.Enabled = False
    RichEdit1.Clear
    RichEdit1.AddString("CRF Precision : "+Str$(iPrec)+" (adjustable)")
    RichEdit1.AddString("---------------------")
    For i = 1 to 1001
      dpf = CRF(dp,iPrec,bFloor) : dpr = CRF(dp,iPrec,bRound) : dpc = CRF(dp,iPrec,bCeil)
      dnf = CRF(dn,iPrec,bFloor) : dnr = CRF(dn,iPrec,bRound) : dnc = CRF(dn,iPrec,bCeil)
      DoEvents
      RichEdit1.AddString("Floor("+Dbl2Str(dp,iPrec+1,True)+") "+Dbl2Str(dpf,iPrec+3,True))
      RichEdit1.AddString("Round("+Dbl2Str(dp,iPrec+1,True)+") "+Dbl2Str(dpr,iPrec+3,True))
      RichEdit1.AddString(" Ceil("+Dbl2Str(dp,iPrec+1,True)+") "+Dbl2Str(dpc,iPrec+3,True))
      RichEdit1.AddString("Floor("+Dbl2Str(dn,iPrec+1,True)+") "+Dbl2Str(dnf,iPrec+3,True))
      RichEdit1.AddString("Round("+Dbl2Str(dn,iPrec+1,True)+") "+Dbl2Str(dnr,iPrec+3,True))
      RichEdit1.AddString(" Ceil("+Dbl2Str(dn,iPrec+1,True)+") "+Dbl2Str(dnc,iPrec+3,True))
      RichEdit1.AddString(" ")
      dp = dp - .0010
      dn = dn + .0010
    Next i
    mnuTestB.Enabled = True
    mnuTestA.Enabled = True
    MainForm.Caption = " CRF vs RapidQ"
    RichEdit1.SelStart = 0
    dnc = 0 : dnr = 0 : dnf = 0 : dpc = 0 : dpr = 0 : dpf = 0
    dn = 0 : dp = 0
    iPrec = 0 : i = 0
    bCeil = 0 : bRound = 0 : bFloor = 0
End Sub

Sub mnuTestB_OnClick
    DefInt i = 0, iPrec = 2
    DefDbl dp = 2.0001, dn = -2.0001
    DefDbl dpf = 0, dpr = 0, dpc = 0, dnf = 0, dnr = 0, dnc = 0
    Mainform.Caption = " Please be patient. This takes a minute to complete."
    mnuTestA.Enabled = False
    mnuTestB.Enabled = False
    RichEdit2.Clear
    RichEdit2.AddString("RQ Precision : 0 (not adjustable)")
    RichEdit2.AddString("---------------------")
    For i = 1 to 1001
      dpf = Floor(dp) : dpr = Round(dp) : dpc = Ceil(dp)
      dnf = Floor(dn) : dnr = Round(dn) : dnc = Ceil(dn)
      DoEvents
      RichEdit2.AddString("Floor("+Dbl2Str(dp,iPrec+1,True)+") "+Dbl2Str(dpf,iPrec+3,True))
      RichEdit2.AddString("Round("+Dbl2Str(dp,iPrec+1,True)+") "+Dbl2Str(dpr,iPrec+3,True))
      RichEdit2.AddString(" Ceil("+Dbl2Str(dp,iPrec+1,True)+") "+Dbl2Str(dpc,iPrec+3,True))
      RichEdit2.AddString("Floor("+Dbl2Str(dn,iPrec+1,True)+") "+Dbl2Str(dnf,iPrec+3,True))
      RichEdit2.AddString("Round("+Dbl2Str(dn,iPrec+1,True)+") "+Dbl2Str(dnr,iPrec+3,True))
      RichEdit2.AddString(" Ceil("+Dbl2Str(dn,iPrec+1,True)+") "+Dbl2Str(dnc,iPrec+3,True))
      RichEdit2.AddString(" ")
      dp = dp - .0010
      dn = dn + .0010
    Next i
    mnuTestB.Enabled = True
    mnuTestA.Enabled = True
    MainForm.Caption = " CRF vs RapidQ"
    RichEdit2.SelStart = 0
    dnc = 0 : dnr = 0 : dnf = 0 : dpc = 0 : dpr = 0 : dpf = 0
    dn = 0 : dp = 0
    iPrec = 0 : i = 0
End Sub

