Minecraft Blogs / Tutorial

base 10 computer explanation : how to add , substract , multiply , divide or perform square roots in Minecraft

  • 412 views, 2 today
  • 4
  • 1
IA_Des's Avatar IA_Des
Level 49 : Master Engineer
37
This post explains how my new calculator operates.

You can find it here



The computer can perform additions, substractions, multiplications, divisions and square roots .This post will be divided in 5 parts, each of them explaining how the operation works.



Both inputs are 3 digits long (from 0 to 999) and the output is 7 digits long (enough place for 999x999 and a good precision for division and roots)

  1. The Addition (+)



Outside minecraft adding two numbers is really easy as long as you don’t forget the carry.

But in minecraft it’s only possible to perform substractions with comparators, plus, the redstone power is in base 16 (from 0 to 15) which means we need to use math tricks.



A-(-B) = A+B

However if we set A to be 9 and B to be 3 we get

A-(-B) = 9-(0) = 9 as redstone doesn’t allow negative numbers to exist.



Let’s get around this problem by using 15 (which is the biggest number for a digit in base 16)

15-(15-A-B) = 15-15+A+B = A+B

Then, if A = 9 and B = 3 we get 15-(15-9-3) = 15-(3) = 12 which is 9+3

This is better but it won’t works if A+B is greater than 15, plus we need to get the carry.



Let’s use our final trick:

C = B-(9-A)

If C = 0: S = 15-(15-A-B), else S = B-(10-A)



This is equivalent to saying C is not 0 (1 or more) if A+B > 9 (>= 10) and S = A+B or A+B-10 according to the logic state of C.

You can try yourself and see it works for any value of A and B (A and B are positive integers from 0 to 9 included) because 0 < A+B < 9+9 = 18 (no more than one carry).



The units created can even be stacked because C is either 0 or 1, meaning 0 < A+B+C < 19 (max 1 carry)



In minecraft it looks like that:

adder

A & B are inputs; C & S are outputs (the C input from the previous unit is not represented here)

Keep in mind each time an addition is done there is a module like that involved.





  1. the Substraction (-)



There is at least two ways to substract numbers. I choose to use the “overflow method” as it is simpler to implement. It’s probably not the method you personally use to compute the difference between two numbers so I will explain it briefly.

substract






The implementation in minecraft is simple; we just need to add a system to get before and after the adder.



  • The Multiplication (x)

From now on, I’ll use pseudo-code to explain how the operations are implemented



As I didn’t want to implement the multiplication tables in Minecraft I choose to use sequential additions.

Since A x B = A + A + … + A (B times) can be a long addition this idea seems limited to small numbers.

However there’s a way to deal with any given number in a decent amount of time by separating the digits of the second number. (That’s what you usually do to multiply)

Ex :
641*234 = 641*2*100 + 641*3*10 + 641*4*1


Where

641*2 = 1282
641*3 = 1923
641*4 = 2564



Then, by shifting (as multiplying by is the same as shifting by n to the right) and adding those three numbers we get the right answer.



Let’s say we want S = A x B with B = u*100+v*10+w*1

In minecraft it process like that:

Initialize T at 0 and S at 0

While (u > 0 or v > 0 or w > 0):

T += A

Decrease u, v and w by 1

If u reaches 0:

S += T*100

If v reaches 0:

S += T*10

If w reaches 0:

S += T*1


  1. The division (/)



Searching for S in A / B = S is the same as finding what S verify B x S = A

The common way to divide two numbers consists in getting the digits one by one as followed:

divide

Each time we search the biggest X so that 313*X is lesser than the result of the substraction before (with a 0 added at the end)



division algorithm

  1. The Square Root (√)



It’s likely you have never heard about computing square roots by hand so I recommend you to try doing one.



It works like that:

Split your number into groups of 2 digits such that 53512 gives 05 35 12 and 1535 gives 15 35 12. (Whether the number as an odd number of digits)



For the first step, find the greatest U for U x U to be lesser than the first group.

The multiple U you’ll get will be the digits of the result of the root

Then substract the two numbers and ‘lower’ the next group. (Call it R)

From now on, SC is the sum of the numbers appearing in the multiplication. (U + U for the first step)

Now, for each step we’ll have to find the greatest U so that (SC*10 + U) * U is lesser than R. Therefore, the SC at the next step is SC*10+U+U.

Repeat the process to get the precision you want



Example for x = 535:

square root + prog



exp roots







feel free to comment for more information
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome