I want to write a program to add two numbers, such that the addition result is not going to overflow at anytime.Can somebody help me out.
Thanks.
This is a discussion on Addition of two numbers within the C Programming forums, part of the General Programming Boards category; I want to write a program to add two numbers, such that the addition result is not going to overflow ...
I want to write a program to add two numbers, such that the addition result is not going to overflow at anytime.Can somebody help me out.
Thanks.
>> Can somebody help me out.
Sure. But since this is probably homework anyway, I would ask what have you got so far?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Ha! True.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
You mean like
if ( some_kind_of_test_on_the_operands )
-- do addition
else
-- warn about overflow
Yes, there are ways to do that in a finite machine.
Write the obvious test on paper, and rearrange it a little.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Probably the only thing you need to know is the highest and lowest numbers that a variable type can hold. For int, you probably have numbers ranging from -2^31 up to 2^31-1, but it may be different on your machine. The easiest way to get at these numbers is to #include <limits.h> and use macros, e.g. INT_MIN and INT_MAX, which will be the values you need.
Actually, you can do the overflow test without knowing INT_MIN or INT_MAX, but knowing them may make it easier.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
#include<stdio.h>
main()
{
int x,y;
printf("Enter two no.s to ADD:");
scanf("%d%d",&x,&y);
printf("\n%d",x+y);
}
this is what i understand from u
What exactly are you trying to do? Answer with respect to Salem's post #5.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)