473,840 Members | 1,627 Online

# Difference between int and double???

1 New Member
Expand|Select|Wrap|Line Numbers
1. #include<iostream>
2. using namespace std;
3. int main()
4. {
5.     double x,i;
6.     cout<<"Enter the value of the number\n";
7.     cin>>x;
8.      for(i=0;i<x;i=i+0.1)
9.     {                     cout<<"value of i"<<i<<endl;
10.                           double l=i*i;
11.                           cout<<"value of square "<<l<<endl;
12.                           if(x==l)
13.                           break;
14.
15.     }
16.     cout<<"the sqrt is "<<i;
17.
18.     system("PAUSE");
19.     return 0;
20. }
why I am not getting exact square root if I take x as double,but if I am taking it as int I got the correct result.
Jul 31 '13 #1
15 46851
weaknessforcats
9,208 Recognized Expert Moderator Expert
An int is a binary representation of an integer. Like 5 is 101.

double is a formatted representation of a real number. Like 3.14. Doing math on a double causes more or fewer positions after the decimal point so these variables are called "floating point".

Try to keep integers separate from floating point in your code. Typically, floating point is used for scientific work where extreme accuracy is not required. Use integers for everything else.
Jul 31 '13 #2
Xillez
93 New Member
I learned that int is a hole number like 1, 2, 3, 4 and so on. Double is numbers with decimals like 1.1 or 45.564, float is a even more specific version of double ..

Example:

Expand|Select|Wrap|Line Numbers
1. //if you just going to work with hole numbers
2. int a;
3. a = 1
4.
5. //if you're working with numbers with a bit more presition
6. double b;
7. b = 1.1234
8.
9. //if you're working with numbers with massive presition..
10. float c;
11. c = 1.123456
12.
Aug 1 '13 #3
AceInfinity
12 New Member
However different from first glance, they do have similarities. Floating point datatypes do have an integral representation. float can be thought of as single(?) floating point datatype, and double is just a double floating point datatype. Just because they have a decimal doesn't make them accurate datatypes though. Use them for approximation, not accuracy.
Aug 12 '13 #4
brianmanee
1 New Member
The Decimal, Double, and Float variable types are different in the way that they store the values. Precision is the main difference where float is a single precision (32 bit) floating point data type, double is a double precision (64 bit) floating point data type and decimal is a 128-bit floating point data type. More info...Difference between int and double

Brian
Sep 23 '14 #5
donbock
2,426 Recognized Expert Top Contributor
The original program won't work when x is double. Line 12 intends to break out of the loop when x equals l. You can't count on that ever happening -- don't ever try to compare floating point numbers for equality or not-equals. Refer to what every computer scientist should know about floating-point arithmetic.

For the same reason, it is only bad luck that the program worked when x is an integer. I say bad luck because if it had failed the OP would have been led to change the algorithm.

@Xillez, you have float and double reversed. Use double for the highest precision. In fact, for most applications there is no need to use float at all.
Sep 23 '14 #6
Mathan L
1 New Member
int holds 4 butes and double holds10 bytes..
Sep 29 '14 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
You can't say an int is 4 bytes and a double is 10 bytes since the size of the type is not specified in the C language specs.

Sep 29 '14 #8
Sherin
77 New Member
Int

Int when you don't need fractional numbers and you've no reason to use anything else; on most processors/OS configurations, this is the size of number that the machine can deal with most efficiently;

Int data type is used to assign integer values ie) 0 to 9. It stores 2 bit

Int stores interger values like 1,2,3,4,-1,6

double

double when you need fractional numbers and you've no reason to use anything else;

Double data type is used to store numbers. It stores 8 bit

Double is same like float but with a bigger memory size.
Jul 3 '20 #9
SioSio
272 Contributor
When incrementing a double type variable, errors (digit loss) are accumulated.
Even if the operation result of double type variable and input data are compared with "==", the possibility of matching is low.
Jul 7 '20 #10