How do you compare signed numbers ?

navkaleel

New Member
I have made an LCD thermometer with an 18X, DS18B20 and a parallel LCD. I am reading the temperature periodically using the READTEMP command in a loop - basically using code modified from the PICAxe manual to read the DS18B20 and display the temperature on the LCD. I handle negative numbers by detecting the presence of a 1 in bit 7 and prefacing the temperature with a minus sign. So far, so good.
I now want to have a MAX and MIN display of the temperature. I want to do this by comparing the present temperature with the previous temperature using an IF..THEN statement and updating variables 'max_temp' and 'min-temp' accordingly. My problem is how to compare signed numbers (sign and magnitude numbers) as produced by the READTEMP statement. For example, if I compare -7 (10000111) with +7 (00000111), the -7 comes out as greater than the +7 as it is a bigger binary number. As I understand it, this is because the comparison operators work on unsigned numbers. How do I compare signed numbers ? Is there some sort of prefix to tell the 18X that we are dealing with a signed number ?
Any help with this will be greatly appreciated.
 

Dippy

Moderator
Surely the easiest-to-undestand way is simply to test for the sign bit and then adjust your arithmetic.
Or, use the sign bit and apply an appropriate offset, then difference is a simple subtraction.
 

Jeremy Leach

Senior Member
I think I'd convert the signed byte temperature to a positive Word value with an offset. Then it would be easy to compare. Something like this ....

Code:
Symbol DisplayTempB = b0
Symbol CurrentTempB = b1 'Byte value to store temperature from ReadTemp command
Symbol CurrentTempW = w1'Word value to store ofsetted temperature value
Symbol MaxTempW = w2
Symbol MinTempW = w3
Symbol DisplayTempW = w4
 
 
'Initialise
MaxTempW = 0
MinTempW = 65535
 
Do
    ReadTemp pin,CurrentTempB
 
   'Convert signed byte temperature to unsigned Word value
   If CurrentTempB > 127 Then
      CurrentTempW = CurrentTempB AND %01111111
      CurrentTempW = 10000 - CurrentTempW 
   Else
      CurrentTempW = 10000 + CurrentTempB
   EndIf
 
   'Update MaxTempW if necessary 
   If CurrentTempW > MaxTempW Then 
      MaxTempW = CurrentTempW
   EndIf
 
   'Update MinTempW if necessary 
   If CurrentTempW < MinTempW Then 
      MinTempW = CurrentTempW
   EndIf
 
   'Display Current Temp
   DisplayTempW = CurrentTempW
   Gosub DisplayTemp
 
   'Display Max Temp
   DisplayTempW = MaxTempW
   Gosub DisplayTemp
 
   'Display Min Temp
   DisplayTempW = MinTempW
   Gosub DisplayTemp
Loop
 
 
DisplayTemp:
   If DisplayTempW < 10000 Then
        Serout ...."-"
        DisplayTempB = 10000 - DisplayTempW
    Else
        DisplayTempB = DisplayTempW - 10000
    EndIf
 
   Serout .... DisplayTempB
Return
 

QuIcK

Senior Member
Are you sure that -7 is 10000111 and not 11111000. is it 1's or 2's compliment? I havent used the temp function yet.

I presume you want -7 to be min and 7 to be max. heres the psuedo code how i would do it.
work out the actual code, its good for you. if you need a nudge, i can post the code i came up with:
Code:
'quick psuedo code:
'if temp is negative:
'if min is negative, check if temp is more negative than min. swap if true
'if min is positive, swap, as temp is negative
'if max is negative, check if temp is less negative than max. swap if true
'if max is positive, no comparison needed.
 
