469,612 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Difference between int and double???

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
13 21017
weaknessforcats
9,208 Expert Mod 8TB
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 64KB
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
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
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,423 Expert 2GB
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
int holds 4 butes and double holds10 bytes..
Sep 29 '14 #7
weaknessforcats
9,208 Expert Mod 8TB
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.

The correct answer is that sizeof(int) and sizeof(double) will give the correct answer for your compiler. Other compilers may give other answers.
Sep 29 '14 #8
Sherin
77 64KB
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
264 256MB
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
Naheedmir
62 32bit
Decimal can 100% perfectly represent any number within the precision of the decimal format. In contrast, Float and Double, cannot perfectly represent all numbers, even numbers within their respective formats precision. Int data type is utilized to allocate integer values. It stores 2 bit
Jul 23 '20 #11
It usually occupies 32 bits in the computer memory with 4 bytes. An integer can have a maximum value of 2,147,483,647, whereas a float can have a maximum value of 3.4028235 1038.

Double is an IEEE 754 64 bits double-precision floating-point format data type, which is also used to represent floating-point numbers. IEEE 754 is a standard representation of floating-point numbers in a computer.

Blog link to know about the difference between float and double.
1 Week Ago #12
donbock
2,423 Expert 2GB
As weaknessforcats said 7y ago, the C Standard allows compiler implementations substantial flexibility regarding the internal representation for the various types.

Standard library header <limits.h> provides a method for the compiler implementation to tell an application program some details of the implementation-defined internal representation of integral types.
  • INT_MIN and INT_MAX are the minimum negative / maximum positive values that can be represented by type int.

Standard library header <float.h> provides a method for the compiler implementation to tell an application program some details of the implementation-defined internal representation of floating-point types.
FLT_MIN and FLT_MAX are the minimum/maximum positive values that can be represented by type float.
DBL_MIN and DBL_MAX are the minimum/maximum positive values that can be represented by type double.
LDBL_MIN and LDBL_MAX are the minimum/maximum positive values that can be represented by type long double.

sizeof(typename) tells you how many bytes it takes to represent a number of type typename.
1 Week Ago #13
eddparker01
3 2Bits
They share certain commonalities, despite their differences at first look. Integral representations exist for floating-point datatypes. Double is simply a double floating-point datatype, while float is a single(?) floating-point datatype. However, just because they have a decimal does not mean they are accurate data types. Use them for approximation rather than precision.
4 Days Ago #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

21 posts views Thread by b83503104 | last post: by
15 posts views Thread by Mohanasundaram | last post: by
44 posts views Thread by Daniel | last post: by
2 posts views Thread by Sanjeev Azad | last post: by
18 posts views Thread by Vasileios Zografos | last post: by
7 posts views Thread by Tor Aadnevik | last post: by
13 posts views Thread by Shirsoft | last post: by
6 posts views Thread by Alexander Stoyakin | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.