0

Situation:

I've a system which reads a continuous stream of numbers (integers only).

  • The numbers are both positive and negative.
  • Some of the numbers are results of overflowed arithmetic operations: so these numbers will be negative ones in the stream.

Problem:

How can I differentiate between overflowed negative numbers and non-overflowed negative number in a stream? Is there a way to find and discard overflowed numbers? I'm developing in C# and don't have control over the stream source. So I can't change the code or add any checks.

3
  • Do you have any additional information/statistics about those numbers? Like distribution, max/min, p99, etc? Commented Feb 20, 2016 at 4:39
  • The information I have is numbers are 32 bit numbers and will be in range 2147483647 to -2147483647. These are singed integers. Commented Feb 20, 2016 at 4:42
  • Based on information you've provided - there is no way (I've listed some options in my answer that could help if you'd not have these restrictions). Note that if this is interview question that is likely some other information you are expected to query for that will allow particular types of filtering. Commented Feb 20, 2016 at 4:49

2 Answers 2

4

There is no way to achieve what you want in generic case. There is no information on int (or any other built in value type) on the way how it was generated. There is no difference in binary pattern of value obtained by regular operation or overflow.

Your options:

  • some cases may be detected by range checking - if normal values are relatively small some large values are likely errors
  • capture more information when constructing stream of values - include "overflow" flag as pair to the value
  • use wider type for values (long or BigInteger)

Note that overflow also can produce small number/positive numbers, so there is no chances of filtering all invalid values unless you know more information about how computation is performed.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Alexei for answering, what about carry flag or further addition or subtraction on overflowed numbers if overflowed number is represented on number line -2147483647........ 0..........2147483647. Just dumb thought
@Sandeep carry flag exist only on CPU level immediately after operation, it is not stored anywhere and looses validity roughly after next CPU level command. There is no space in 32 bits of integer to store it either.
0

You can write defensive code to guard against overflow.

All of the built-in numeric types have MinValue and MaxValue properties

if (number >= Int32.MinValue && number <= Int32.MaxValue)
{
     // logic
}

1 Comment

Numbers are already overflowed and no control on source of numbers where I can change

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.