# Qingdao MCU develops a fast and accurate division scheme of MCU C language

Qingdao MCU develops a fast and accurate division scheme of MCU C language

At present, the 51 single chip microcomputer can generally use the floating-point calculation method in dividing the result with decimal point, but the floating-point calculation has a disadvantage that it is very time-consuming and is not applicable in the working condition with strict time requirements.

The author's company has long undertaken projects such as single chip microcomputer, circuit, electromechanical hydraulic, industrial control, automation and software exe programming. Recently, it has designed a single chip microcomputer calculator. When designing division, it can obtain the precision calculation of several decimal places by using long shaping division and remainder operation, which can be shared with you.

The design idea is as follows:

Assuming the long shaping divisor A and the long shaping 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 of division, f = E / B;

(important: multiply the remainder of a and B by 10 times and divide by the divisor 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 division, I = H / B;

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

By analogy, you can get any decimal of division

/***********The C language program is attached below. Yongke technology studio provides solutions such as single chip microcomputer, circuit board, electromechanical hydraulic, industrial control, controller, automation and software.

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

unsigned long result, result_ p;// Represents the integer and decimal parts of the division result

// result_ P is the decimal part, such as result_ P = 12345678, indicating 0.12345678

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

//Wei is the number of decimal places

{ unsigned char i;

unsigned long tmp;

result=result_p=0;

If (beichushu! = 0) / / the divisor 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;

}

}

}

}