overflow (C++)
overflow (C++)
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.
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...
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
I assume C++ still has the limits.h header.. or something of equivalent relevance.
I suppose you could...
But really, I think this is a bit over the top...
I suppose you could...
Code: Select all
if((val1 * val2) > UCHAR_MAX)
return -1;
unsigned char var = (unsigned char)(val * val2);
I could use that methode for some situation, but using your example consider this:
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++.
Code: Select all
unsigned long long var1, var2;
var1 = 0xffffffffffffffffull;
var2 = 1ull;
if((var1 + var2) > UINT64_MAX)...
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...
- Brynet-Inc
- Member
- Posts: 2426
- Joined: Tue Oct 17, 2006 9:29 pm
- Libera.chat IRC: brynet
- Location: Canada
- Contact:
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:
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.
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:
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...
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...
Try this:
I'm not 100% sure if the OverflowFlag is bit 11 in the EFLAGS-register.
Code: Select all
bool checkOverflowFlag() { int eflags; asm volatile("pushf; popl %0" : "=g"(eflags)); return (eflags >> 11) & 1; }