overflow (C++)

Programming, for all ages and all languages.
Post Reply
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

overflow (C++)

Post by Zacariaz »

A simple question, which i should allready know the answer too (well i don't)
How do i check for overflow?
fx.

unsigned char var = 6*64;

Obiously this will result in overflow and wrap around resulting in an incorrect result. 128 i should think.

If this should happen, i don't need to know the result, just that the result is to large to fit into the datatype used.

In my scenario, where im going to multiply alot of rather large numbers, i know that this, sooner or later, will be a problem and therefore i am in desperate need of a solution.

Thanks.
This was supposed to be a cool signature...
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

I assume C++ still has the limits.h header.. or something of equivalent relevance.

I suppose you could...

Code: Select all

if((val1 * val2) > UCHAR_MAX)
	return -1;
unsigned char var = (unsigned char)(val * val2);
But really, I think this is a bit over the top... ;)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

I could use that methode for some situation, but using your example consider this:

Code: Select all

unsigned long long var1, var2;
var1 = 0xffffffffffffffffull;
var2 = 1ull;
if((var1 + var2) > UINT64_MAX)...
I may be mistaken, but i don't think this is going to work, the cpu register only go so high.

As i see it i need to check the overflow flag, but i have know idea howto in C++.
This was supposed to be a cool signature...
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

You didn't say anything about > 64bit integers in your initial post.. ;)

Obviously that would be something different...

It has already been pointed out in the past that you should be using 3rd party libraries for very long numbers.. what is your obsession with them? :lol:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Post by B.E »

you could do this:
tempx = x
x = x+y
if (x > tempx)
// overflow didn't occur
else
// overflow did occur
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

Yes, but as i demonstrated, that doesnt allways work.

True, you can often the bigger of the two numbers and then if the result is smaller, we have overflow, however this is only guarantied to work if we add or subtract (of course doing everything in reverse), not if we multiply or divide (acutally im not quite sure about anything when it comes to division) and multiplying alot of number is exactly what i need to do.

Should it be of any interest the math looks like this:

Image

A little big i know, but thats what happens when you do thing in a hurry.
Anyway, it is obious that you can't just do the math like: (1/2)(2/3)(4/5)(6/7)(10/11)... you'll find that precision be a problem before long, fortunantly it is easy to rewrite it:
(1*2*4*6*10)/(2*3*5*7*11) this enables us to wait with the division, bus as we all know, when multiplying alot of large numbers you'll quicklyget a rather large result. That we allso have a solution for:(1*2*4*6*10)/(2*3*5*7*11)=(4*2*2)/(7*11).

So basicly i have my methodes in order. this way i can do the math in stages and avoid any significant precision problems, but i do need to be sure that no limits are exceeded.
This was supposed to be a cool signature...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Zacariaz wrote:As i see it i need to check the overflow flag, but i have now idea howto in C++.
You can't.

But perhaps doing the calculation / overflow check in inline assembler helps things? Faster than the C++ workarounds for sure...
Every good solution is obvious once you've found it.
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

i considered this, but i never learned inline assembly. It shouldn't be hard, but every tutorial or documentation i find seems to have no working examples, it refuses to compile, and that goes for anything i do myself allso. Maybe you could point me in the right direction? obiously im doing something wrong.
This was supposed to be a cool signature...
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Post by cyr1x »

Try this:

Code: Select all

bool checkOverflowFlag() { int eflags; asm volatile("pushf; popl %0" : "=g"(eflags)); return (eflags >> 11) & 1; }
I'm not 100% sure if the OverflowFlag is bit 11 in the EFLAGS-register.
Post Reply