Thread: difference between int and double?

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

3. int is a whole number, double is a double-precision floating point number.

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

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

cin>>a;
cin>>b;
cin>>c;

if ((b*b)-(4*a*c)<0)
{
}
else
{
}

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:

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

thanks
-paul

6. You're doing things in the wrong order

First, you input the numbers
cin>>a;
cin>>b;
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;

7. > m = -b + sqrt(b - 4*a*c);

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

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