04-19-2013, 03:42 PM
I have occasionally been messing with arithmetical calculations to perform mathematical functions using Boolean logic (redstone).
Addition, subtraction, multiplication and division are relative simple. You could of course use Taylor series to proximate any function, since it only requires multiplication and addition, however you would have to add more and more terms to get a sufficiently accurate answer when trying to calculate the solution further away from the point where you proximate the function.
After this I also explored if the iterative Newton–Raphson method would be a possibility. However often these solution require other not easy to calculate functions and the amount of iterations isn't fixed, since you would continue until <x_n+1 = x_n>.
But there are also other methods of acquiring solutions of certain function, often using conditional logic. For instance to calculate the square root, this method can also be applied to binary which has the advantage that you do not have to calculate the z, since it would either be an one or a zero (in which case the remainder after the subtraction will be negative).
After I discovered this method to calculate the square root I also wondered if there are also methods for other commonly using mathematical functions like logarithm, exponential, sine, cosine and tangent.
And I came up with this arithmetic to calculate log2(x):
Since you need to know the exponent and mantissa, it would be easier if you would apply this method to a floating point number.
The exponential function would be easier to do, since it would just be <N> multiplications.
I am not sure how you could do sine, cosine and tangent. However you would only have to be able to do one of them, since: cos(x) = sin(x + pi/2) and tan(x) = sin(x) / sin(x + pi/2). First of all you could just take the 2*pi module of x, since all these functions are cyclic, which reduces the range of x in which the solution has to be found. This range can be reduced even more, since: sin(x) = -sin(x + pi) and half a cycle is symmetric as well: sin(x) = sin(pi - x), which reduces the range of x down to [0, pi/2].
Addition, subtraction, multiplication and division are relative simple. You could of course use Taylor series to proximate any function, since it only requires multiplication and addition, however you would have to add more and more terms to get a sufficiently accurate answer when trying to calculate the solution further away from the point where you proximate the function.
After this I also explored if the iterative Newton–Raphson method would be a possibility. However often these solution require other not easy to calculate functions and the amount of iterations isn't fixed, since you would continue until <x_n+1 = x_n>.
But there are also other methods of acquiring solutions of certain function, often using conditional logic. For instance to calculate the square root, this method can also be applied to binary which has the advantage that you do not have to calculate the z, since it would either be an one or a zero (in which case the remainder after the subtraction will be negative).
After I discovered this method to calculate the square root I also wondered if there are also methods for other commonly using mathematical functions like logarithm, exponential, sine, cosine and tangent.
And I came up with this arithmetic to calculate log2(x):
Code:
y = floor(log2(x(n))); // Exponent
m = x(n) * 2 ^ -y; // Mantissa
b = 1;
f = sqrt(2); // You could save a list of all the different values of <f> to reduce the calculation time.
for k = 1 : N // With <N> the number of bits.
if b * f < m
b = b * f;
y = y + 2 ^ -k;
end
f = sqrt(f);
end
The exponential function would be easier to do, since it would just be <N> multiplications.
I am not sure how you could do sine, cosine and tangent. However you would only have to be able to do one of them, since: cos(x) = sin(x + pi/2) and tan(x) = sin(x) / sin(x + pi/2). First of all you could just take the 2*pi module of x, since all these functions are cyclic, which reduces the range of x in which the solution has to be found. This range can be reduced even more, since: sin(x) = -sin(x + pi) and half a cycle is symmetric as well: sin(x) = sin(pi - x), which reduces the range of x down to [0, pi/2].