Qingdao Single Chip Microcomputer Develops a Single Chip Microcomputer C Language Fast Precision Division Scheme

2020-12-28 18:39:47 admin

Qingdao Single Chip Microcomputer Develops a Single Chip Microcomputer C Language Fast Precision Division Scheme


     The current 51 single-chip microcomputers generally use floating-point calculations in the division of results with decimal points, but floating-point calculations have a disadvantage that they are very time-consuming, which is not suitable for working conditions that require strict time.

    The author’s company has been undertaking projects such as single-chip microcomputer, circuit, electro-mechanical fluid, industrial control, automation, software EXE programming for a long time, and recently made a design of a single-chip calculator. When designing division, long integer division and remainder operations can be used to obtain several decimals. Bit precision calculations are shared with everyone.

The design ideas are as follows:

Assuming long integer divisor a and long integer dividend b, the steps are as follows:

<1> Get the integer part of the division, c=a/b;

<2> Let d be a%b, e=10*d,

Get the first decimal place of the division, f=e/b;

(Key point: Multiply the remainder of a and b by 10 times, and divide by the dividend b to get one decimal place after the decimal point)

<3> Let g be e%b, h=10*g,

Get the second decimal place of the division, i=h/b;

The division result obtained by the above steps is c.fi...

By analogy, you can get any decimal for division...

/*********** The C language program is attached below, and Yongke Technology Studio-provides solutions such as single chip microcomputer, circuit board, electromechanical fluid, industrial control, controller, automation, software and so on.

************************************************** ********/

unsigned long result, result_p;//Integer and decimal part of the result of division

// result_p is the decimal part, for example, result_p=12345678, which means 0.12345678

Void chufa(unsigned long chushu, unsigned long beichushu, unsigned char wei)

// wei represents the number of places after the decimal point

{unsigned char i;

     unsigned long tmp;

     result=result_p=0;

     if (beichushu!=0)//The dividend must not be 0

        {

         if (wei==0)

            {result=chushu/beichushu;//Calculate the integer part

             result_p=0;

            }

            else

              {result=chushu/beichushu;//Calculate the integer part

               tmp=chushu%beichushu;

               for (i=1;i<=wei;i++)//Calculate the decimal part

                   {tmp=tmp*10;

                    result_p=result_p*10+tmp/beichushu;

                    tmp=tmp%beichushu;

                   }

              }

        }

}


label: Qingdao MCU