3 ms·
There are a few different ways, so I'll choose a method that's pretty easy to explain, but not necessarily the simplest to actually execute. I'll assume you can
by WCSTombs 2y ago
There are a few different ways, so I'll choose a method that's pretty easy to explain, but not necessarily the simplest to actually execute. I'll assume you can do addition, subtraction, and multiplication of binary floating-point numbers by hand.
For division A/B, you compute it as A*(1/B), so it suffices to be able to compute 1/B. Because in floating-point you can multiply by powers of 2 and +/-1 "for free," we can assume B is between 1 and 2. So here's the algorithm:
x_0 = 3/4
x_{n+1} = x_n*(2 - x_n*B)
It can be shown that x_n converges to 1/B, and in fact each subsequent iteration has approximately double the number of correct bits.
For square roots, it's a pretty similar idea. To compute sqrt(B), we can similarly assume B is between 0.5 and 2, and then
x_0 = 1
x_{n+1} = 0.5*(x_n + B/x_n)
(So you need to use the division algorithm as a subroutine.)
For sine and cosine, I would start with the Taylor series. You can use the symmetry and periodicity to bring the argument (for example) into [0, pi/2], where you can guarantee a good error bound with a finite number of terms of the Taylor series. There's also a trick, you can divide the argument by some large power of two to make it really close to zero, use just a few terms of the Taylor series to get great approximations to sin(x/2^n) and cos(x/2^n), and then use the angle-doubling trigonometric identities
sin(2*a) = 2*sin(a)*cos(a)
cos(2*a) = cos(a)^2 - sin(a)^2
repeatedly to get sin(x) and cos(x).
These are just some ways to approach these computations, and there are other methods. It's not unreasonable to base computational architecture on these algorithms, but if you really want to do them by hand on paper, there may be simpler ways, especially if you just want a few significant bits or digits.