Comparison failure at line 2

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Comparison failure at line 2

jfmolderez
This post was updated on .
I cannot figure out what could go wrong with the comparison in the following lines of Jack code :

var int i;
let i = 0;

while (i < 16) {
    ....
    let i = i + 1;
}

However I get "Comparison failure at line 2" ???????

Screenshot - VMEmulator
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

WBahn
Administrator
You've given virtually nothing to go on.

What did YOUR output produce?

Where did it fail to match the comparison file?

You have 14 tests, which ones did it pass and which ones did it fail?

We are NOT mind readers!
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

jfmolderez
I have only tested the first module: Math.

This is the MathTest.out :
|RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|RAM[8004]|RAM[8005]|RAM[8006]|RAM[8007]|RAM[8008]|RAM[8009]|RAM[8010]|RAM[8011]|RAM[8012]|RAM[8013]|
|       6 |    -180 |  -18000 |  -18000 |       0 |       3 |   -3000 |       0 |       3 |       0 |       0 |       0 |       0 |       0 |

and this is MathTest.cmp :
|RAM[8000]|RAM[8001]|RAM[8002]|RAM[8003]|RAM[8004]|RAM[8005]|RAM[8006]|RAM[8007]|RAM[8008]|RAM[8009]|RAM[8010]|RAM[8011]|RAM[8012]|RAM[8013]|
|       6 |    -180 |  -18000 |  -18000 |       0 |       3 |   -3000 |       0 |       3 |     181 |     123 |     123 |      27 |   32767 |
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

WBahn
Administrator
So you can see that your code is putting zero in locations 8009 through 8013 (i.e., r[9] through r[13]).

Looking at the Main.jack code, we can see that these lines are:

        let r[9] = Math.sqrt(32767);       // 181
       
        let r[10] = Math.min(345, 123);    // 123
        let r[11] = Math.max(123, -345);   // 123
        let r[12] = Math.abs(27);          // 27
        let r[13] = Math.abs(-32767);      // 32767
 
It's a bit interesting that it's the last five that are wrong, especially since you got r[8], Math.sqrt(9), correct.

I'd recommending first just moving the last five lines up in the .jack file to just after where r is set to 8000 and see if you get the same results.

Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

jfmolderez
Ok : solved .Thanks for the help  
I had indeed to be more careful about the edge case sqrt(32767).
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

alldp19
In reply to this post by WBahn
Your suggestion didn't  work for  me. I too have the same problem. r[9] through r[13] are zero. I can run the square root function by itself  and  get the correct  answer 181 but when I try the math class I get the error Comparison failure at line 2.
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

WBahn
Administrator
As was the case above, I'm not a mind reader. Please post the relevant portion of your .cmp and .out files and let's see what they tell us.
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

alldp19
Thank you for your reply. It's working now. I had to change my square root program a little bit so it could finish the calculation within the given vm steps (1000000) in the MathTest.TST file.  
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

WBahn
Administrator
Sure thing. When you get it changed that might make your problem go away. If not, post the contents of those files and we'll take a look.
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

hugobarroca
In reply to this post by jfmolderez
Replying just in case someone comes here with the same issue I had; Try to do a < x + 1 in place of a <= x, you might end up creating an extra overflow problem which was not anticipated...
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

WBahn
Administrator
Note that a <= x is a syntax error in Jack. All operators are single symbols, which may seem limiting, but your compiler implementation would have been a lot more complicated if that weren't the case.
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

hugobarroca
Yeah, I figured! That's why I tried a < (x + 1) instead, but as x was initialized as 32767, it ended up causing an overflow for me. I ended up going with (a - 1) < x instead. Not sure if it's the best solution given that it can underflow also, but it was good enough to pass the tests anyway.
Reply | Threaded
Open this post in threaded view
|

Re: Comparison failure at line 2

WBahn
Administrator
Think of how we say <=. We say "if a is less than or equal to x".

(a < x) | (a = x)

Another way would be to say if a is not greater than x

! (a > x)

I think Jack uses ! for logical negation. It's been a while.