urgent...experts pls reply

This is a discussion on urgent...experts pls reply within the C Programming forums, part of the General Programming Boards category; question: Add two very large long integers variables(say long integers with maximum permitted value) with the help of linked list. ...

  1. #1
    Unregistered
    Guest

    Question urgent...experts pls reply

    question:
    Add two very large long integers variables(say long integers with maximum permitted value) with the help of linked list.

    when we add two long variables and save the result in a double variable ...the result is an unexpected one....though the double permits the value that we get in the result.
    please explain it as soon as possible, why we cant save the result in a double and the possible solution of the above problem.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You probably added the two longs together and then converted the result to a double. This will give you the incorrect value because the result of adding two longs is a long, thus you get overflow. According to C's type conversion rules, the return of an addition is the type of the longest floatiest operand. If you cast one of the operands to double then the result will be correct:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main ( void )
    {
      long a, b;
      double result;
      a = b = LONG_MAX;
      /*
      ** This works.
      */
      result = (double)a + b;
      printf ( "%ld + %ld = %.0f\n", a, b, result );
      return 0;
    }
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main ( void )
    {
      long a, b;
      double result;
      a = b = LONG_MAX;
      /*
      ** This does not.
      */
      result = (double)(a + b);
      printf ( "%ld + %ld = %.0f\n", a, b, result );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> longest floatiest operand

    nice one. i think that should've been :
    the variable with the highest comprehension.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent! Pls help me for my assignment!!
    By seanlee210 in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 08:30 PM
  2. pls help me urgent..
    By intruder in forum C Programming
    Replies: 4
    Last Post: 01-13-2003, 03:41 AM
  3. Urgent Help required.. pls help ??
    By intruder in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2003, 12:05 PM
  4. text box & buttons on window .. pls help urgent ???
    By intruder in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2002, 09:28 PM
  5. Another urgent help, plz reply fast
    By playboy1620 in forum C Programming
    Replies: 4
    Last Post: 04-11-2002, 05:21 AM

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