473,840 Members | 1,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
  15.     }
  16.     cout<<"the sqrt is "<<i;
  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
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
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 ..


Expand|Select|Wrap|Line Numbers
  1. //if you just going to work with hole numbers
  2. int a;
  3. a = 1
  5. //if you're working with numbers with a bit more presition
  6. double b;
  7. b = 1.1234
  9. //if you're working with numbers with massive presition.. 
  10. float c;
  11. c = 1.123456
Aug 1 '13 #3
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
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

Sep 23 '14 #5
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
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.

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
77 New Member

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

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

Similar topics

by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
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 references that I have come across tells that calloc initialize the memory to all zeros. In Herbert Shiltd's C/C++ reference I found that calloc returns a pointer to an array. I want to understand in what way the memory returned by malloc and calloc...
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, "Built-in == operator should not be
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 = 0, d = 0, e = 0; for(int n = 0; n != 6000000; n++) { a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2; b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2; c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 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, the debugger shows the value of val as "1.599999999999999e-5", not "1.6000000000000e-5", which is what the debugger shows in VC++6.0.
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 like cint(anInt) and cType(anInt, System.Int32) but I checked with ildasm) - if I didn't made a mistake. So when to use which syntax? Is there any performance penalty when using the one over the other or does anybody knows any differences? '...
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
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 Totto
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 ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
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, val2 = 0.2; if ( (val1 - val2) limit )
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.