---------------------------------------------------------------------------------------------------------------
                      [BUG/PRB.] VFP 9.0 FIX - ADJUST THE PRECISION OF THE FLOATING-POINT NUMBERS FOR THE DIVISION OPERATOR
                                 January 2025
                 ---------------------------------------------------------------------------------------------------------------
                                     CCB



1. BUG:

     There is a test program:

     *PROC testdivisionoperator

     ?STR(4/1000000,16,12) && Displays 0.000004000000
     ?STR(4/10000000,16,12) && Displays 0.000000000000
     WAIT

     RETURN

     * END OF PROC TESTDIVISIONOPERATOR.

     We think STR(4/10000000,16,12) will display 0.000000400000, but:
     ?STR(4/10000000,16,12) && Displays 0.000000000000


2. CAUSE:

     When VFP 9.0 and earlier versions calculate 4/10000000,
     it will set the precision of the result float number:

     n=3, eps = 0.5*10^(-(n+3)) = 5e-07

     4/10000000 = 4e-07 < 5e-07,

     So 4/10000000 = 0.

     When VFP Advanced calculates 4/10000000,
     it will adjust the precision of the result float number to 15 valid digits:

     n=15, eps = 0.5*10^(-(n+3)) = 5e-18

     So 4/10000000 = 4e-07 = 0.000000400000.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Label4ff7ab ::
             push ecx                                                        ;0x004ff7ab :        51
             push ebx                                                        ;0x004ff7ac :        53
             push esi                                                        ;0x004ff7ad :        56
             mov  esi , dword ptr [ Data9370f0 ]                             ;0x004ff7ae :        8b35f0709300
             sub esi , 02Ch                                                  ;0x004ff7b4 :        83ee2c
             push edi                                                        ;0x004ff7b7 :        57
             lea ebx ,  dword ptr [ esi + 44 ]                               ;0x004ff7b8 :        8d5e2c
             push ebx                                                        ;0x004ff7bb :        53
             lea edi ,  dword ptr [ esp + 16 ]                               ;0x004ff7bc :        8d7c2410
             mov  dword ptr [ Data9370f0 ] , esi                             ;0x004ff7c0 :        8935f0709300
             call Fun420dd3                                                  ;0x004ff7c6 :        e80816f2ff
             test eax , eax                                                  ;0x004ff7cb :        85c0
             jne Label4ff89c                                                 ;0x004ff7cd :        0f85c9000000
             mov dword ptr [ Data93cc80 ] ,  eax                             ;0x004ff7d3 :        a380cc9300
             cmp  byte ptr [esi] , 059h                                      ;0x004ff7d8 :        803e59
             je Label5908a3                                                  ;0x004ff7db :        0f84c2100900
             cmp  byte ptr [ebx] , 059h                                      ;0x004ff7e1 :        803b59
             je Label5908a3                                                  ;0x004ff7e4 :        0f84b9100900
             mov eax , esi                                                   ;0x004ff7ea :        8bc6
             call Fun53e878                                                  ;0x004ff7ec :        e887f00300
             mov eax , ebx                                                   ;0x004ff7f1 :        8bc3
             call Fun53e878                                                  ;0x004ff7f3 :        e880f00300
             cmp  byte ptr [esi] , 04Eh                                      ;0x004ff7f8 :        803e4e
             jne Label59086b                                                 ;0x004ff7fb :        0f856a100900
             cmp  byte ptr [ebx] , 04Eh                                      ;0x004ff801 :        803b4e
             jne Label59086b                                                 ;0x004ff804 :        0f8561100900

     Label4ff80a ::
             fld qword ptr [ esi + 16 ]                                      ;0x004ff80a :        dd4610
             fdiv qword ptr [ esi + 60 ]                                     ;0x004ff80d :        dc763c
             fstp qword ptr [ esi + 16 ]                                     ;0x004ff810 :        dd5e10

     Label4ff813 ::
             mov  ecx , dword ptr [ esi + 8 ]                                ;0x004ff813 :        8b4e08
             test ecx , ecx                                                  ;0x004ff816 :        85c9
             jne Label484dd0                                                 ;0x004ff818 :        0f85b255f8ff
             mov  eax , dword ptr [ esi + 52 ]                               ;0x004ff81e :        8b4634
             test eax , eax                                                  ;0x004ff821 :        85c0
             jne Label484dd0                                                 ;0x004ff823 :        0f85a755f8ff
             xor edx , edx                                                   ;0x004ff829 :        33d2

     Label4ff82b ::
             mov  eax , dword ptr [ esi + 52 ]                               ;0x004ff82b :        8b4634
             mov  edi , dword ptr [ esi + 48 ]                               ;0x004ff82e :        8b7e30
             mov  ebx , dword ptr [ esi + 4 ]                                ;0x004ff831 :        8b5e04
             sub edi , eax                                                   ;0x004ff834 :        2bf8
             sub edi , ecx                                                   ;0x004ff836 :        2bf9
             sub edi , edx                                                   ;0x004ff838 :        2bfa
             dec edi                                                         ;0x004ff83a :        4f
             add ebx , edi                                                   ;0x004ff83b :        03df
             cmp ecx , eax                                                   ;0x004ff83d :        3bc8
             mov  dword ptr [ esi + 4 ] , ebx                                ;0x004ff83f :        895e04
             ja Label484dda                                                  ;0x004ff842 :        0f879255f8ff

     Label4ff848 ::
             mov  ecx , dword ptr [ Data9370f8 ]                             ;0x004ff848 :        8b0df8709300
             mov  dword ptr [ esi + 8 ] , eax                                ;0x004ff84e :        894608
             mov  edx , dword ptr [ecx]                                      ;0x004ff851 :        8b11
             mov  ecx , dword ptr [ edx + 0D8h ]                             ;0x004ff853 :        8b8ad8000000
             add ecx , 02h                                                   ;0x004ff859 :        83c102


     ;
     ;                 ----------------------------------------------------------------------------------------------------
     ;                      VFP 9.0 FIX - ADJUST THE PRECISION OF THE FLOATING-POINT NUMBERS FOR THE DIVISION OPERATOR
     ;                                February 2024
     ;                 ----------------------------------------------------------------------------------------------------
     ;                                     CCB
     ;
     ; Adjust the precision of the floating-point numbers for the division operator.
     ;
     ; 2024/2/4, by ccb
     ;

             cmp dword ptr vfpa_sys9035_data,00h
             je Label4ff85c

             sub esp , 08h
             fld qword ptr [ esi + 16 ]

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

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

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

             fistp dword ptr [ esp ]
             mov edx,0Fh
             sub edx,dword ptr [ esp ]
             cmp edx,0
             jl Label4ff85b
             cmp edx,0Fh
             jg Label4ff85b

             cmp edx,ecx
             jle Label4ff85b
             cmp dword ptr vfpa_sys9181_data,ecx
             jle Label4ff85b
             mov ecx,edx
             cmp ecx,dword ptr vfpa_sys9181_data
             jle Label4ff85b
             mov ecx,dword ptr vfpa_sys9181_data
             jmp Label4ff85b

     Label4ff85a ::
             fstp st(0)
             mov edx,0Fh
             cmp edx,ecx
             jle Label4ff85b
             cmp dword ptr vfpa_sys9181_data,ecx
             jle Label4ff85b
             mov ecx,edx
             mov ebx,01h
             mov dword ptr [ esi + 4 ],ebx
             cmp ecx,dword ptr vfpa_sys9181_data
             jle Label4ff85b
             mov ecx,dword ptr vfpa_sys9181_data
             mov ebx,01h
             mov dword ptr [ esi + 4 ],ebx
             jmp Label4ff85b

     Label4ff85b ::
             fstp st(0)
             add esp , 08h
             jmp Label4ff85c


     Label4ff85c ::
             cmp eax , ecx                                                   ;0x004ff85c :        3bc1
             ja Label4ff862                                                  ;0x004ff85e :        7702
             mov eax , ecx                                                   ;0x004ff860 :        8bc1

     Label4ff862 ::
             cmp eax , 012h                                                  ;0x004ff862 :        83f812
             mov  dword ptr [ esi + 8 ] , eax                                ;0x004ff865 :        894608
             ja Label590950                                                  ;0x004ff868 :        0f87e2100900

     Label4ff86e ::
             mov  eax , dword ptr [ esi + 8 ]                                ;0x004ff86e :        8b4608
             mov  edx , dword ptr [ esi + 4 ]                                ;0x004ff871 :        8b5604
             xor ecx , ecx                                                   ;0x004ff874 :        33c9
             test eax , eax                                                  ;0x004ff876 :        85c0
             setne cl                                                        ;0x004ff878 :        0f95c1
             add ecx , eax                                                   ;0x004ff87b :        03c8
             add edx , ecx                                                   ;0x004ff87d :        03d1
             mov eax , edx                                                   ;0x004ff87f :        8bc2
             cmp eax , 028h                                                  ;0x004ff881 :        83f828
             mov  dword ptr [ esi + 4 ] , edx                                ;0x004ff884 :        895604
             jnl Label59095c                                                 ;0x004ff887 :        0f8dcf100900

     Label4ff88d ::
             mov  dword ptr [ esi + 4 ] , eax                                ;0x004ff88d :        894604
             mov  eax , dword ptr [ esp + 12 ]                               ;0x004ff890 :        8b44240c
             test eax , eax                                                  ;0x004ff894 :        85c0
             jne Label590966                                                 ;0x004ff896 :        0f85ca100900

     Label4ff89c ::
             pop edi                                                         ;0x004ff89c :        5f
             pop esi                                                         ;0x004ff89d :        5e
             pop ebx                                                         ;0x004ff89e :        5b
             pop ecx                                                         ;0x004ff89f :        59
             ret                                                             ;0x004ff8a0 :        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_vfp9fix123.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix23.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix24.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix37.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix38.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix97.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix98.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_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
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix373.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix374.asp

     2, mzvfp.com:
     http://www.mzvfp.com/read.php?tid=106670


6. OTHER:

     For reference only, there is no guarantees.

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