- #include<iostream>
-
using namespace std;
-
int main()
-
{
-
double x,i;
-
cout<<"Enter the value of the number\n";
-
cin>>x;
-
for(i=0;i<x;i=i+0.1)
-
{ cout<<"value of i"<<i<<endl;
-
double l=i*i;
-
cout<<"value of square "<<l<<endl;
-
if(x==l)
-
break;
-
-
}
-
cout<<"the sqrt is "<<i;
-
-
system("PAUSE");
-
return 0;
-
}
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.
15 46636
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.
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: -
//if you just going to work with hole numbers
-
int a;
-
a = 1
-
-
//if you're working with numbers with a bit more presition
-
double b;
-
b = 1.1234
-
-
//if you're working with numbers with massive presition..
-
float c;
-
c = 1.123456
-
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.
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
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.
int holds 4 butes and double holds10 bytes..
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.
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.
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.
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
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.
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.
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.
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.
"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.
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
| |