'if temp is positive:
'if min is positive, check if temp is less positive than min. swap if true
'if min is negative, no comparison needed
'if max is negative, swap
'if max is positive, check if temp is more positive than max. swap if true
to get the magnitude of a negative number(1's compliment, where bit7 denotes sign, and b0-6 are non-inverted int.)
b5 = temp & %01111111 ' filter out the modulous

if it is 2's compliment then:
b5 = NOT min 'invert it
b5 = b5 & %01111111 ' filter out the modulous

however, if its 2's compliment, direct comparison should work. just check bit 8 for signage.
with 2's compliment:
-8 < -7
-6 > -7
--------
+ < -
eg 8 < -7

so you need to test negative numbers against positive numbers differently.
 
Last edited:

Dippy

Moderator
Good point re: two's comp. I hadn't checked.

"The temperature data is stored as a 16-bit sign-extended
two’s complement number in the temperature register (see Figure 2). The sign bits (S) indicate if the
temperature is positive or negative: for positive numbers S = 0 and for negative numbers S = 1."
et cetera...
 

navkaleel

New Member
Thank you all for your very prompt replies. You've put a lot of effort into your posts. Thank you. I will try your suggestions and post again later but, for now, here are some thoughts;

Dippy - I'd already tried the offset method. For negative values, I subtracted the modulus from a constant of 55. For positive values, I added it to the 55. This gave me absolute values in the range 0 to 180. Unfortunately, this meant using six variables instead of three as I still have to hang onto the normal values for display purposes. As I have a few other variables too, I ran out of variables.

QuIck and Dippy - I don't think the readings are in two's complement form. The link that eclectic posted seems to confirm that (thanks for that). Dippy, where did you get that quotation from ? My copy of the PICAxe manual does not say that under either 'readtemp' or 'readtemp12'.

QuIck - thank you for that pseudocode. I was struggling to come up with something like that last night but couldn't figure it out.

Jeremy - I like your idea too. In fact, I could probably used that 'word-extension' concept in other code too.
 
Last edited:

hippy

Ex-Staff (retired)
One way to hadle this is to convert temperature readings so $FF=+127, $81=+1, $80=+0, $7F=-1, $01=-127. Thus all temperature readings are in ascending order so normal comparisons can be used.

The value read in from the temperature sensor reading has to be converted to what's to be held in the variables during use, and then converted back for display ...

Code:
#Picaxe 18X
#Terminal 4800
#No_Data

Symbol DS18B20_PIN = 0

Symbol curTemp = b1
Symbol minTemp = b2
Symbol maxTemp = b3

Gosub ReadTempToB0
curTemp = b0
minTemp = b0
maxTemp = b0

Do
  SerTxd(  "Cur=" ) : b0 = curTemp : Gosub DisplayTempFromB0
  SerTxd( " Min=" ) : b0 = minTemp : Gosub DisplayTempFromB0
  SerTxd( " Max=" ) : b0 = maxTemp : Gosub DisplayTempFromB0
  SerTxd( CR,LF )
  Pause 1000
  Gosub ReadTempToB0
  curTemp = b0
  If b0 < minTemp Then
    minTemp = b0
  End If
  If b0 > maxTemp Then
    maxTemp = b0
  End If
Loop

ReadTempToB0:
  ReadTemp DS18B20_PIN, b0
  If bit7 = 0 Then
    bit7 = 1
  Else
    b0 = - b0
  End If
  Return
  
DisplayTempFromB0:
  If bit7 = 1 Then
    SerTxd( "+" )
    bit7 = 0
  Else
    SerTxd( "-" )
    b0 = $80 - b0
  End If
  SerTxd( #b0, "C" )
  Return
 

QuIcK

Senior Member
sneaky, hippy. nicely done, and very compact. nice way of implementing the offset method. better than testing each condition.
 

Dippy

Moderator
I got the quote from the DS18B20 Dallas Data Sheet from maxim-ic.com . Page 4.
I always thought a simple offset would do it, but was put into confused mode when I read the Data Sheet.
Anyway, all sorted apparently. Its too hot to think :)
 

navkaleel

New Member
quIck - I've coded the pseudocode you gave me. I hope I am on the right lines.

Code:
'if temp is negative:
if Temp > 127 then 
	'if min is negative, check if temp is more negative than min. swap if true
	if Min > 127 then
		if Temp > Min then
			swap Temp, Min
		endif
	endif
	'if min is positive, swap, as temp is negative
	if Min <= 127 then
		swap Temp, Min
	endif
	'if max is negative, check if temp is less negative than max. swap if true
	if Max > 127 then
		if Temp < Max then
			swap Temp, Max
		endif
	endif
	'if max is positive, no comparison needed.
endif

'if temp is positive:
if Temp <= 127 then
	'if min is positive, check if temp is less positive than min. swap if true
	if Min <= 127 then
		if Temp < Min then
			swap Temp, Min
		endif
	endif
	'if min is negative, no comparison needed
	'if max is negative, swap
	if Max > 127 then
		swap Temp, Max
	endif
	'if max is positive, check if temp is more positive than max. swap if true
	if Max <= 127 then
		if Temp > Max then
			swap Temp, Max
		endif
	endif
endif
Unfortunately, when I try to program this, I get loads of syntax errors on the swap statements. Maybe it is a new statement and I need a newer copy of the Program Editor. I will download it and try again. Thanks very much for your help.
 

navkaleel

New Member
Damn ! I've just figured out why swap doesn't compile. It's not available on the 18X. Okay, let's try again. It'll cost me another variable, I guess.
 

QuIcK

Senior Member
hmm, its not showing all the code.
you dont need to swap them. i forgot that was a command :rolleyes:
just do "min = temp" instead of "swap temp, min" or whatever
you dont want the previous minimum ending up in the temp variable.

you will need to compare the modulous (magnitude) of the current temp with whats in the min & max.
ie, without bit7
this is what I came up with:
Code:
gosub get_curr_temp
if temp > 127 then
 b4 = curr_temp & %011111111 ' filter out the modulous
 b5 = min & %01111111 ' filter out the modulous
 if min > 127 then 'min is negative
  if b4 > b5 then 'curr_temp is less than min
   min = curr_temp
  end if
 else 'min is positive, so curr_temp will be less than
  min = curr_temp
 end if
 b5 = max & %01111111 ' filter out the modulous 
 if max > 127 then 'if max is neg
  if b4 < b5 then 'if curr_temp > max (negative numbers, so its less negative, not greater than)
   max = curr_temp
  end if
 end if
else
 if min =< 127 then 'if min is positive
  if curr_temp < min then 'see if curr_temp is less negative
   min = curr_temp
  end if
 end if 
 if max > 127 then 'if max is negative, curr_temp is greater than
  max = curr_temp
 else
  if curr_temp > max then 'max is positive. if curr_temp is greater than max.
   max = curr_temp
  end if
 end if
end if
hippy's method will work just as well, tho. and with less variables.

basically add $80 to it. if its a negative number, it will make it positive and less than $80. if its a positive number, then it will make it greater than $80.
then you can directly compare:
Code:
curr_temp = curr_temp + $80
if curr_temp < min then
     min = curr_temp
end if
if curr_temp > max then
     max = curr_temp
end if
to output, check if its less than $80 for the sign:
Code:
if curr_temp < $80 then
     'negative number, display a -
else
     'positive number
end if
then to display the number, or get the magnitude, just subtract $80

Code:
curr_temp = curr_temp - $80
min = min - $80 'etc
when you subtract the $80, you will loose the sign! so mayb:
Code:
if curr_temp < $80 then
    curr_temp = curr_temp - $80
    curr_temp = curr_temp OR %10000000 'set bit7 on.
end if
which would return curr_temp to its original state.
 

navkaleel

New Member
Quick and hippy - thank you both.
QuIck - you said " basically add $80 to it. if its a negative number, it will make it positive and less than $80. if its a positive number, then it will make it greater than $80. "
I'm a bit confused by that. I can see how adding $80 to a negative number will make it positive but I cannot see how adding $80 to a positive number will make it greater. Wouldn't it just make the positive number a negative one by setting bit7 to a 1 ? Sorry if I am being a bit thick.

On a related point; does the 'readtemp' command represent 0 Celsius as a positive number (00000000) or a negative number (10000000) ? I suppose I could find out myself but I haven't got an ice bucket handy. I wish I did though as it's been a hot day ! :)
 

