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



1. BUG:

     There is a test program:

     *PROC testintfunction

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

     RETURN

     * END OF PROC TESTINTFUNCTION.

     We think they will display 255, but:
     ?INT(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.

     Label4d9d4e ::
             push ebp                                                        ;0x004d9d4e :        55
             mov ebp , esp                                                   ;0x004d9d4f :        8bec
             and esp , 0FFFFFFF8h                                            ;0x004d9d51 :        83e4f8
             push ecx                                                        ;0x004d9d54 :        51
             push ebx                                                        ;0x004d9d55 :        53
             push esi                                                        ;0x004d9d56 :        56
             push edi                                                        ;0x004d9d57 :        57
             call Fun420d8c                                                  ;0x004d9d58 :        e82f70f4ff
             mov ebx , eax                                                   ;0x004d9d5d :        8bd8
             push ebx                                                        ;0x004d9d5f :        53
             lea edi ,  dword ptr [ esp + 16 ]                               ;0x004d9d60 :        8d7c2410
             mov esi , ebx                                                   ;0x004d9d64 :        8bf3
             call Fun420dd3                                                  ;0x004d9d66 :        e86870f4ff
             test eax , eax                                                  ;0x004d9d6b :        85c0
             jne Label4d9df4                                                 ;0x004d9d6d :        0f8581000000
             mov  al , byte ptr [ebx]                                        ;0x004d9d73 :        8a03
             cmp al , 049h                                                   ;0x004d9d75 :        3c49
             je Label4d9de8                                                  ;0x004d9d77 :        746f
             cmp al , 04Eh                                                   ;0x004d9d79 :        3c4e
             jne Label591c3a                                                 ;0x004d9d7b :        0f85b97e0b00
             fld qword ptr [ ebx + 16 ]                                      ;0x004d9d81 :        dd4310
             mov  ecx , dword ptr [ ebx + 8 ]                                ;0x004d9d84 :        8b4b08
             fcomp qword ptr [ Data91fbc8 ]                                  ;0x004d9d87 :        dc1dc8fb9100
             lea esi ,  dword ptr [ ebx + 16 ]                               ;0x004d9d8d :        8d7310
             fld qword ptr [esi]                                             ;0x004d9d90 :        dd06
             add ecx , 03h                                                   ;0x004d9d92 :        83c103
             fstsw ax                                                        ;0x004d9d95 :        dfe0
             test  byte ptr ah , 05h                                         ;0x004d9d97 :        f6c405
             jnp Label591c7a                                                 ;0x004d9d9a :        0f8bda7e0b00
             fcomp qword ptr [ Data91fbc8 ]                                  ;0x004d9da0 :        dc1dc8fb9100
             fstsw ax                                                        ;0x004d9da6 :        dfe0
             test  byte ptr ah , 041h                                        ;0x004d9da8 :        f6c441
             jne Label4d9db8                                                 ;0x004d9dab :        750b


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

             cmp dword ptr vfpa_sys9146_data,00h
             jne Label4d9db1
             cmp dword ptr vfpa_sys9125_data,00h
             je Label4d9db1

             cmp ecx,0Fh
             jge Label4d9db1

             sub esp , 08h
             fld qword ptr [esi]

             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 Label4d9daf
             and edx,80000000h
             test edx,edx
             jne Label4d9daf

             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 Label4d9db0
             cmp eax,0Fh
             jge Label4d9db0

             cmp eax,ecx
             jle Label4d9db0

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

             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
             add esp , 08h
             jmp Label4d9db6

     Label4d9dae ::
             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
             add esp , 08h
             jmp Label4d9db6

     Label4d9daf ::
             fstp st(0)
             jmp Label4d9db0

     Label4d9db0 ::
             fstp st(0)
             add esp , 08h
             jmp Label4d9db1


     Label4d9db1 ::
             IFDEF RAX
             push esi
             lea esi ,  dword ptr [ Data922438 ]
             fld qword ptr [ 8 * ecx + esi ]                            ;0x004d9dad :        dd04cd38249200
             pop esi
             ELSE
             fld qword ptr [ 8 * ecx + offset Data922438 ]                            ;0x004d9dad :        dd04cd38249200
             ENDIF
             fadd qword ptr [esi]                                            ;0x004d9db4 :        dc06

     Label4d9db6 ::
             fstp qword ptr [esi]                                            ;0x004d9db6 :        dd1e

     Label4d9db8 ::
             xor eax , eax                                                   ;0x004d9db8 :        33c0
             mov  ax , word ptr [ ebx + 22 ]                                 ;0x004d9dba :        668b4316
             shr eax , 04h                                                   ;0x004d9dbe :        c1e804
             and eax , 07FFh                                                 ;0x004d9dc1 :        25ff070000
             sub eax , 03FFh                                                 ;0x004d9dc6 :        2dff030000
             cmp eax , 01Fh                                                  ;0x004d9dcb :        83f81f
             jnl Label591c86                                                 ;0x004d9dce :        0f8db27e0b00
             fld qword ptr [esi]                                             ;0x004d9dd4 :        dd06
             call Fun4337e6                                                  ;0x004d9dd6 :        e80b9af5ff
             mov  dword ptr [ ebx + 12 ] , eax                               ;0x004d9ddb :        89430c
             mov  byte ptr [ebx] , 049h                                      ;0x004d9dde :        c60349

     Label4d9de1 ::
             mov  dword ptr [ ebx + 8 ] , 00h                                ;0x004d9de1 :        c7430800000000

     Label4d9de8 ::
             mov  eax , dword ptr [ esp + 12 ]                               ;0x004d9de8 :        8b44240c
             test eax , eax                                                  ;0x004d9dec :        85c0
             jne Label591cb1                                                 ;0x004d9dee :        0f85bd7e0b00

     Label4d9df4 ::
             pop edi                                                         ;0x004d9df4 :        5f
             pop esi                                                         ;0x004d9df5 :        5e
             pop ebx                                                         ;0x004d9df6 :        5b
             mov esp , ebp                                                   ;0x004d9df7 :        8be5
             pop ebp                                                         ;0x004d9df9 :        5d
             ret                                                             ;0x004d9dfa :        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_vfp9fix255.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_vfp9fix257.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix258.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix259.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix260.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.