--------------------------------------------------------------------------------------------------
                      [BUG/PRB.] ADJUST THE PRECISION OF THE FLOATING-POINT NUMBERS FOR THE CEILING() FUNCTION
                                January 2024
                 --------------------------------------------------------------------------------------------------
                                    CCB



1. BUG:

     There is a test program:

     *PROC testceilingfunction

     SET DECIMALS TO 2
     ?CEILING(-4294967295/16777216) && Displays -256 if SET("DECIMALS")<=3
     SET DECIMALS TO 10
     ?CEILING(-4294967295/16777216) && Displays -255 if SET("DECIMALS")>=4

     RETURN

     * END OF PROC TESTCEILINGFUNCTION.

     We think they will display -255, but:
     ?CEILING(-4294967295/16777216) && Displays -256 if SET("DECIMALS")<=3


2. CAUSE:

     There are some BUGs in the following code.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Label74aad2 ::
             push ecx                                                        ;0x0074aad2 :        51
             push esi                                                        ;0x0074aad3 :        56
             push edi                                                        ;0x0074aad4 :        57
             call Fun420d8c                                                  ;0x0074aad5 :        e8b262cdff
             mov esi , eax                                                   ;0x0074aada :        8bf0
             push esi                                                        ;0x0074aadc :        56
             lea edi ,  dword ptr [ esp + 12 ]                               ;0x0074aadd :        8d7c240c
             call Fun420dd3                                                  ;0x0074aae1 :        e8ed62cdff
             test eax , eax                                                  ;0x0074aae6 :        85c0
             jne Label74ab9d                                                 ;0x0074aae8 :        0f85af000000
             mov  al , byte ptr [esi]                                        ;0x0074aaee :        8a06
             cmp al , 049h                                                   ;0x0074aaf0 :        3c49
             je Label74ab84                                                  ;0x0074aaf2 :        0f848c000000
             cmp al , 04Eh                                                   ;0x0074aaf8 :        3c4e
             je Label74ab32                                                  ;0x0074aafa :        7436
             cmp al , 059h                                                   ;0x0074aafc :        3c59
             je Label74ab28                                                  ;0x0074aafe :        7428
             test  byte ptr [ Data936f10 ] , 01h                             ;0x0074ab00 :        f605106f930001
             je Label74ab1e                                                  ;0x0074ab07 :        7415
             mov eax ,  dword ptr [ Data936f14 ]                             ;0x0074ab09 :        a1146f9300
             test eax , eax                                                  ;0x0074ab0e :        85c0
             jne Label74ab8b                                                 ;0x0074ab10 :        7579
             mov  dword ptr [ Data936f14 ] , 0385h                           ;0x0074ab12 :        c705146f930085030000
             jmp Label74ab8b                                                 ;0x0074ab1c :        eb6d

     Label74ab1e ::
             mov ecx , 0385h                                                 ;0x0074ab1e :        b985030000
             call Fun544742                                                  ;0x0074ab23 :        e81a9cdfff

     Label74ab28 ::
             lea eax ,  dword ptr [ esi + 24 ]                               ;0x0074ab28 :        8d4618
             call Fun6daac5                                                  ;0x0074ab2b :        e895fff8ff
             jmp Label74ab8b                                                 ;0x0074ab30 :        eb59

     Label74ab32 ::
             fld qword ptr [ esi + 16 ]                                      ;0x0074ab32 :        dd4610
             sub esp , 08h                                                   ;0x0074ab3f :        83ec08


     ;
     ;                 -----------------------------------------------------------------------------------------------------
     ;                      VFP 9.0 FIX - ADJUST THE PRECISION OF THE FLOATING-POINT NUMBERS FOR THE CEILING() FUNCTION
     ;                                  March 2023
     ;                 -----------------------------------------------------------------------------------------------------
     ;                                     CCB
     ;
     ; Sometimes the CEILING() function returns wrong result, for example,
     ; SET DECIMALS TO 2
     ; ?CEILING(-4294967295/16777216) && Displays -256 if SET("DECIMALS")<=3
     ; SET DECIMALS TO 10
     ; ?CEILING(-4294967295/16777216) && Displays -255 if SET("DECIMALS")>=4
     ;
     ; 2023/3/21, by ccb
     ;

             cmp dword ptr vfpa_sys9146_data,00h
             jne Label74ab38
             cmp dword ptr vfpa_sys9127_data,00h
             je Label74ab38

             cmp dword ptr [ esi + 8 ],0Fh
             jge Label74ab38

             fld st(0)
             fabs
             fldlg2
             fxch st(1)
             fyl2x

             fld st(0)
             fistp dword ptr [ esp ]
             mov edx,dword ptr [ esp ]
             cmp edx,80000000h
             je Label74ab37
             and edx,80000000h
             test edx,edx
             jne Label74ab37

             mov eax,0
             IFDEF RAX
             push esi
             lea esi ,  dword ptr [ Data922438 ]
             fadd qword ptr [ 8 * eax + esi ]
             pop esi
             ELSE
             fadd qword ptr [ 8 * eax + offset Data922438 ]
             ENDIF

             mov eax,0Ah
             IFDEF RAX
             push esi
             lea esi ,  dword ptr [ Data922438 ]
             fadd qword ptr [ 8 * eax + esi ]
             pop esi
             ELSE
             fadd qword ptr [ 8 * eax + offset Data922438 ]
             ENDIF

             fistp dword ptr [ esp ]
             mov eax,0Fh
             sub eax,dword ptr [ esp ]
             cmp eax,0
             jle Label74ab38
             cmp eax,0Fh
             jge Label74ab38

             cmp eax,dword ptr [ esi + 8 ]
             jle Label74ab38

             fld st(0)
             fistp dword ptr [ esp ]
             mov edx,dword ptr [ esp ]
             cmp edx,80000000h
             je Label74ab38
             and edx,80000000h
             test edx,edx
             jne Label74ab36

             IFDEF RAX
             push esi
             lea esi ,  dword ptr [ Data922438 ]
             fsub qword ptr [ 8 * eax + esi ]
             pop esi
             ELSE
             fsub qword ptr [ 8 * eax + offset Data922438 ]
             ENDIF
             jmp Label74ab42

     Label74ab36 ::
             IFDEF RAX
             push esi
             lea esi ,  dword ptr [ Data922438 ]
             fsub qword ptr [ 8 * eax + esi ]
             pop esi
             ELSE
             fsub qword ptr [ 8 * eax + offset Data922438 ]
             ENDIF
             jmp Label74ab42

     Label74ab37 ::
             fstp st(0)
             jmp Label74ab38


     Label74ab38 ::
             mov  eax , dword ptr [ esi + 8 ]                                ;0x0074ab35 :        8b4608
             IFDEF RAX
             push esi
             lea esi ,  dword ptr [ Data922450 ]
             fsub qword ptr [ 8 * eax + esi ]                           ;0x0074ab38 :        dc24c550249200
             pop esi
             ELSE
             fsub qword ptr [ 8 * eax + offset Data922450 ]                           ;0x0074ab38 :        dc24c550249200
             ENDIF

     Label74ab42 ::
             fst qword ptr [ esi + 16 ]                                      ;0x0074ab42 :        dd5610
             fstp qword ptr [ esp ]                                          ;0x0074ab45 :        dd1c24
             call  ceil                                        ;0x0074ab48 :        ff15e8799100
             xor ecx , ecx                                                   ;0x0074ab4e :        33c9
             fst qword ptr [ esi + 16 ]                                      ;0x0074ab50 :        dd5610
             mov  cx , word ptr [ esi + 22 ]                                 ;0x0074ab53 :        668b4e16
             add esp , 08h                                                   ;0x0074ab57 :        83c408
             shr ecx , 04h                                                   ;0x0074ab5a :        c1e904
             and ecx , 07FFh                                                 ;0x0074ab5d :        81e1ff070000
             sub ecx , 03FFh                                                 ;0x0074ab63 :        81e9ff030000
             cmp ecx , 01Fh                                                  ;0x0074ab69 :        83f91f
             jnl Label74ab7b                                                 ;0x0074ab6c :        7d0d
             call Fun4337e6                                                  ;0x0074ab6e :        e8738cceff
             mov  dword ptr [ esi + 12 ] , eax                               ;0x0074ab73 :        89460c
             mov  byte ptr [esi] , 049h                                      ;0x0074ab76 :        c60649
             jmp Label74ab7d                                                 ;0x0074ab79 :        eb02

     Label74ab7b ::
             fstp st(0)                                                      ;0x0074ab7b :        ddd8

     Label74ab7d ::
             mov  dword ptr [ esi + 8 ] , 00h                                ;0x0074ab7d :        c7460800000000

     Label74ab84 ::
             mov  dword ptr [ esi + 4 ] , 014h                               ;0x0074ab84 :        c7460414000000

     Label74ab8b ::
             mov  eax , dword ptr [ esp + 8 ]                                ;0x0074ab8b :        8b442408
             test eax , eax                                                  ;0x0074ab8f :        85c0
             je Label74ab9d                                                  ;0x0074ab91 :        740a
             mov  byte ptr [esi] , 030h                                      ;0x0074ab93 :        c60630
             mov  dword ptr [ esi + 12 ] , 049h                              ;0x0074ab96 :        c7460c49000000

     Label74ab9d ::
             pop edi                                                         ;0x0074ab9d :        5f
             pop esi                                                         ;0x0074ab9e :        5e
             pop ecx                                                         ;0x0074ab9f :        59
             ret                                                             ;0x0074aba0 :        c3