SilentScreamer

Senior Member
$80 is hexadecimal for 128 which is the what the 7th bit represents.

%00000000 + 128 = %10000000
%10000000 + 128 = %100000000 which with a byte variable would overflow to %00000000

Sorry if that still doesn't explain it, I'm sure someone else will be able to explain it better.
 

lbenson

Senior Member
>Wouldn't it [adding $80] just make the positive number a negative one by setting bit7 to a 1

The picaxe does not deal with negative numbers, so the negative degrees Centigrade from the DS18B20 are handled--as a convention--as the 7-bit number of degrees below 0 "flagged" with a bit7 of 1. So %10000001 is minus 1 and %10000011 is minus 3.

If you look in the Windows calculator at -1 in binary, you will see that it is %1111111 (carried to howevermany bits), not %10000001.

So adding $80 (%10000000) doesn't make a positive temperature reading negative, it just shifts it up by 128 (never overflowing because the highest permissible positive reading is 127). Likewise, negating a temperature reading which has bit7=1 results in the most negative temperatures being closest to 0, and the least negative being closer to 128.

Zero degrees C will be read as %00000000.
 
Last edited:

QuIcK

Senior Member
This is long. Ive had a few drinks, so ill amend (delete) this in the morning! (edit, re-reading your post, read my next post. ill leave this in for those interested...)

