Difference between int and double???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashishkumardass
    New Member
    • Jul 2013
    • 1

    Difference between int and double???

    Code:
    #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.
    Last edited by Banfa; Jul 31 '13, 12:15 PM. Reason: Place code tags round the code, please do this in future
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • Xillez
      New Member
      • Jul 2013
      • 93

      #3
      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:

      Code:
      //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

      Comment

      • AceInfinity
        New Member
        • Apr 2013
        • 12

        #4
        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.

        Comment

        • brianmanee
          New Member
          • Sep 2014
          • 1

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

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            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.

            Comment

            • Mathan L
              New Member
              • Sep 2014
              • 1

              #7
              int holds 4 butes and double holds10 bytes..

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                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.

                Comment

                • Sherin
                  New Member
                  • Jan 2020
                  • 77

                  #9
                  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.

                  Comment

                  • SioSio
                    Contributor
                    • Dec 2019
                    • 272

                    #10
                    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.

                    Comment

                    • Naheedmir
                      New Member
                      • Jul 2020
                      • 62

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

                      Comment

                      • cloudytechi147
                        New Member
                        • Nov 2021
                        • 2

                        #12
                        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.

                        Comment

                        • donbock
                          Recognized Expert Top Contributor
                          • Mar 2008
                          • 2427

                          #13
                          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.

                          Comment

                          • eddparker01
                            New Member
                            • Nov 2021
                            • 3

                            #14
                            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.

                            Comment

                            • Riya Bajpai
                              New Member
                              • Feb 2023
                              • 18

                              #15
                              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.

                              Comment

                              Working...