Add solutions for part 1
This commit is contained in:
133
projects/12/MathTest/Math.jack
Normal file
133
projects/12/MathTest/Math.jack
Normal file
@@ -0,0 +1,133 @@
|
||||
// This file is part of www.nand2tetris.org
|
||||
// and the book "The Elements of Computing Systems"
|
||||
// by Nisan and Schocken, MIT Press.
|
||||
// File name: projects/12/Math.jack
|
||||
|
||||
/**
|
||||
* A library of commonly used mathematical functions.
|
||||
* Note: Jack compilers implement multiplication and division using OS method calls.
|
||||
*/
|
||||
class Math {
|
||||
|
||||
/** Initializes the library. */
|
||||
function void init() {
|
||||
return;
|
||||
}
|
||||
|
||||
/** Returns the absolute value of x. */
|
||||
function int abs(int x) {
|
||||
if (x < 0) {
|
||||
let x = -x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/** Returns the product of x and y.
|
||||
* When a Jack compiler detects the multiplication operator '*' in the
|
||||
* program's code, it handles it by invoking this method. In other words,
|
||||
* the Jack expressions x*y and multiply(x,y) return the same value.
|
||||
*/
|
||||
function int multiply(int x, int y) {
|
||||
var int sum;
|
||||
var int i;
|
||||
var int mask;
|
||||
var int xShifted;
|
||||
|
||||
let sum = 0;
|
||||
let i = 0;
|
||||
let mask = 1;
|
||||
let xShifted = x;
|
||||
|
||||
if (x = 1) {
|
||||
return y;
|
||||
}
|
||||
|
||||
if (y = 1) {
|
||||
return x;
|
||||
}
|
||||
|
||||
while (i < 16) { // 16 because we got a 16 bit architecture
|
||||
if (y & mask) {
|
||||
let sum = sum + xShifted;
|
||||
}
|
||||
let xShifted = xShifted + xShifted; // xShifted << 2;
|
||||
let mask = mask + mask; // mask << 2;
|
||||
let i = i + 1;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
/** Returns the integer part of x/y.
|
||||
* When a Jack compiler detects the multiplication operator '/' in the
|
||||
* program's code, it handles it by invoking this method. In other words,
|
||||
* the Jack expressions x/y and divide(x,y) return the same value.
|
||||
*/
|
||||
function int divide(int x, int y) {
|
||||
var int q;
|
||||
var int sign;
|
||||
|
||||
let sign = 1;
|
||||
|
||||
if (x < 0) {
|
||||
let x = -x;
|
||||
let sign = -sign;
|
||||
}
|
||||
|
||||
if (y < 0) {
|
||||
let y = -y;
|
||||
let sign = -sign;
|
||||
}
|
||||
|
||||
if (y > x) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let q = x / (y + y);
|
||||
|
||||
if ((x - (2 * q * y)) < y) {
|
||||
return (q + q) * sign;
|
||||
} else {
|
||||
return (q + q + 1) * sign;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the integer part of the square root of x. */
|
||||
function int sqrt(int x) {
|
||||
var int y;
|
||||
var int p;
|
||||
var int s;
|
||||
var int s2;
|
||||
|
||||
let y = 0;
|
||||
let p = 128; // 2^7
|
||||
|
||||
while (p > 0) {
|
||||
let s = y + p;
|
||||
let s2 = s * s;
|
||||
if ((s2 - 1) < x) {
|
||||
if (s2 > 0) {
|
||||
let y = s;
|
||||
}
|
||||
}
|
||||
let p = p / 2; // p >> 1;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
/** Returns the greater number. */
|
||||
function int max(int a, int b) {
|
||||
if (a > b) {
|
||||
return a;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/** Returns the smaller number. */
|
||||
function int min(int a, int b) {
|
||||
if (a < b) {
|
||||
return a;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user