Its all about integer maths, and knowing when to overflow it.
i dont know how much you know...
an integer is a whole number (no decimals or fractions).
in basic binary, there has to be a method for denoting positive and negative numbers. this obviously takes 1 bit.
in normal pic integer maths, you have 0->255 range for an 8 bit variable. a word is 0-65535. but, as far as the processor is concerned, that is positive.
so to represent a negative integer on a pic processor, you loose 1 bit of accuracy... as you do not get 9 or 17 bit variables.
so, to describe a negative number, bit7 is used for an 8bit int, and bit 15 for a 16bit word (the last bit. remember there is a bit0).

now, there are 2 kinds of negative representation. 1 lends its self to easy understanding, and translating (perhaps even comparison, and other logic). whilst the other lends itself to addition and subtraction, and other maths.

1's compliment is the first kind.
basically, you describe the magnitude of the integer (, ie 20, 100, 42, etc) with 7 bits (bit0->bit6) and use the 8th bit (bit7) to describe the sign. this makes it easy for us humans (well, easy-ish) to understand. magnitude, and sign.
this means, that you can use all 8 bits if you are only ever using positive numbers. 1's compliment is only used for negative/positive numbers. this means compatibility needs to be maintained, ie a positive number is unaffected.
if you remove bit7, then you have the size of the bit. you can test the sign-bit, display the magnitude, etc.
the cool thing is: you can add 128, and you can have an off-set range which makes it easy to compare.
so, some examples:
120 = %01111000.
-120 = %11111000 (as far as the processor is concerned, thats 248)
so for that, direct comparison doesnt work:
-120 > 120 as far as the processor is concerned.
but, it is easy to understand.
now, you can offset the zero, by adding $80. this makes a "relative" 0 (at 128). the processor doesnt care where zero is. it will overflow variables as and when it needs to. you could have 255 as your zero, and remember when you output to add 1 (ie, when variable = 0, thats relative to 255, ie actually +1).
so, offset examples:
%01111000 (120)
+ %10000000 (128 = $80)
= %1111100 (248)

...

%11111000 (-120)
+ %10000000 (128 = $80)
=%101111000 (504)
now, a variable on a pic is only 8bits. so, when presented with 9bits, it "overflows" and truncates the most significant bit... ie the bit that has the highest value.
for the 9bit value above, a pic would return %01111000.
this has essentially made positive numbers negative, and negative numbers positive (if you didnt know that $80 has been added on).
the other way of looking at it is: 128($80) has become your "zero". so you have biased, or offset, the mathematics. now, this is complicated for humans to follow, however it makes binary comparison very easy:
%10000001 (129 (offset math +1))
%01111111 (127 (offset math -1))

for a integer comparison, 127 < 129 (offset math -1 < +1)
whereas before, -120 > 120 (no offset)

