subtracting larger number from smaller and get a minus result

Started by Dave-S, Nov 22, 2023, 06:51 PM

Previous topic - Next topic

Dave-S

Subtracting a larger number from smaller and get a minus result.
ie: 12 - 15 should get -3, but get 65533

      Dim value4 As SWord
      Dim value5 As SWord
      Dim valueT As SWord
      value4 = 12
      value5 = 15
      valueT = value4 - value5

      result = 65533   exspected    - 3


How can you get a minus result?

Dompie

Have you used the "sdec" modifier?

HRsoutLn SDec valueT

Johan

Dave-S


John Drew

Just by way of explanation that others might find useful.
Byte numbers are 0 to 255
Word numbers are from 0 to 65535
These are all positive numbers, negative numbers do not exist in memory.
So 12-15 rolls under to 65533 in Word.

By using a signed word and some Les magic if you use sWord, Positron assigns the top half of integer numbers to represent negative numbers. This is why there was an answer of 65533 (2 down from 0) when using Word but -2 if using sWord.
 The key point is that in memory the number is stored as 65533 but interpreted as -2

You'll notice that signed numbers can only be half the size in a +ve direction and half size in the negative direction so the value can fit into an integer's space.

Even floats only exist as positive numbers in memory, they are made up of positive bits that instruct the compiler how to interpret the 4 bytes as negative or positive decimal values.
I hope this helps.
Cheers
John


top204

You are correct tumbleweed, but I tend to steer away from wikipedia because the information on there has a tendency to be stupidly complex and only fit for people who already know, or it is often "wrong".

I have looked on wikipedia a few times for information and all that is there is a mass of complex calculations that mask, even, simple processes and lots of words with very little 'actual' meaning, a bit like a lot of the people who write on it. :-)