different calculation 20M2 and 20X2

elektriker

New Member
this code calculates different values with 20X2 and 20M2.

w0=65535
w1=1

for b10 = 0 to 11
w1 = w0/w1 + w1/2
sertxd(#b10," ",#w1,cr)
next b10

simulate with Editor 5.5.6 and chip = 20M2
0 0
1 32767
2 16384
3 8193
4 4100
5 2057
6 1044
7 553
8 335
9 265
10 256
11 255

simulate with Editor 5.5.6 and chip = 20X2
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0

why these different values ?

Thank you very much !
 

AllyCat

Senior Member
Hi,

Which simulation do you think is correct? For the expression "w1 = w0/w1 + w1/2"; Is the third w1 the same value as the first or the second w1 ? Strict left to right priority on the whole expression gives a different result to priority across only the right hand side.

There a several "known" issues with the simulators in PE5, but they are not being fixed because it is "obsolete". Those of us who still like to use PE5 accept them as the price to pay for a "fast" User Environment. ;)

Cheers, Alan.
 

Aries

New Member
Given that you are dividing by zero (w1 = 0 after the first pass through the loop), the "correct" result is "infinity", "not-a-number" or "undefined" or a "divide-by-zero" error. I haven't tried the code on actual chips, but it is quite likely that the (firmware in the) chips themselves deals with "divide-by-zero" differently.
 

hippy

Ex-Staff (retired)
I believe I have the answer ... It's all down to what result a division by zero delivers, as Aries correctly guessed -

20M2 : 65535 / 0 -> 65535
20X2 : 65535 / 0 -> 0

The results in simulation are exactly the same as on a physical chip in both cases.

Also; I am wondering if you were you expecting : w1 = w0/w1 + w1/2
to be calculated as : w1 = (w0/w1) + (w1/2)
rather than the left to right : w1 = ( (w0/w1) + w1 ) / 2

If you can define what you expect the result of a division by zero to be you can implement it in a way which works on both 20M2 and 20X2, something like ...
Code:
If w1 > 0 Then
  w1 = w0/w1 + w1/2 
Else
  w1 = ? + w1/2 
End If
 
Top