AnsweredHot!Is it possible to obtain an unsigned result of the difference of two unsigned numbers?

Page: 123 > Showing page 1 of 3 - Powered by APG vNext Trial
Author
DominusT
Super Member
  • Total Posts : 1240
  • Reward points : 0
  • Joined: 2005/07/22 08:31:18
  • Status: offline
2018/06/12 15:33:15 (permalink)
0

Is it possible to obtain an unsigned result of the difference of two unsigned numbers?

Hi.
 
A possibly silly question.

I have two unsigned variables like this (XC32): 
 
unsigned int a;
unsigned int b;

 
When making the difference between the two variables I get a signed result (that is, there may be negative value at some point)
To correct that problem I get the absolute value of the result.
 
My question is:
 
Can I configure XC32, so that the difference of two unsigned variables is also unsigned and thus avoid the user of an absolute design error?
 
Any comment is welcomed.
 
 
#1
qɥb
Monolothic Member
  • Total Posts : 2965
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 15:50:27 (permalink)
+2 (2)
You did not show HOW you are doing the subtraction.
If you just store it back into an unsigned variable, it will do exactly what you want.
If you want an unsigned result stored back into a signed variable, then cast the result before storing it.
 
Note, taking the absolute value of the signed result is NOT the same as forcing an unsigned result.
Maybe you should explain better what you are trying to do.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#2
du00000001
Just Some Member
  • Total Posts : 1967
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 15:58:06 (permalink)
+1 (1)
I still have to get what you intend to achieve: the "distance" between the two or the simple difference?

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#3
simong123
Lab Member No. 003
  • Total Posts : 1251
  • Reward points : 0
  • Joined: 2012/02/07 18:21:03
  • Location: Future Gadget Lab (UK Branch)
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 16:24:41 (permalink)
+1 (1)
Maybe somethin like:-
#define DIFFERENCE(a,b) ((a)>(b)?(a)-(b):(b)-(a))

#4
qɥb
Monolothic Member
  • Total Posts : 2965
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 16:41:51 (permalink)
+1 (1)
I think you're right Simon.
The OP does just want the absolute value of the difference, so your macro will do it correctly using only unsigned arithmetic.
 
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#5
DominusT
Super Member
  • Total Posts : 1240
  • Reward points : 0
  • Joined: 2005/07/22 08:31:18
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 17:57:21 (permalink)
0
Thank you all for responding. What I am doing is measuring the time of a timer like this:


First capture the initial value:
app_uart2.instantValueTMR = getValueTemp();
+


Then I calculate if the elapsed time is greater than the desired
 
 
 
if(abval(getValueTemp() - app_uart2.instantValueTMR)> _300ms){
     sendComm(WAKE_NFC);
}

 
Where app_uart2.instantValueTMR and the value returned by getValueTemp () are unsigned int 
 
 
To avoid negative values, I use the function that obtains the absolute value (abval)
post edited by DominusT - 2018/06/12 18:02:14
#6
qɥb
Monolothic Member
  • Total Posts : 2965
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 18:05:23 (permalink)
+4 (4)
In that case, you SHOULD be using unsigned arithmetic.
Try:
if((unsigned int)(getValueTemp() - app_uart2.instantValueTMR) > _300ms){
     sendComm(WAKE_NFC);
}

 
This will correctly handle the case where the time count rolls over back to zero.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#7
DominusT
Super Member
  • Total Posts : 1240
  • Reward points : 0
  • Joined: 2005/07/22 08:31:18
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 18:11:37 (permalink)
0
qɥb
In that case, you SHOULD be using unsigned arithmetic.
Try:
if((unsigned int)(getValueTemp() - app_uart2.instantValueTMR) > _300ms){
     sendComm(WAKE_NFC);
}

 
This will correctly handle the case where the time count rolls over back to zero.


Initially I did that, but when I discovered that the time obtained was less more than 300 ms, I resorted to the absolute value and it works very well.
I just wanted to know if there was any way to configure the compiler to not use the absolute value function.
post edited by DominusT - 2018/06/12 19:02:11
#8
qɥb
Monolothic Member
  • Total Posts : 2965
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 18:16:43 (permalink)
+1 (1)
DominusT
Initially I did that, but when I discovered that the time obtained was less than 300 ms, I resorted to the absolute value and it works very well.