4. APPLIES TO:

     VFP 6.0.8167.0
     VFP 6.0.8961.0 (SP5)

     VFP 7.0.0.9262
     VFP 7.0.0.9465 (SP1)

     VFP 8.0.0.2521
     VFP 8.0.0.3117 (SP1)

     VFP 9.0.0.2412
     VFP 9.0.0.3504 (SP1)
     VFP 9.0.0.4611 (SP2)
     VFP 9.0.0.5015 (SP2)
     VFP 9.0.0.5411 (SP2)
     VFP 9.0.0.5721 (SP2)
     VFP 9.0.0.5815 (SP2)
     VFP 9.0.0.6303 (SP2)
     VFP 9.0.0.6602 (SP2)
     VFP 9.0.0.7423 (SP2)

     The bug has been fixed in VFP Advanced.


5. REFERENCE WEBSITES:

     1, baiyujia.com:
     http://www.baiyujia.com
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix259.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix23.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix24.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix97.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix98.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix123.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix124.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix255.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix256.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix257.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix258.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix303.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix304.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix305.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix306.asp

     2, foxite.com:
     https://www.foxite.com/archives/binary-to-hex-0000494011.htm


6. OTHER:

     For reference only, there is no guarantees.

     Any questions or suggestions, please send me an email at ccb2000@163.com.