What is the difference between float and double?

前端 未结 13 737
悲哀的现实
悲哀的现实 2020-11-22 06:00

I\'ve read about the difference between double precision and single precision. However, in most cases, float and double seem to be interchangeable,

相关标签:
13条回答
  • 2020-11-22 06:14

    Type float, 32 bits long, has a precision of 7 digits. While it may store values with very large or very small range (+/- 3.4 * 10^38 or * 10^-38), it has only 7 significant digits.

    Type double, 64 bits long, has a bigger range (*10^+/-308) and 15 digits precision.

    Type long double is nominally 80 bits, though a given compiler/OS pairing may store it as 12-16 bytes for alignment purposes. The long double has an exponent that just ridiculously huge and should have 19 digits precision. Microsoft, in their infinite wisdom, limits long double to 8 bytes, the same as plain double.

    Generally speaking, just use type double when you need a floating point value/variable. Literal floating point values used in expressions will be treated as doubles by default, and most of the math functions that return floating point values return doubles. You'll save yourself many headaches and typecastings if you just use double.

    0 讨论(0)
  • 2020-11-22 06:14

    Unlike an int (whole number), a float have a decimal point, and so can a double. But the difference between the two is that a double is twice as detailed as a float, meaning that it can have double the amount of numbers after the decimal point.

    0 讨论(0)
  • 2020-11-22 06:17
    • A double is 64 and single precision (float) is 32 bits.
    • The double has a bigger mantissa (the integer bits of the real number).
    • Any inaccuracies will be smaller in the double.
    0 讨论(0)
  • 2020-11-22 06:20

    If one works with embedded processing, eventually the underlying hardware (e.g. FPGA or some specific processor / microcontroller model) will have float implemented optimally in hardware whereas double will use software routines. So if the precision of a float is enough to handle the needs, the program will execute some times faster with float then double. As noted on other answers, beware of accumulation errors.

    0 讨论(0)
  • 2020-11-22 06:23

    The built-in comparison operations differ as in when you compare 2 numbers with floating point, the difference in data type (i.e. float or double) may result in different outcomes.

    0 讨论(0)
  • 2020-11-22 06:25

    When using floating point numbers you cannot trust that your local tests will be exactly the same as the tests that are done on the server side. The environment and the compiler are probably different on you local system and where the final tests are run. I have seen this problem many times before in some TopCoder competitions especially if you try to compare two floating point numbers.

    0 讨论(0)
提交回复
热议问题