1's compliment, however, makes maths a pain:
%11111000 (-120)
+ %00000001 (1)
= %11111001(-121, whereas -120 + 1 should be -119)

This is where 2's compliment is better.
2's compliment makes more sense for negative numbers. its not easier to understand, but definately makes more sense.
a positive number is as normal.
when a number goes negative, it does a sort of reverse overflow.
this is complicated.
1) invert the bits.
ie:
%01111000 (120) will be our example
becomes:
%10000111 (135)... you can see bit7 represents sign again.
2) Add 1
%10000111 (135)
+ %00000001
= %10001000 (136)

now, again direct comparison doesnt work... -120 > 120.
but, filter out bit7, and you get: 120(+120) > 8 (-120, described by 7 bits)
2's compliment is essentially like 1's compliment, with the offset. however, positive numbers run from 0 upwards, meaning positive-only numbers can be described with 8bits.
negative mathematics works as well:
%10001000 (135, 2's comp -120)
+ %00000001
= %10001001 (136)
not obvious, but reverse the steps...
subtract 1:
%10001000 (135)
invert the bits:
%01110111 (119)
this means that -120 + 1 = -119!

and thats me about to fall asleep.
I hope this helps. do ask questions, and correct/update me where Im wrong.
 
Last edited:

QuIcK

Senior Member
Quick and hippy - thank you both.
QuIck - you said " basically add $80 to it. if its a negative number, it will make it positive and less than $80. if its a positive number, then it will make it greater than $80. "
I'm a bit confused by that. I can see how adding $80 to a negative number will make it positive but I cannot see how adding $80 to a positive number will make it greater. Wouldn't it just make the positive number a negative one by setting bit7 to a 1 ? Sorry if I am being a bit thick.
a pic doesnt know negative numbers. %11111111 can be 255 or -127 to us humans, but a pic will only see that as 255. the difference is we know the context, so we write the program to deal with only positive numbers, or positive and negative numbers.

also, 2 Zeros is a problem of 1's compliment, a positive and a negative.
 

Jeremy Leach

Senior Member
Also ...
Code:
If b0 < minTemp Then
    minTemp = b0
Endif
Can be written
Code:
Mintemp = Mintemp MAX b0
And in a similar way
Code:
MaxTemp = MaxTemp MIN b0
For example:
Code:
b0 = 5, MinTemp = 7
Mintemp = 7 MAX 5 = 5
Takes a bit of getting your head around.
 

navkaleel

New Member
Thank you all for your responses, especially to QuIcK for his detailed and informative post - all the more admirable for the fact that he'd had a couple of drinks ! Actually, I tend to find that a drink or two makes my thoughts more lucid. :)

I have some previous knowledge of binary arithmetic, logical operators, two's-complement, sign & modulus representation and so on (though I've got a bit rusty recently) but until yesterday I didn't realise how little I knew ! Some of the stuff I've read in this thread in the last couple of days is pretty heavy-duty. Even if I get this project working, I plan on going back and slowly re-reading the previous posts until it all sinks in properly. It would be useful to have a good understanding of it for future projects.

Back to this project, I have written some code based on the pseudo-code QuIcK kindly provided me. I've taken on board the need to filter out the modulus of the number and use the modulus to make comparisons. My code now works .... partially !

I've tested it by removing the DS18B20 and assigning values to the 'Temp' variable using a FOR..NEXT loop. That way I can make the value of 'Temp' sweep through the whole range from -55 to +125. On the LCD I have a display of the 'Temperature', Maximum and Minimum as I sweep through the range. What I found is that the Maximum appears to work okay. The Minimum works okay in the positive range but not in the negative range.

As 'Temp' reduces into the negative range and becomes progressively more negative, 'Minimum' follows suit. When the 'Temp' starts to rise from a very negative value to less and less negative values on its way to Zero, 'Minimum' follows along rather than staying at the most negative temperature.
By the way, I have a suspicion that 'Maximum' does this too when in the negative range. I need to test it by making 'Temp' rise through the negative range and then peak and fall whilst still in the negative range. It is only when a value peaks that problems become apparent, it seems. I haven't done that yet.