Huh?
That is doing the calculation correctly.
It will give a result below 300ms until 300ms has elapsed.
Please show an example of the two numbers and the result you got. It sounds like you are not describing your requirement correctly.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#9
DominusT
Super Member
  • Total Posts : 1240
  • Reward points : 0
  • Joined: 2005/07/22 08:31:18
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 19:01:34 (permalink)
0
qɥb
DominusT
Initially I did that, but when I discovered that the time obtained was less than 300 ms, I resorted to the absolute value and it works very well.

 
Huh?
That is doing the calculation correctly.
It will give a result below 300ms until 300ms has elapsed.
Please show an example of the two numbers and the result you got. It sounds like you are not describing your requirement correctly.
 




 
I'm sorry I wanted to say more than 300 ms.
 
 
 
#10
cvm
Senior Member
  • Total Posts : 120
  • Reward points : 0
  • Joined: 2011/09/16 05:16:15
  • Location: 0
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 22:17:42 (permalink)
+2 (2)
with unsigned, you will get the correct difference if the second number is increasing, up to the point it reaches the start number, and the compiler will do unsigned arithmetic on 2 unsigned values- so unless your timer value is decreasing, it will work
 
simple example, imaginary 3bit type to make it simple (values 0-7)-
unsigned 3bit start;
unsigned 3bit end;

start = gettimer(); // (0-7)
wait...
end = gettimer(); // (0-7)
 
our data- assuming start is 4,
all the possible values of end are shown, and the end-start will always be correct
up until end reaches start (get your binary calculator out to check it)
 
start end end-start (unsigned arithmetic)
4     4     0
4     5     1
4     6     2
4     7     3
4     0     4
4     1     5
4     2     6
4     3     7
 
now change the 3bit numbers to 32bit- same-same
#11
1and0
Access is Denied
  • Total Posts : 8129
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 22:38:03 (permalink)
+1 (1)
DominusT
First capture the initial value:
app_uart2.instantValueTMR = getValueTemp();
+

Then I calculate if the elapsed time is greater than the desired  
 </p>
<p>if(abval(getValueTemp() - app_uart2.instantValueTMR)> _300ms){
     sendComm(WAKE_NFC);
}
 Where app_uart2.instantValueTMR and the value returned by getValueTemp () are unsigned int   To avoid negative values, I use the function that obtains the absolute value (abval)

Subtracting two unsigned int's will result in an unsigned int; i.e. there's no negative values. Neither absolute value nor casting is needed. What is _300ms?
#12
du00000001
Just Some Member
  • Total Posts : 1967
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/12 23:33:44 (permalink)
0 (2)
qɥb (some way up) already gave the correct answer: unsigned math handles the rollover perfectly !
So just subtract the start time from the current time (irrespective whether a rollover occurred or not) and test for the result being bigger than your interval. No fancy abs(), no fancy float or whatever: simple plain unsigned integer math.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#13
DominusT
Super Member
  • Total Posts : 1240
  • Reward points : 0
  • Joined: 2005/07/22 08:31:18
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/13 06:00:09 (permalink)
0
1and0
DominusT
First capture the initial value:
app_uart2.instantValueTMR = getValueTemp();
+

Then I calculate if the elapsed time is greater than the desired  
 </p>
<p>if(abval(getValueTemp() - app_uart2.instantValueTMR)> _300ms){
     sendComm(WAKE_NFC);
}
 Where app_uart2.instantValueTMR and the value returned by getValueTemp () are unsigned int   To avoid negative values, I use the function that obtains the absolute value (abval)

Subtracting two unsigned int's will result in an unsigned int; i.e. there's no negative values. Neither absolute value nor casting is needed. What is _300ms?



Suppose that the timer is 16 bits, therefore its value varies from 0x000 to 0xFFFF.
 
Let's suppose that with 5 values of the timer it gets 300 ms, that is to say that if the timer starts at 0, when entering the value of 5, 300 ms has elapsed.
 
But assuming that initial value captured is 0xFFFE then let's make a table
Step |  InstantValue of tmr -           CapturedValue |
0      |  0xFFFE                  -           0xFFFE = 0 <5
1      |   0xFFFF                 -           0xFFFE = +1 <5
2      |   0x0000                 -           0xFFFE = -1 <5
3      |   0x0001                 -           0xFFFE = -2 < 5
4      |   0x0002                 -           0xFFFE = -3 < 5
5      |   0x0003                 -           0xFFFE = -4 <5
6      |   0x0004                 -           0xFFFE = -5 < 5
 
So far, the timer has counted more than 5 steps, which implies that more than 300ms have already passed.
 
To solve that, I tried to do the following:

 
if ((unsigned int) (a -b) > 5) { 
 
 
 
}
 

but it didn't work, I had to resort to a function that extracts the absolute value of a number.
Everything works well.
 
I only want to know if the compiler can be configured so that the result of the difference between two numbers is unsigned.



 
post edited by DominusT - 2018/06/13 06:07:08
#14
1and0
Access is Denied
  • Total Posts : 8129
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/13 07:04:21 (permalink)
+1 (1)
DominusT
Suppose that the timer is 16 bits, therefore its value varies from 0x000 to 0xFFFF.

Then perhaps you should use unsigned short (16-bit) instead of unsigned int (32-bit) for the XC32 compiler.
 
 
But assuming that initial value captured is 0xFFFE then let's make a table
Step |  InstantValue of tmr -           CapturedValue |
0      |  0xFFFE                  -           0xFFFE = 0 <5
1      |   0xFFFF                 -           0xFFFE = +1 <5
2      |   0x0000                 -           0xFFFE = -1 <5
3      |   0x0001                 -           0xFFFE = -2 < 5
4      |   0x0002                 -           0xFFFE = -3 < 5
5      |   0x0003                 -           0xFFFE = -4 <5
6      |   0x0004                 -           0xFFFE = -5 < 5

Like I said before, unsigned subtraction yields an unsigned result; i.e.
 
Step |  InstantValue of tmr - CapturedValue
0    |  0xFFFE              - 0xFFFE =  0  < 5
1    |  0xFFFF              - 0xFFFE =  1  < 5
2    |  0x0000              - 0xFFFE =  2  < 5
3    |  0x0001              - 0xFFFE =  3  < 5
4    |  0x0002              - 0xFFFE =  4  < 5
5    |  0x0003              - 0xFFFE =  5 == 5
6    |  0x0004              - 0xFFFE =  6  > 5
 
 
I only want to know if the compiler can be configured so that the result of the difference between two numbers is unsigned.

There is no configuration, it is standard C. It's the magic of unsigned integers. ;)
 
 
#15
DominusT
Super Member
  • Total Posts : 1240
  • Reward points : 0
  • Joined: 2005/07/22 08:31:18
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/13 07:08:30 (permalink)
0
1and0
Then perhaps you should use unsigned short (16-bit) instead of unsigned int (32-bit) for the XC32 compiler.

 
Only the above is an example, to make it more understandable, I'm actually using the core timer that is 32 bits.
#16
qɥb
Monolothic Member
  • Total Posts : 2965
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/13 07:17:40 (permalink)
+1 (1)
In which case it should  just work.
Show us some numbers where it doesn't.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#17
1and0
Access is Denied
  • Total Posts : 8129
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/13 07:26:11 (permalink)
+1 (1)
DominusT
Only the above is an example, to make it more understandable, I'm actually using the core timer that is 32 bits.

It will still work the same, e.g.
 
Step |  InstantValue of tmr - CapturedValue
0    |  0xFFFFFFFE          - 0xFFFFFFFE =  0  < 5
1    |  0xFFFFFFFF          - 0xFFFFFFFE =  1  < 5
2    |  0x00000000          - 0xFFFFFFFE =  2  < 5
3    |  0x00000001          - 0xFFFFFFFE =  3  < 5
4    |  0x00000002          - 0xFFFFFFFE =  4  < 5
5    |  0x00000003          - 0xFFFFFFFE =  5 == 5
6    |  0x00000004          - 0xFFFFFFFE =  6  > 5
 
#18
DominusT
Super Member
  • Total Posts : 1240
  • Reward points : 0
  • Joined: 2005/07/22 08:31:18
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/13 08:08:26 (permalink)
0
qɥb
In which case it should  just work.
Show us some numbers where it doesn't.
 




I made a video:
 
https://youtu.be/K2aguwUBWIY
 
 
#19
1and0
Access is Denied
  • Total Posts : 8129
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Is it possible to obtain an unsigned result of the difference of two unsigned numbers? 2018/06/13 08:20:49 (permalink)
0
Try
#define _300ms   0x00B71B00uL

#20
Page: 123 > Showing page 1 of 3 - Powered by APG vNext Trial
Jump to:
© 2018 APG vNext Trial Version 4.5