Now that i learned a little bit more about math operation with byte and word variables, i think that i understand how to do this.
First i have to say that the "signed_math" functions i posted are no needed, so i think that the posts about that should be deleted by some admin to aviod confusing people (Kent: if you can, do it please).
The proper way to manage this is using word variables and testing the 7th bit of high byte, as Kent suggested (and i dind't understand in that moment). The only thing is that we have to be sure that the result will keep betwen -32767 and +32767.
Or using Byte variables for values betwen -127 and +127.
Then it can work this way:
________________________________________________
dim first_var as word
dim second_var as word
dim result as word
result = first_var + second_var
________________________________________________
'To have the absolute value of result:
dim result_abs as word
if result_H.7 on then 'if result is a negative value
result_abs = 65535 - result + 1
else
result_abs = result
_________________________________________________
GCBasic can also accept "integer" variables, but i'm not sure how they work, perhaps someone that know it could tell us something about this.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Integer variables in GCBASIC are word variables that the compiler will treat in a special way. They're stored as 16 bit two's complement, which gives a range of -32768 to 32767. At present, there are routines for negate, multiply and divide on integers built in to the compiler. A nice feature of two's complement integers is that they can use the word routines for add, subtract, equal and not equal.
There are still no integer operations for less than and less than or equal - the compiler is already altered, I just haven't yet gotten around to writing the SysCompLessThanInt orSysCompLessOrEqualInt routines in system.h. There may also be a few bugs, hardly any testing has been done on integers.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Then if i undertand, integer variables are just word variables, but using the absolute value, and bit7 of high byte as sign??, something like this:
________________________________________________
if result_H.7 on then 'if result is a negative value
result = 65535 - result + 1
result_H.7 = 1
else
result = result
_________________________________________________
Anyway i know this is not a GCBasic specific cuestion... so i will look for information about this in the web.
I'm happy to know that integers are near to be full supported by GCBasic. Let me know if you want me to do some specific testing.
Greetings.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Now that i learned a little bit more about math operation with byte and word variables, i think that i understand how to do this.
First i have to say that the "signed_math" functions i posted are no needed, so i think that the posts about that should be deleted by some admin to aviod confusing people (Kent: if you can, do it please).
The proper way to manage this is using word variables and testing the 7th bit of high byte, as Kent suggested (and i dind't understand in that moment). The only thing is that we have to be sure that the result will keep betwen -32767 and +32767.
Or using Byte variables for values betwen -127 and +127.
Then it can work this way:
________________________________________________
dim first_var as word
dim second_var as word
dim result as word
first_var = 10
second_var = -1200 'GCBasic accepts negative values
result = first_var + second_var
________________________________________________
'To have the absolute value of result:
dim result_abs as word
if result_H.7 on then 'if result is a negative value
result_abs = 65535 - result + 1
else
result_abs = result
_________________________________________________
GCBasic can also accept "integer" variables, but i'm not sure how they work, perhaps someone that know it could tell us something about this.
Ok... it's easier than that.
Just do this:
'To have the absolute value of result:
dim result_abs as word
if result_H.7 on then 'if result is a negative value
result_abs = - result
else
result_abs = result
Integer variables in GCBASIC are word variables that the compiler will treat in a special way. They're stored as 16 bit two's complement, which gives a range of -32768 to 32767. At present, there are routines for negate, multiply and divide on integers built in to the compiler. A nice feature of two's complement integers is that they can use the word routines for add, subtract, equal and not equal.
There are still no integer operations for less than and less than or equal - the compiler is already altered, I just haven't yet gotten around to writing the SysCompLessThanInt orSysCompLessOrEqualInt routines in system.h. There may also be a few bugs, hardly any testing has been done on integers.
Hi Hugh... thanks for the explanation.
Then if i undertand, integer variables are just word variables, but using the absolute value, and bit7 of high byte as sign??, something like this:
________________________________________________
if result_H.7 on then 'if result is a negative value
result = 65535 - result + 1
result_H.7 = 1
else
result = result
_________________________________________________
Anyway i know this is not a GCBasic specific cuestion... so i will look for information about this in the web.
I'm happy to know that integers are near to be full supported by GCBasic. Let me know if you want me to do some specific testing.
Greetings.