summaryrefslogtreecommitdiff
path: root/projects/12/Math.jack
diff options
context:
space:
mode:
Diffstat (limited to 'projects/12/Math.jack')
-rw-r--r--projects/12/Math.jack19
1 files changed, 16 insertions, 3 deletions
diff --git a/projects/12/Math.jack b/projects/12/Math.jack
index 61e09fc..01bce8f 100644
--- a/projects/12/Math.jack
+++ b/projects/12/Math.jack
@@ -28,6 +28,18 @@ class Math {
return ~(x & twoToThe[i] = 0);
}
+ function int sign(int x) {
+ if (x > 0) {
+ return 1;
+ } else {
+ if (x < 0) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+ }
+
/** Returns the absolute value of x. */
function int abs(int x) {
if (x > 0){
@@ -43,11 +55,12 @@ class Math {
* the Jack expressions x*y and multiply(x,y) return the same value.
*/
function int multiply(int x, int y) {
- var int z, res, shiftedX, i;
+ var int res, shiftedX, i;
let shiftedX = x;
let i = 0;
let res = 0;
- while ((twoToThe[i] > x) & (i < 16)) {
+ //while ((~(twoToThe[i] > y)) & (i < 16)) {
+ while (i < 16) {
if (Math.bit(y, i)){
let res = res + shiftedX;
}
@@ -117,7 +130,7 @@ class Math {
while (~(k < 0)) {
let z = y + twoToThe[k];
let w = z * z;
- if ((w < x) & (w > 0)) {
+ if ((~(w > x)) & (w > 0)) {
let y = z;
}
let k = k - 1;