difference between int and double?

This is a discussion on difference between int and double? within the C++ Programming forums, part of the General Programming Boards category; i havent done this in about a year, and i seem to have forgotton pretty much everything. whats the diff ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    difference between int and double?

    i havent done this in about a year, and i seem to have forgotton pretty much everything. whats the diff between using int and double? i got an error from my compiler that said: " warning: assignment from 'int' to 'double' ". i changed the variable on the problem line from
    Code:
     int x;
    to
    Code:
     double x;
    and the error went away, but the program still doesnt do what i want it to? what did i change when i switched int for double?


    thanks
    -paul

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    It's impossible to deduce what the rest of your program does (or doesn't) do from a single declaration.

    So post the rest of it
    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.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    int is a whole number, double is a double-precision floating point number.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    The following web tutorial will tell you briefly about the different types:

    http://www.robertjacobs.fsnet.co.uk/basics.htm

    There's no reason why you can't cast an int directly to a double if that's what you want to do:

    Code:
    int i = 12345;
    double d = static_cast<double> (i);
    If you don't provide a cast your compiler may issue a warning (but will compile and run the code anyway). If you post the code and explain what you're trying to do we can offer specific advice as to which is appropriate for you.
    Last edited by Omnius; 01-11-2004 at 05:39 AM.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    2
    ok heres what i have. keep in mind that i only know really basic functions, as i have just started doing this -
    Code:
    #include <iostream.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main()
    {
    
      int a;
      int b;
      int c;
      double x;
      double x2;
      double m;
      double n;
      double q;
      double r;
      m = -b + sqrt(b - 4*a*c);
      n = 2*a;
      q = -b - sqrt(b-4*a*c);
      x = m/n;
      x2 = q/n;
    
      cout<<"Please enter a:";
      cin>>a;
      cout<<"Please enter b:";
      cin>>b;
      cout<<"Please enter c:";
      cin>>c;
    
      if ((b*b)-(4*a*c)<0)
       {
         cout<<"Answer contains non-real numbers";
       }
      else
       {
         cout<<"x = "<<x<<"or x = "<<x2<<"  ....Please check for extranious answers....";
       }
    
    system ("pause");
    return (0);
    }
    its supposed to do the quadratic formula { x = ( -b + root(b - 4(a)(c)) / 2a } which solves for x when given standard form ( ax + bx + c = 0 )
    if that equation is kinda unreadable, here it is also:
    http://www.sosmath.com/algebra/quadr...rmula/img9.gif

    it might be an order of operations thing, but im not really sure..


    thanks
    -paul

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    You're doing things in the wrong order

    First, you input the numbers
    cout<<"Please enter a:";
    cin>>a;
    cout<<"Please enter b:";
    cin>>b;
    cout<<"Please enter c:";
    cin>>c;


    Then you perform the test which prevents you doing sqrt() on negative numbers
    if ((b*b)-(4*a*c)<0)

    Finally, in the else part of your code, you do the following just before you output the roots
    m = -b + sqrt(b - 4*a*c);
    n = 2*a;
    q = -b - sqrt(b-4*a*c);
    x = m/n;
    x2 = q/n;
    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.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > m = -b + sqrt(b - 4*a*c);

    You also forgot to square b.
    m = -b + sqrt(b*b - 4*a*c);

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    An integer in 32-bit code is 32-bits and a double is always 64-bits.
    Doubles are handled by the FPU and integers are handled by the CPU.

    Their binary representations are interpreted very differently from one another - doubles and floats both are very complex when it comes to binary representation whereas integers are fairly straightforward.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Actually Bubba,

    A 32 bit compiler will probably hold a 32 bit int, but the standard specifies that it must hold at least 16 bits.

    And, C++ code can be compiled for machines without an FPU.

    REF: section 5.2.4.2.1 of the C standard. The C++ standard refers-back to the C standard. (It only took me 45 minutes to find that... 1200 PDF pages...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  5. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM

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