473,394 Members | 1,149 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
15 46646
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,426 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
272 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.
Nov 22 '21 #12
donbock
2,426 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.
Nov 22 '21 #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.
Nov 28 '21 #14
Riya Bajpai
18 16bit
int refers to integer in which we can use whole numbers only where as double is for decimal numbers. For example 1 is an integer and 1.0 is a decimal.
Mar 15 '23 #15
vipulguptaseo
21 16bit
"int" and "double" are data types used in programming languages like C++, Java, and Python, but they represent different values.

"int" is acronym for integer and is used to represent whole numbers (positive, negative, or zero) that do not have a fractional part. In most programming languages, the range of values that can be represented by an "int" is determined by the number of bits used to store it. For example, a 32-bit "int" can represent values from -2,147,483,648 to 2,147,483,647.

On the other hand, "double" is short for double-precision floating-point, and it is used to represent decimal numbers (positive, negative, or zero) that have a fractional part. In most programming languages, a "double" is represented using 64 bits, which allows it to describe a broader range of values than an "int." However, the precision of a "double" is limited by the number of bits used to represent it, so it may not be able to represent very small or huge values with complete accuracy.
Mar 23 '23 #16

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

Similar topics

21
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
15
by: Mohanasundaram | last post by:
Hi All, What is the difference between malloc and calloc other than the point that calloc will initialize the memory to all zeros? This was an interview question for me. All the books and...
44
by: Daniel | last post by:
I am grappling with the idea of double.Epsilon. I have written the following test: public void FuzzyDivisionTest() { double a = 0.33333d; double b = 1d / 3d; Assert.IsFalse(a == b,...
21
by: Rich | last post by:
I was considering C# for developing a scientific application, but I have noticed a ~30% difference between VC++ .NET and C# on the same machine, under identical conditions: double a = 0,b = 0, c...
2
by: Sanjeev Azad | last post by:
I'm porting my application from VC++ 6.0 to VS .Net and experiencing a difference in the double precisions. I have the following code in my application double val = 16e-6; When I debug it,...
4
by: Daniel Walzenbach | last post by:
Hi, I wonder if somebody could explain me the difference between Double.Parse and Convert.ToDouble. If I'm not mistaken they are implemented differently (I though for a moment they might be the same...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
6
by: Alexander Stoyakin | last post by:
Hello, please advise on the following issue. I need to check that difference between two double values is not higher than defined limit. int main() { double limit = 0.3; double val1 = 0.5,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.