[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppDouble
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
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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
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! }
- include <numeric>
- include <vector>
- include <cassert>
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.int main() { double zero = 0.0; double minusZero = -0.0; assert(zero==minusZero) }
- include <cassert>
Tricks
Code links
- <<, stream out operator
- #include
- assert
- cassert (header file)
- const
- #include
- int
- iostream
- main
- numeric (header file)
- return
- std
- std::vector
- stream out operator, <<
- vector
- vector (header file)
'double' links
Reference
- 1) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
