Programmers - Arithmetic II: We continue to introduce you to mathematical procedures. We will talk about methods of raising to any degree and extracting any root.

ZX Format #08
Arithmetic II.         
                               
                               
music by IRONMAN               
(C)GreenFort                   
________________________________
                               
   Dalee we continue to introduce you to
mathematical procedures.  In the past
since we looked at the methods of multiplication and
dividing numbers, and here we will talk about me-
procedures for raising to any power and calculating
treatment of any root.                    
                               
            ROOTS.             
                               
 Method of root extraction of any degree
based on the gradual approximation of science
reptile of the taken number to the answer.              
 Below is the mathematical algorithm
extracting the nth root.           
                               
 1. Select any number - the closer it is
number to the answer, the faster it will happen
calculation.  This will be our current answer.
 2. Bdepending on the root index (n)
divide the number under the root n-1 times. (For example,
ru, to extract the square root of
root exponent = 2, divide once, for cube-
divide the logical root 2 times).             
 3.  Check the result after
divisions corresponds to the current answer.   
 4.  If the numbers are approximately equal, with a given
accuracy, then the end of the program.            
 5.  Multiply the quotient after division by
n-1.And add our current answer, we will find
the arithmetic mean of these numbers.       
 6.  Now this is our current number.      
 7. Go to step 2                   
                               
 As an example, I propose the procedure
extracting cube root:            
                               
 At the input: A,H,L-number from which
we treat the root. A - integer part of the number, HL
- fractional part of a number.                   
                               
 Output: A,H,L-result. A - whole 
part of a number, HL - fractional part of a number.;A,HL=A,HL'(1/3)                        
CUBICF  LD (DATA+1),A                                         
        LD (DATHL+1),HL                                       
        LD B,1         ;выбранное наугад                      
        LD DE,#0000    ;   число-единица                      
DATA    LD A,#00                                              
DATHL   LD HL,#0000                                           
        CALL DIVISIO   ;число под корнем                      
        CALL DIVISIO   ;делим два раза                        
        LD (LHL+1),HL                                         
        LD (LA+1),A                                           
        PUSH DE LD DE,#0020    ;заданная точность                     
        ADD HL,DE      ;до 1/1024,т.е.                        
        ADC A,#00      ;+-1/2048                              
        POP DE                                                
        CP B           ;проверка точности                     
        JR C,LA        ;верхний предел                        
        JP NZ,CUBЗ                                            
        LD A,H                                                
        CP D                                                  
        JR C,LA                                               
        JP NZ,CUBЗ                                            
        LD A,L  CP E                                                  
        JR C,LA                                               
CUBЗ    LD A,(LA+1)                                           
        PUSH DE                                               
        LD HL,(LHL+1) ;проверка точности                      
        LD DE,#0020   ;нижний предел                          
        SBC HL,DE                                             
        SBC A,#00                                             
        POP DE                                                
        CP B                                                  
        JR C,CUB2                                             
        JPNZ,LA                                              
        LD A,H                                                
        CP D                                                  
        JR C,CUB2                                             
        JP NZ,LA                                              
        LD A,L                                                
        CP E                                                  
        JP NC,LA                                              
CUB2    LD A,(LA+1)   ;заданная точность                      
        LD HL,(LHL+1) ;достигнута,выход                       
        RET                                                   
LA      LD A,#00    ;Calculation of the arithmetic average                       
LHL LD HL,#0000 ;metal three                         
        ADD HL,DE ;numbers:two numbers -                        
        ADC A,B ;result after de-                      
        ADD HL,DE ;                       
        ADC A,B ;general result                          
        LD B,#03                                             
        LD DE,0                                               
        CALL DIVISIO                                         
        LD B,A                                               
        LD D,H                                               
        LD E,L                                               
        JP DATA ;cycle                                     
                               
                               
 This program can be easily converted into
program to extract the cor-
Nya.                                      
                               
 Division subroutine for the extraction procedure
root treatment.                           
 At the input: A,HL - divisible number (A - integer 
           lak part, HL - fractional part)
          B,DE-divisor (B - integer part,
           DE - fractional part)           
                               
 Output: A,HL-result (A - integer  
          part DE - fractional part)      
                               
                    ;A,HL=A,HL/B,DE                           
DIVISIO                                                       
        LD C,A                                               
        XOR A                                                 
        EXX                                                   
        LD HL,#0001                                           
        LD B,H                                                
        EXX                                                   
DIV1    SLA L                                                 
        RL H                                                  
        RL C                                                  
        RLA                                                   
        CP B                                                  
        JR C,DIV2                                             
        JR NZ,DIVЗ                                            
        EX AF,AF'                                             
        LD A,C  CP D                                                  
        JR C,DIV22                                            
        JR NZ,DIVЗ2                                           
        LD A,H                                                
        CP E                                                  
        JR C,DIV22                                            