I have tried various fixes for this 'Minimum' problem and spent hours over it but cannot, for the life of me, figure out what I've done wrong ! I've attached the relevant bits of code below in case QuIcK or anyone else would like to have a look and tell me what I am doing wrong.

Thanks in advance.

Code:
' Variables
symbol Temp = b6	             ' The current temperature
symbol Maximum = b8	' Maximum temperature
symbol Minimum = b9	' Minimum temperature
symbol MagTemp = b10	' Magnitude (modulous) of Temp
symbol MagMax = b11	' Magnitude (modulous) of Maximum
symbol MagMin = b12	' Magnitude (modulous) of Minimum


'Initialise the Maximum and Minimum variables before use.
'Maximum is initially set to the lowest temperature
'Minimum is initially set to the highest temperature
Maximum = %10110111	'-55	--- The coldest temperature a DS18B20 can measure
Minimum = %01111101	' 125	--- The hottest temperature a DS18B20 can measure





CalcMaxMin:
' MagTemp is the magnitude (modulus) of the Temp variable
' MagMax is the magnitude (modulus) of the Maximum variable
' MagMin is the magnitude (modulus) of the Minimum variable
MagTemp = Temp & %011111111	' filter out the modulous
MagMax  = Maximum  & %01111111 	' filter out the modulous
MagMin  = Minimum  & %01111111 	' filter out the modulous

' ---------------- Maximum --------------------

' if Temperature is negative
if Temp > 127 then
	'if Maximum is negative
	if Maximum > 127 then
		' if MagTemp is smaller than MagMax
		if MagTemp < MagMax then
			Maximum=Temp
		endif
	endif	
	'if Maximum is positive - Maximum unaffected
endif	
	
' if Temperature is positive
if Temp <= 127 then
	'if Maximum is negative
	if Maximum > 127 then
		Maximum = Temp
	endif
	'if Maximum is positive
	if Maximum <= 127 then
		'if MagTemp is larger than MagMax
		if MagTemp > MagMax then
			Maximum = Temp
		endif
	endif
endif

' ---------------- Minimum --------------------

' if Temperature is negative
if Temp > 127 then
	'if Minimum is negative
	if Minimum > 127 then
		'if MagTemp is larger than MagMin
		if MagTemp > MagMin then
			Minimum = Temp
		endif
	endif
	'if Minimum is positive
	if Minimum <= 127 then
		Minimum = Temp
	endif
endif

' if Temperature is positive
if Temp <= 127 then
	'if Minimum is negative - Minimum unaffected
	'if Minimum is positive
	if Minimum <= 127 then
		if MagTemp < MagMin then
			Minimum = Temp
		endif
	endif
endif

return
 

Jeremy Leach

Senior Member
Have a look at Hippy's 'adding 128($80)' code, that seemed a bit simpler :confused:

Out of interest, I've come up with this little formula to directly convert the temp (in b0) into a value offset by 128 ...
Code:
b0 = 2 * b0 - 128 * bit7 + 128 - b0
If bit7 is 1 (temp is negative) then the 2 * b0 - 128 part of the formula applies, therefore b0 = 2*b0 - 128 + 128 - b0 = b0
If bit7 is 0 (temp is positive) then b0 = 128 - b0

Because negative numbers are stored as 128 + T (because of the sign bit), then this convertion translates negative temperatures to 128 + T and positive temperatures to 128 - T. Even though this is 'upside down' it would still allow direct comparison of Max and Min values if they had been stored in this format.

I'm not saying this method is better, but I was just seeing if I could get the conversion to a useful format onto one line !
 
Last edited:

Jeremy Leach

Senior Member
I've had another idea, which makes it really easy and doesn't require converting to any other format ...

Minimum
Code:
If MinTemp > 127 OR CurrentTemp > 127 Then
    MinTemp = MinTemp MIN CurrentTemp
Else
    MinTemp = MinTemp MAX CurrentTemp
