[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppDouble

(C++) double

A standard data type (and thus a keyword) that can hold a single broken number.

const double myDouble = 12.34;


Prefer a double over a float or a long double (ref. 1, 4.10.15)

The range of a double can be found at compile time with std::numeric_limits.

If a double value can be written like an int, for example 1.0, do not skip the '.0', as then the compiler thinks it is an int. The example below shows this mistake

  1. include <numeric>
  2. include <vector>
  3. include <cassert>
int main() { std::vector<double> v; v.push_back(0.1); v.push_back(0.2); v.push_back(0.3); //Line below is wrong! '0' should be '0.0'! const double sum = std::accumulate(v.begin(),v.end(),0); assert(sum==0.0); //Hey, they sum is 0.0! }


Advanced

The double is on some/most environments a 64 bit number. The 0th bit contains the sign of the number. In some environments, this might cause the value 0.0 be unequal to -0.0. The == might yield a true, but in memory this will not be the case.
  1. include <cassert>
int main() { double zero = 0.0; double minusZero = -0.0; assert(zero==minusZero) }


Tricks

Code links

'double' links

Reference



last edited (May 28, 2007) by bilderbikkel, Number of views: 5489, Current Rev: 15 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.