Thread: Addition of two numbers

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    New Delhi,India
    Posts
    17

    Addition of two numbers

    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.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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;
    }

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Sebastiani View Post
    >> Can somebody help me out.

    Sure. But since this is probably homework anyway, I would ask what have you got so far?
    It would be a really stupid homework assignment, seeing how it requires infinite memory
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    #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

  8. #8
    Registered User
    Join Date
    Apr 2008
    Location
    New Delhi,India
    Posts
    17
    Quote Originally Posted by saswatdash83 View Post
    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.
    I was trying with bit shift operation but did not succeed.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Location
    New Delhi,India
    Posts
    17
    Quote Originally Posted by Sebastiani View Post
    >> Can somebody help me out.

    Sure. But since this is probably homework anyway, I would ask what have you got so far?
    Hi Sebastiani,

    Could u pls explain a bit.I tried bit shift operation,but did not get the result.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly are you trying to do? Answer with respect to Salem's post #5.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2007, 01:38 PM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM