Eslam Ahmed
Eslam Ahmed

Reputation: 3

Marie Simulator Multiplication of fractions

I have a task to use Marie Simulator to calculate the area of a circle requiring its radius

I know that in Marie Language there is no multiplication operator so we use multiplication by adding numbers several times so If I wanted to multiply 2*3 I could write it down like 3+3 or 2+2+2

but when using the area of a circle there is pi which is 3.14 I can't imagine how could I get it so can anyone give me the algorithm or code for that ?

thanks in advance.

Upvotes: 0

Views: 384

Answers (2)

trincot
trincot

Reputation: 350310

As in MARIE numbers are integers in the range [−215, ... , 215−1], the formula 𝑟²π will lead to overflow when 𝑟 > 102. So unless you are going to implement multi-word data types in MARIE, there are only 103 possible values for 𝑟 for which you can do the calculation. That means we have the choice to either create a program to do the multiplication with all the challenges of approaching π as close as necessary for accurate results and not getting limited by multiplication overflows, or to create an array with precalculated results and read the result from that array at index 𝑟.

If we go for the hard-coded array, then the total program, including the array takes 110 words of memory in MARIE:

          Input
          Add       AreasPtr
          Store     ResultPtr
          LoadI     ResultPtr
          Output
          Halt

/ Variables
ResultPtr,Hex 0
/ Constants
AreasPtr, Adr Areas
Areas,    Dec 0
          Dec 3
          Dec 13
          Dec 28
          Dec 50
          Dec 79
          Dec 113
          Dec 154
          Dec 201
          Dec 254
          Dec 314
          Dec 380
          Dec 452
          Dec 531
          Dec 616
          Dec 707
          Dec 804
          Dec 908
          Dec 1018
          Dec 1134
          Dec 1257
          Dec 1385
          Dec 1521
          Dec 1662
          Dec 1810
          Dec 1963
          Dec 2124
          Dec 2290
          Dec 2463
          Dec 2642
          Dec 2827
          Dec 3019
          Dec 3217
          Dec 3421
          Dec 3632
          Dec 3848
          Dec 4072
          Dec 4301
          Dec 4536
          Dec 4778
          Dec 5027
          Dec 5281
          Dec 5542
          Dec 5809
          Dec 6082
          Dec 6362
          Dec 6648
          Dec 6940
          Dec 7238
          Dec 7543
          Dec 7854
          Dec 8171
          Dec 8495
          Dec 8825
          Dec 9161
          Dec 9503
          Dec 9852
          Dec 10207
          Dec 10568
          Dec 10936
          Dec 11310
          Dec 11690
          Dec 12076
          Dec 12469
          Dec 12868
          Dec 13273
          Dec 13685
          Dec 14103
          Dec 14527
          Dec 14957
          Dec 15394
          Dec 15837
          Dec 16286
          Dec 16742
          Dec 17203
          Dec 17671
          Dec 18146
          Dec 18627
          Dec 19113
          Dec 19607
          Dec 20106
          Dec 20612
          Dec 21124
          Dec 21642
          Dec 22167
          Dec 22698
          Dec 23235
          Dec 23779
          Dec 24328
          Dec 24885
          Dec 25447
          Dec 26016
          Dec 26590
          Dec 27172
          Dec 27759
          Dec 28353
          Dec 28953
          Dec 29559
          Dec 30172
          Dec 30791
          Dec 31416
          Dec 32047
          Dec 32685

It looks near impossible to write a program that performs the necessary arithmetic without this lookup table in less than 110 lines.

Upvotes: 0

Erik Eidt
Erik Eidt

Reputation: 26656

MARIE does not have floating point support.

So, should refer to your course work or ask your instructors what to do, as it is not obvious.

It is, of course, possible to do floating point in software, but the complexity is extraordinary, so unlikely to be what the're looking for.

You could use fixed point arithmetic, fractions, or decimal.

Here's one solution that might be appropriate: multiply one of the numbers (having decimal places) by some fixed constant factor, do the arithmetic, then interpret answers accordingly.  For example, let's use 100 as the factor, so 3.14 is represented by 314.  Let's say r is 9, so we can square that (9x9=81), then multiply 81 x 314 = 25434.  Now we know that value is 100x too large, so the real answer is 254.34.  (You can choose to ignore the .34, or, round it, then ignore.  254 is still more accurate than 243 which we would get from 9x9x3.)

Fixed point multiplies all numbers by the constant (usually a power of 2, so that the binary point is in the same bit position).  Additions are relatively straightforward, but multiplications need to interpret results by factoring in (or out) that both sources are in scaled, meaning the answer is doubly scaled.

If you need to measure radius also with decimal digits, e.g. 9.5, then you could scale both 9.5 and 3.14 by 100.  Then we need 950x950, and multiply by 314.  The answer will be 100x100x100 too large, so 1000000x too large.  With this approach, 16 bits that MARIE offers will overflow, so you would need to use at least 32-bit arithmetic (not trivial on 16-bit machine).

You can use two different scaling factors, e.g. 9.5 as 95 and 3.14 as 314.  Take 95x95x314, is 10000x too large, so interpret the answer accordingly.  Still this will overflow MARIE's 16-bits

Fractions would maintain both a numerator and denominator for all numbers.  So, 3.14 could be 314/100, and 9.5 could be 95/10 — and simplified 157/50 and 19/2.  To add you have to find a common denominator, convert, then sum numerators.  To multiply you multiply both numerators and denominators: numerator = 19x19x157, denominator = 2x2x50.  Just fits in 16-bit unsigned arithmetic, but still overflows 16-bit signed arithmetic..

And finally binary coded decimal is more like a string format, where numbers are stored one decimal digit per byte or per nibble (packed decimal).  Algorithms for addition and subtraction need to account for variable length inputs.

Big integer forms also use similar to binary coded decimal but compose much larger elements instead of single decimal digits.

All of these approaches require some thought, and the more limitations you want to remove, the more work required.  So, I'd suggest to go back to your course to find what they really want.

Upvotes: 0

Related Questions