Addition of two numbers

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 ...

  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,699
    >> Can somebody help me out.

    Sure. But since this is probably homework anyway, I would ask what have you got so far?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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,699
    Ha! True.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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
    21,630
    What exactly are you trying to do? Answer with respect to Salem's post #5.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12: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, 10: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, 09:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21