Author Topic: less than zero value  (Read 4890 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18917
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
less than zero value
« on: November 08, 2016, 03:07:16 pm »
if I carry out a calculation with an unsigned integer and the result is less than 0 will the result default at 0 ? I just want to make sure it does not do anything dunny like roll over and give me something wiered like a larger value than I started with.
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: less than zero value
« Reply #1 on: November 08, 2016, 03:15:39 pm »
if I carry out a calculation with an unsigned integer and the result is less than 0 will the result default at 0 ? I just want to make sure it does not do anything dunny like roll over and give me something wiered like a larger value than I started with.

Presumably you are referring to C? Yes, if the unsigned int overflows / underflows, you will get a large positive number, thus being a careful programmer you need to check that the input value is within the range producing valid results - not the output value as the damage has already be done. However, if you know that the result should be a quite small number compared to the maximum unsigned number, then you should be safe checking the output value.

You can try this assigning a small value to an unsigned integer and subtracting a larger value from it. Print the result and you will find out.
 

Offline StuUK

  • Frequent Contributor
  • **
  • Posts: 390
  • Country: gb
Re: less than zero value
« Reply #2 on: November 08, 2016, 03:16:08 pm »
You may well get wrap around behaviours which lead to non-zero values....
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18917
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: less than zero value
« Reply #3 on: November 08, 2016, 03:19:21 pm »
This is using GCC C on AVR, ok, maybe best I just use a signed number format just in case, it's not a time sensitive program so extra time spent dealing with a larger number format in 8 bit is not a problem.
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 9243
  • Country: de
  • A qualified hobbyist ;)
Re: less than zero value
« Reply #4 on: November 08, 2016, 03:35:49 pm »
You could also check the values before doing the calculation:

Code: [Select]
/* the calculation is "a - b" */
if (b > a)        /* prevent underflow */
{
  a = 0;
}
else              /* no worries */
{
  a -= b;
}
 

Online zapta

  • Super Contributor
  • ***
  • Posts: 6606
  • Country: 00
Re: less than zero value
« Reply #5 on: November 08, 2016, 04:18:17 pm »
This is using GCC C on AVR, ok, maybe best I just use a signed number format just in case, it's not a time sensitive program so extra time spent dealing with a larger number format in 8 bit is not a problem.

What do you want to happen, detect the underflow (overflow) and do something special? Just clip it to zero (255) and not let it underflow (overflow) ?
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: less than zero value
« Reply #6 on: November 08, 2016, 04:41:48 pm »
You may find this document interesting: "Branchfree Saturating Arithmetic"
http://locklessinc.com/articles/sat_arithmetic/
 
The following users thanked this post: laneboysrc

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18917
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: less than zero value
« Reply #7 on: November 08, 2016, 05:55:58 pm »
This is using GCC C on AVR, ok, maybe best I just use a signed number format just in case, it's not a time sensitive program so extra time spent dealing with a larger number format in 8 bit is not a problem.

What do you want to happen, detect the underflow (overflow) and do something special? Just clip it to zero (255) and not let it underflow (overflow) ?

Yes, or being that the range of "underflow" is known I can use a signed type
 

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 9893
  • Country: ca
  • Non-expert
Re: less than zero value
« Reply #8 on: November 08, 2016, 08:53:12 pm »
Some TI DSPs have saturation option, safely avoids overflow issues while preserving speed.
But if you have the cpu cycles available I would check it in code. Then you have the ability to log or flag it for investigation.
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 

Offline Brumby

  • Supporter
  • ****
  • Posts: 12616
  • Country: au
Re: less than zero value
« Reply #9 on: November 09, 2016, 12:03:28 am »
You could also check the values before doing the calculation:

I am of the school that checking for potentially problematic conditions is best done beforehand.  The values you will be working with will be certain and the tests unambiguous.

Trying to identify a problem afterwards means you may well have entered the twilight zone - an area of uncertainty that may vary between platforms.  Also, in an abnormal situation, it is rather unwise to depend on a specific response for guaranteed operation.  If different platforms give different responses, you are not going to have guaranteed operation.  It is much better to deal with potential problems than real ones.

Aside from this, there is also the issue of coding clarity and documentation.  Ii's a lot easier for you (or anybody) who comes along at a later date that looks at the code to read an explicit value check, rather than a verbose description of the 'default behaviour' you are relying on (assuming you would be so disciplined to do so).

In short, it is better programming practice to check your values before using them.

IMHO.
Why Clippy?  --> https://www.youtube.com/watch?v=2_Dtmpe9qaQ
 
The following users thanked this post: 3db

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18917
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: less than zero value
« Reply #10 on: November 09, 2016, 06:42:49 am »
I will just use int_16, a negative value is not a problem as I am doing a subtraction that will produce a negative value so it makes sense to work with signed values rather than try to clip it. int_16 still gives me 32'000 numbers either way and my numbers are in the hundreds so plenty of headroom.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->