----------------------------------------------------------------------
                      VFP 9.0 FIX - ADJUST THE PRECISION FOR THE DIVISION OPERATOR
                                  August 2020
                 ----------------------------------------------------------------------
                                     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.

     Fun4680c6 :: ; proc near
             sub esp , 08h                                                   ;0x004680c6 :        83ec08
             push ebx                                                        ;0x004680c9 :        53
             push ebp                                                        ;0x004680ca :        55
             push esi                                                        ;0x004680cb :        56
             mov esi , eax                                                   ;0x004680cc :        8bf0
             cmp  byte ptr [ edi + 16 ] , 0FAh                               ;0x004680ce :        807f10fa
             mov  dword ptr [ esp + 16 ] , esi                               ;0x004680d2 :        89742410
             je Label5ca396                                                  ;0x004680d6 :        0f84ba221600
             mov  ecx , dword ptr [edi]                                      ;0x004680dc :        8b0f
             test ecx , ecx                                                  ;0x004680de :        85c9
             mov  dword ptr [ esp + 12 ] , ecx                               ;0x004680e0 :        894c240c
             jnl Label465adf                                                 ;0x004680e4 :        0f8df5d9ffff

     Label4680ea ::
             mov eax , ecx                                                   ;0x004680ea :        8bc1
             cdq                                                             ;0x004680ec :        99
             xor eax , edx                                                   ;0x004680ed :        33c2
             sub eax , edx                                                   ;0x004680ef :        2bc2
             cmp eax , 07FFFFFFFh                                            ;0x004680f1 :        3dffffff7f
             jg Label5ca416                                                  ;0x004680f6 :        0f8f1a231600
             cmp ecx , 080000000h                                            ;0x004680fc :        81f900000080
             je Label5ca416                                                  ;0x00468102 :        0f840e231600
             cmp eax , 07FFFh                                                ;0x00468108 :        3dff7f0000
             jg Label5ca3f8                                                  ;0x0046810d :        0f8fe5221600
             mov  byte ptr [esi] , 0F9h                                      ;0x00468113 :        c606f9
             mov  al , byte ptr [ edi + 17 ]                                 ;0x00468116 :        8a4711
             inc esi                                                         ;0x00468119 :        46
             mov  byte ptr [esi] , al                                        ;0x0046811a :        8806
             inc esi                                                         ;0x0046811c :        46
             mov  word ptr [esi] , cx                                        ;0x0046811d :        66890e
             mov  ecx , dword ptr [ esp + 16 ]                               ;0x00468120 :        8b4c2410
             add esi , 02h                                                   ;0x00468124 :        83c602
             mov eax , esi                                                   ;0x00468127 :        8bc6
             pop esi                                                         ;0x00468129 :        5e
             pop ebp                                                         ;0x0046812a :        5d
             sub eax , ecx                                                   ;0x0046812b :        2bc1
             pop ebx                                                         ;0x0046812d :        5b
             add esp , 08h                                                   ;0x0046812e :        83c408
             ret                                                             ;0x00468131 :        c3

     Label5ca396 ::
             movzx eax ,byte ptr [ edi + 18 ]                                ;0x005ca396 :        0fb64712
             add eax , 03h                                                   ;0x005ca39a :        83c003


     ;
     ;                 ----------------------------------------------------------------------
     ;                      VFP 9.0 FIX - ADJUST THE PRECISION FOR THE DIVISION OPERATOR
     ;                                  August 2020
     ;                 ----------------------------------------------------------------------
     ;                                     CCB
     ;
     ; Adjust the precision for the division operator.
     ;
     ; 2020/8/29, by ccb
     ;

             cmp dword ptr [ esp + 14h ] , offset Label847742 ;; Fun8474bb :: ; proc near
             jne Label5ca39d
             cmp dword ptr vfpa_sys9035_data,00h
             je Label5ca39d
             cmp byte ptr [ edi + 17 ] , 0Fh
             jae Label5ca39d
             movzx ecx ,byte ptr [ edi + 17 ]
             add eax , 0Fh
             sub eax , ecx


     Label5ca39d ::
             cmp eax , 012h                                                  ;0x005ca39d :        83f812
             mov ebp , eax                                                   ;0x005ca3a0 :        8be8
             jl Label5ca3a9                                                  ;0x005ca3a2 :        7c05
             mov ebp , 012h                                                  ;0x005ca3a4 :        bd12000000

     Label5ca3a9 ::
             fld qword ptr [ edi + 8 ]                                       ;0x005ca3a9 :        dd4708
             lea ebx ,  dword ptr [ edi + 8 ]                                ;0x005ca3ac :        8d5f08
             sub esp , 08h                                                   ;0x005ca3af :        83ec08
             fstp qword ptr [ esp ]                                          ;0x005ca3b2 :        dd1c24
             call  _isnan                                      ;0x005ca3b5 :        ff150c799100
             add esp , 08h                                                   ;0x005ca3bb :        83c408
             test eax , eax                                                  ;0x005ca3be :        85c0
             jne Label5ca3d4                                                 ;0x005ca3c0 :        7512
             fld qword ptr [ebx]                                             ;0x005ca3c2 :        dd03
             fabs                                                            ;0x005ca3c4 :        d9e1
             fcomp qword ptr [ 8 * ebp + offset Data922450 ]                          ;0x005ca3c6 :        dc1ced50249200
             fstsw ax                                                        ;0x005ca3cd :        dfe0
             test  byte ptr ah , 041h                                        ;0x005ca3cf :        f6c441
             jp Label5ca423                                                  ;0x005ca3d2 :        7a4f

     Label5ca3d4 ::
             fld qword ptr [ Data91fbc8 ]                                    ;0x005ca3d4 :        dd05c8fb9100
             jmp Label5ca421                                                 ;0x005ca3da :        eb45

     Label5ca3dc ::
             mov  byte ptr [esi] , 0F8h                                      ;0x005ca3dc :        c606f8
             mov  al , byte ptr [ edi + 17 ]                                 ;0x005ca3df :        8a4711
             inc esi                                                         ;0x005ca3e2 :        46
             mov  byte ptr [esi] , al                                        ;0x005ca3e3 :        8806
             inc esi                                                         ;0x005ca3e5 :        46
             mov  byte ptr [esi] , cl                                        ;0x005ca3e6 :        880e
             mov  ecx , dword ptr [ esp + 16 ]                               ;0x005ca3e8 :        8b4c2410
             inc esi                                                         ;0x005ca3ec :        46
             mov eax , esi                                                   ;0x005ca3ed :        8bc6
             pop esi                                                         ;0x005ca3ef :        5e
             pop ebp                                                         ;0x005ca3f0 :        5d
             sub eax , ecx                                                   ;0x005ca3f1 :        2bc1
             pop ebx                                                         ;0x005ca3f3 :        5b
             add esp , 08h                                                   ;0x005ca3f4 :        83c408
             ret                                                             ;0x005ca3f7 :        c3

     Label5ca3f8 ::
             mov  byte ptr [esi] , 0E9h                                      ;0x005ca3f8 :        c606e9
             mov  dl , byte ptr [ edi + 17 ]                                 ;0x005ca3fb :        8a5711
             inc esi                                                         ;0x005ca3fe :        46
             mov  byte ptr [esi] , dl                                        ;0x005ca3ff :        8816
             inc esi                                                         ;0x005ca401 :        46
             mov  dword ptr [esi] , ecx                                      ;0x005ca402 :        890e
             mov  ecx , dword ptr [ esp + 16 ]                               ;0x005ca404 :        8b4c2410
             add esi , 04h                                                   ;0x005ca408 :        83c604
             mov eax , esi                                                   ;0x005ca40b :        8bc6
             pop esi                                                         ;0x005ca40d :        5e
             pop ebp                                                         ;0x005ca40e :        5d
             sub eax , ecx                                                   ;0x005ca40f :        2bc1
             pop ebx                                                         ;0x005ca411 :        5b
             add esp , 08h                                                   ;0x005ca412 :        83c408
             ret                                                             ;0x005ca415 :        c3

     Label5ca416 ::
             fild dword ptr [ esp + 12 ]                                     ;0x005ca416 :        db44240c
             lea ebx ,  dword ptr [ edi + 8 ]                                ;0x005ca41a :        8d5f08
             mov  byte ptr [ edi + 18 ] , 00h                                ;0x005ca41d :        c6471200

     Label5ca421 ::
             fstp qword ptr [ebx]                                            ;0x005ca421 :        dd1b

     Label5ca423 ::
             mov  byte ptr [esi] , 0FAh                                      ;0x005ca423 :        c606fa
             mov  cl , byte ptr [ edi + 17 ]                                 ;0x005ca426 :        8a4f11
             mov  byte ptr [ esi + 1 ] , cl                                  ;0x005ca429 :        884e01
             mov  dl , byte ptr [ edi + 18 ]                                 ;0x005ca42c :        8a5712
             inc esi                                                         ;0x005ca42f :        46
             inc esi                                                         ;0x005ca430 :        46
             mov  byte ptr [esi] , dl                                        ;0x005ca431 :        8816
             inc esi                                                         ;0x005ca433 :        46
             pushd 08h                                                       ;0x005ca434 :        6a08
             mov edx , ebx                                                   ;0x005ca436 :        8bd3
             mov ecx , esi                                                   ;0x005ca438 :        8bce
             call Fun42c19b                                                  ;0x005ca43a :        e85c1de6ff
             mov  ecx , dword ptr [ esp + 16 ]                               ;0x005ca43f :        8b4c2410
             add esi , 08h                                                   ;0x005ca443 :        83c608
             mov  byte ptr [esi] , 0CCh                                      ;0x005ca446 :        c606cc
             inc esi                                                         ;0x005ca449 :        46
             mov eax , esi                                                   ;0x005ca44a :        8bc6
             pop esi                                                         ;0x005ca44c :        5e
             pop ebp                                                         ;0x005ca44d :        5d
             sub eax , ecx                                                   ;0x005ca44e :        2bc1
             pop ebx                                                         ;0x005ca450 :        5b
             add esp , 08h                                                   ;0x005ca451 :        83c408
             ret                                                             ;0x005ca454 :        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_vfp9fix97.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix98.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.