I've got to this code by looking at 4 scenarios:
1. (MinTemp is +ve) AND (CurrentTemp is +ve)
The new MinTemp is the least of these two values. Which can be written: MinTemp =MinTemp MAX CurrentTemp.

2. (MinTemp is +ve) AND (CurrentTemp is -ve)
The new MinTemp = CurrentTemp. And because Current Temp value > =128, then the new MinTemp is also the greater of these two values. Which can be written: MinTemp =MinTemp MIN CurrentTemp.

3. (MinTemp is -ve) AND (CurrentTemp is +ve)
The new MinTemp = MinTemp. And because MinTemp value >=128, then the new MinTemp is also the greater of these two values. Which can be written: MinTemp =MinTemp MIN CurrentTemp.

4. (MinTemp is -ve) AND (CurrentTemp is -ve)
The new MinTemp is the greater of these two values. Which can be written: MinTemp =MinTemp MIN CurrentTemp

Another way of putting this is : "if either CurrentTemp or MinTemp are negative then the new MinTemp = the greater of the two values, otherwise the new MinTemp = the least of the two values."

Maximum
In a very similar way ...
Code:
If MaxTemp >127 OR CurrentTemp > 127 Then
    MaxTemp = MaxTemp MAX CurrentTemp
Else
    MaxTemp = MaxTemp MIN CurrentTemp
So this is all you need, to set the min and max, as far as I can see.
 
Last edited:

navkaleel

New Member
Jeremy - Thanks for that. That is some pretty artful stuff there. I am still trying to get my head around the earlier of your two posts. I can never tell for sure wether I understand this sort of thing until I have drawn out an imaginary byte full of ones and zeros. If I can understand it, it'll make my programs a bit more sophiscated, and probably also smaller. If I cannot understand it, I may have to ask you more questions :)
This forum is a very valuable resource.
Cheers,
Nav
 

Jeremy Leach

Senior Member
Well, regarding my last post, just run through each scenario logically. Remember that for a negative temperature value the most significant bit of the byte is set to 1. This bit adds 128 onto the 'magnitude' of the temperature.

The MIN and MAX operators are a bit tricky to get your head around and do cause a lot of confusion:

Take an example of A MIN B, this means you're imposing a minimum value of B. So if A = 2 and B = 5 then you are imposing a minimum value of 5. A is less than 5, so the answer's 5.

A MAX B imposes a maximum value of B. So if A = 5 and B = 2 then you are imposing a maximum value of 2, so the answer's 2.

The minimum value of A and B = A MAX B
The maximum value of A and B = A MIN B
This sounds illogical but it isn't when you walk it through, honest.

For instance, to get the maximum of 2 and 5, 2 MIN 5 = 5 MIN 2 = 5

I've found MIN and MAX to be really useful, once I'd got my head round them ;)
 
Last edited:

QuIcK

Senior Member
ive used max and min a couple of times in arithmetic ops. eg
b0 = b1 + b2 MAX 127 (ie, stop an 8bit signed overflow)
it kept overflowing, unfortunately. especially negative arithmatic. trying to keep a number between 0 & 127 (or 128 & 255) wen doing arithmatic can only reliabley be done with comparision.
eg:
if b1 < b2 then
b0 = b2 - b1

in my humble experince, that is. that was for a PID controller, and the position kept on jumping randomly. i changed all my max & min to appropriate IF statements, and it worked fine.
mayb i hadnt thought out the logic of my max&mins correctly, but i dont really trust them :rolleyes:
 

hippy

Ex-Staff (retired)
ive used max and min a couple of times in arithmetic ops. eg
b0 = b1 + b2 MAX 127 (ie, stop an 8bit signed overflow) it kept overflowing, unfortunately
That can happen, that's because the PICAXE doesn't use signed numbers and MAX ( and MIN ) only applies to the numbers the PICAXE understands not what you choose them to mean.

Even if you are using comparisons you can run into problems. If using two's complement representation; -1 is greater than 1 from the PICAXE's perspective.

Once you use any number representation other than positive only you have to take a lot of care, think things through and test thoroughly.
 
Top