DIVЗ2   EX AF,AF'                                             
DIVЗ    EX AF,AF'                                             
        LD A,L                                                
        LD L,H                                                
        LD H,C                                                
        SBC HL,DE ;NC                                         
        LD C,H                                                
        LD H,L                                                
        LD L,A                                                
        JR NC,DIVЗЗ                                           
        EX AF,AF'                                             
        SUB B                                                 
        DEC A                                                 
DIVЗЧ   EXX                                                   
        SLI L                                                 
        RL H                                                  
        RL B                                              
        EXX                                                   
        JP NC,DIV1                                            
        JP DIVEXIT                                            
DIV22   EX AF,AF'                                             
DIV2    EXX                                                   
        SLA L                                                 
        RL H                                                  
        RL B                                                  
        EXX                                                   
        JP NC,DIV1                                            
DIVEXIT EXX                                       PUSH HL                                               
        LD A,B                                                
        EXX                                                   
        POP HL                                                
        RET                                                   
DIVЗЗ   EX AF,AF'                                             
        SUB B                                                 
        JP DIVЗЧ                                              
                               
           СТЕПЕНь.            
                               
 Возводить в степень можно двумя способа-
ми:                                      
Первый  основан на многократном умножении
пoдстeпeннoгo числа.                     
Второй  основан  на разложении показателя
степени на множители, кратные двум,т.e.,for example: 2^8 = ((x^2)^2)^2, which will require   
only three multiplication operations.          
                               
                               
 Let's start with the simple method. I suggest you
We would like to bring to your attention the construction procedure in the center
any degree.                             
                               
 At the input: A,H,L - the number you need
    raise to a power, B-power (not 0!)
                               
 Output: A,H,L - result            
                               
                       ;A,HL=A,HL'B                           
STEPEN1  ;A,HL=NUMBER B=STEPEN                  
        LD (AHL+1),HL                                         
        LD (AA+1),A                                           
        DEC B                                                 
        RET Z ;to the first power                       
STP1CYC PUSH BC AA LD B,#00                                             
AHL LD DE,#0000                                           
TOCYC CALL MULTI                                           
        POP BC                                               
        DJNZ STP1CYC                                         
        RET                                                   
                               
 With a large exponent, this
The procedure can take quite a long time.
In such cases, it is advisable to use
Follow the procedure below.          
                               
 At the input: A,H,L - number to be reduced, C =
            degree.                     
                               
 Output: A,H,L - result.;A,H,L=A,H,L'C                        
STEPEN2 LD B,8          ;счетчик цикла                        
CYCLSTP RLC C           ;поиск значащего                      
        DEC B           ;разряда                              
        JR Z,ZERSTP                                           
        JR NC,CYCLSTP                                         
        LD (LHL2+1),HL ;сохранение под-                       
        LD (LA2+1),A   ;степенного числа                      
CYCSTP2 PUSH BC                                               
        LD B,A        ;текущее число воз-                     
        LD D,H        ;водим в квадрат                        
        LD E,L  CALL MULTI                                            
        POP BC                                                
        RLC C         ;выборка текущего                       
        PUSH BC       ;разряда степени                        
        JR NC,ENDCYCL                                         
        PUSH BC       ;выбранный разряд=1                     
LHL2    LD DE,#0000   ;домножаем текущий                      
LA2     LD A,#00      ;результат на под-                      
        CALL MULTI    ;степенное число                        
        POP BC                                                
ENDCYCL DJNZ CYCSTP2  ;циклRET                                                   
ZERSTP EXX ;zero processing                      
        LD A,0 ;degrees                                
        LD HL,1                                               
        RET                                                   
                               
                               
 Multiplication routine for procedure
exponentiation                     
                               
 At the input: A,H,L - first factor, B, D, E
- second multiplier.                      
                               
 Output: A,H,L - result, variable
FLAG=0 no overflow, FLAG<>0 overflow
opinion.                                   
                               
                      ;A,H,L=A,H,L*B,D,E                      
MULTI EXX                                                   
        LD B,24                                               
        LD HL,#0000                                           
        LD C,H                                                
PF2     EXX                                                   
        SRL B                                                 
        RR D                                                  
        RR E                                                  
        JR NC,PFЗ                                             
        PUSH AF                                               
        PUSH HL                                               
        EXX                                                   
        POP DE  ADD HL,DE                                             
        ADC A,C                                               
        LD C,A                                                
        EXX                                                   
        POP AF                                                
PFЗ     SLA L                                                 
        RL H                                                  
        RLA                                                   
        CALL C,FLAGC                                          
        EXX                                                   
        DJNZ PF2                                              
        LD A,C                                              
        RET                                                   
FLAGC   LD A,(FLAG) ;переполнение                             
        OR #FF                                                
        LD (FLAG),A                                           
        RET                                                   
FLAG    DB #00      ;#FF=C,00=NC                              
                               
_______________________________

Share your thoughts about the article