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.
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
The result I get for the print statement is 240 :-(
Any ideas of the problem ? Any suggestions ?
Jacques
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
~~~~
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
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 ?
A final question: what are the reasons to prefer Hugh's solution ? This is still not quite clear to me ...
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