Menu

Abs function on word variables

Help
2014-05-28
2014-05-29
  • Jacques Nilo

    Jacques Nilo - 2014-05-28

    I am trying to compute the abs value of word variables. Since the GCB abs funtion only supports integers I wrote the following abs16 function:

    #chip 16F690,8       '
    
    ;Defines (Constants)
    #define LCD_IO 4
    #define LCD_RS PORTB.6
    #define LCD_NO_RW
    #define LCD_Enable PORTB.5
    #define LCD_DB4 PORTC.0
    #define LCD_DB5 PORTC.1
    #define LCD_DB6 PORTC.2
    #define LCD_DB7 PORTC.3
    
    Dim abs16 as word 
    function abs16(xx as word)
    if xx > 0 then
      abs16 = xx
    else
      abs16 = -xx
    end if
    end function
    
    dim cumul1,cumul2 as word
    cumul1=50000
    cumul2=60000
    print abs16(cumul1-cumul2)
    

    The result I get for the print statement is 240 :-(
    Any ideas of the problem ? Any suggestions ?
    Jacques

     
  • Anobium

    Anobium - 2014-05-28

    Please try the following solution. There will be some limits of usage as the ranges of WORD, this can be resolved by using LONG variables where appropriate.

    Anobium

    ~~~~~
    #chip 16F690,8 '

    ;Defines (Constants)
    #define LCD_IO 4
    #define LCD_RS PORTB.6
    #define LCD_NO_RW
    #define LCD_Enable PORTB.5
    #define LCD_DB4 PORTC.0
    #define LCD_DB5 PORTC.1
    #define LCD_DB6 PORTC.2
    #define LCD_DB7 PORTC.3

    dim cumul1,cumul2 as word
    cumul1=50000
    cumul2=60000

    print abs16( cumul1-cumul2 )

    end

    Function Abs16(SysCalcTempA As Word ) As Word
    If SysCalcTempA.15 Then
    Abs16 = -SysCalcTempA
    Else
    Abs16 = SysCalcTempA
    End If
    End Function
    ~~~~

     
  • Hugh Considine

    Hugh Considine - 2014-05-29

    Word variables and integer variables are almost the same, the only difference is that the compiler treats things differently when the highest bit is set. Word variables can hold values between 0 and 65535, so an Abs function would (or should) always return that variable unchanged.

    A better solution here might be a function that can compare the two numbers and return the difference. Something like this:

    ~~~~~~
    '''Calculate the difference between two numbers
    '''@param SysCalcTempA First Value
    '''@param SysCalcTempB Second Value
    Function Difference(SysCalcTempA As Word, SysCalcTempB As Word) As Word
    If SysCalcTempA < SysCalcTempB Then
    Difference = SysCalcTempB - SysCalcTempA
    Else
    Difference = SysCalcTempA - SysCalcTempB
    End If
    End Function

     
  • Jacques Nilo

    Jacques Nilo - 2014-05-29

    Anobium, Hugh
    Thanks a lot for your proposals. They both work ! I definitly would not have found the solution by myself...
    Would not it be worth some comments in the online doc ?

     
  • Jacques Nilo

    Jacques Nilo - 2014-05-29

    A final question: what are the reasons to prefer Hugh's solution ? This is still not quite clear to me ...

     
  • Anobium

    Anobium - 2014-05-29

    Well... :-)

    His function documented. Mine was not. Please laugh.
    Mine will have range limits and will introduce errors. His function will not.
    His function will support with BYTEs and WORDs. Mine will not.

    I would use his function.

    :-)

     

    Last edit: Anobium 2014-05-29

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.