Math problem


Closed Thread
Results 1 to 8 of 8

Thread: Math problem

  1. #1
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102

    Default Math problem

    Dear reader,

    I gave the chip these tasks to calculate

    All variables are WORD

    Code:
        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

    Code:
    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 ?

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by selbstdual View Post
    Dear reader,

    I gave the chip these tasks to calculate

    All variables are WORD

    Code:
        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

    Code:
    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.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Code:
    Result=951-1423
    if result.15=1 then
        ' do the negative stuf here
        ELSE
            ' do the positive stuff here
        ENDIF
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102


    Did you find this post helpful? Yes | No

    Default

    I changed the code to

    Code:
    	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
    Last edited by selbstdual; - 18th February 2007 at 20:20.

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by selbstdual View Post
    I changed the code to

    Code:
    	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.

  6. #6
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102


    Did you find this post helpful? Yes | No

    Default

    So you are talking about page 35. Lets see.

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by selbstdual View Post
    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.

  8. #8
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102


    Did you find this post helpful? Yes | No

    Default

    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.
    Last edited by selbstdual; - 18th February 2007 at 21:31.

Similar Threads

  1. Line Graph, math problem...
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th May 2009, 04:20
  2. Math problem with string result
    By Lotondo in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 15th November 2006, 10:06
  3. PicBasic Pro Math Problem??
    By Glen65 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th March 2006, 04:36
  4. Math Formula / Variable Problem! Help!
    By bwarp in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 18th February 2006, 07:59
  5. Math problem...with picbasic pro
    By pcaccia in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2005, 19:28

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts