PDA

View Full Version : Math problem



selbstdual
- 18th February 2007, 18:54
Dear reader,

I gave the chip these tasks to calculate

All variables are WORD




IF ((Second_Value - First_Value) < 0) THEN
Difference_Between_F_And_S_Value = Second_Value - First_Value
Difference_Between_S_And_T_Value = Third_Value - Second_Value
ELSE
Difference_Between_F_And_S_Value = First_Value - Second_Value
Difference_Between_S_And_T_Value = Second_Value - Third_Value
ENDIF

Difference_Between_Two_Points_As_Reference = Point_Two - Point_One

Steps_Between_F_And_S_Value = (752 * Difference_Between_F_And_S_Value) / Difference_Between_Two_Points_As_Reference
Steps_Between_S_And_T_Value = (752 * Difference_Between_S_And_T_Value) / Difference_Between_Two_Points_As_Reference


These are the values being used



First_Value 1423
Second_Value 951
Third_Value 931
Point_One 493
Point_Two 1702

Result:
Steps_Between_F_And_S_Value 22
Steps_Between_S_And_T_Value 12


According to my hand calculator and the PC's calculator Steps_Between_F_And_S_Value and Steps_Between_S_And_T_Value contain wrong results. What could be the reason for that ?

skimask
- 18th February 2007, 19:06
Dear reader,

I gave the chip these tasks to calculate

All variables are WORD




IF ((Second_Value - First_Value) < 0) THEN
Difference_Between_F_And_S_Value = Second_Value - First_Value
Difference_Between_S_And_T_Value = Third_Value - Second_Value
ELSE
Difference_Between_F_And_S_Value = First_Value - Second_Value
Difference_Between_S_And_T_Value = Second_Value - Third_Value
ENDIF

Difference_Between_Two_Points_As_Reference = Point_Two - Point_One

Steps_Between_F_And_S_Value = (752 * Difference_Between_F_And_S_Value) / Difference_Between_Two_Points_As_Reference
Steps_Between_S_And_T_Value = (752 * Difference_Between_S_And_T_Value) / Difference_Between_Two_Points_As_Reference


These are the values being used



First_Value 1423
Second_Value 951
Third_Value 931
Point_One 493
Point_Two 1702

Result:
Steps_Between_F_And_S_Value 22
Steps_Between_S_And_T_Value 12


According to my hand calculator and the PC's calculator Steps_Between_F_And_S_Value and Steps_Between_S_And_T_Value contain wrong results. What could be the reason for that ?

Your answer, again, is in the PBP manual, page 31.

mister_e
- 18th February 2007, 19:16
PBP work with un-signed integer

Second-First=951-1423=-472

-472 will become 65064, wich is actually 65536 -472

if you want to test if your result is negative, you could still test the bit15 of the substraction


Result=951-1423
if result.15=1 then
' do the negative stuf here
ELSE
' do the positive stuff here
ENDIF

selbstdual
- 18th February 2007, 20:15
I changed the code to



Second_First = Second_Value - First_Value


IF Second_First.15=1 THEN
Difference_First_Second = First - Second
Difference_Third_Second = Third - Second
ELSE
Difference_First_Second = Second - First
Difference_Third_Second = Second - Third
ENDIF

Result:

Is Should
First 1386 1386
Second 771 771
Third 882 882
Point_One 449 449
Point_Two 1663 1663
Steps_First_Second 3 380
Steps_Third_Second 14 68


Calculation: Value stored by WRITE:

Value1 Value2
Byte0 Byte1

Value(Like First, Second etc.) = Byte1*256 + Byte0

skimask
- 18th February 2007, 20:25
I changed the code to



Second_First = Second_Value - First_Value


IF Second_First.15=1 THEN
Difference_First_Second = First - Second
Difference_Third_Second = Third - Second
ELSE
Difference_First_Second = Second - First
Difference_Third_Second = Second - Third
ENDIF

Result:

Is Should
First 1386 1386
Second 771 771
Third 882 882
Point_One 449 449
Point_Two 1663 1663
Steps_First_Second 3 380
Steps_Third_Second 14 68


Calculation: Value stored by WRITE:

Value1 Value2
Byte0 Byte1

Value(Like First, Second etc.) = Byte1*256 + Byte0

And again, we've had this conversation before. You're overflowing the limits of your variables. For instance:

x var word
y var word
z var word
x = 1000
y = 1000
z = x * y

You'd expect z to equal 1,000,000, but it work, because it's a word, 16 bits, a word and only a word, 16 bits, nothing more, nothing less. The answer in this case is not 1,000,000, but 16,960. You've either got to learn how to do 32 bit math (which is explained in the PBP manual and will work just fine), or cut down your input numbers a bit to keep them from overflowing the results.

selbstdual
- 18th February 2007, 20:56
So you are talking about page 35. Lets see.

skimask
- 18th February 2007, 21:15
So you are talking about page 35. Lets see.

Just take your test numbers and put them all below 255 and try the math. Assuming you've shown us all of your code and not just a small chunk, it should work.

selbstdual
- 18th February 2007, 21:27
It does work.

But this is a huge effort for dividing and multiplying.

I want to have full 32-Bit or better 64-Bit calculation in the next version.

It has to be mentioned that it is much easier than assembler also.