Another Native Data Type

We've used almost all the native data types, but there is one other native type that you might see in other programs and in other textbooks, so I should tell you about it now. This other native type is float, which, like the double type we've already seen, is used to store values that can contain fractional parts, (so-called floating-point numbers). Why are there two of these types rather than only one? The main difference between float and double is that the implementation of float in most compilers takes less memory than the implementation of double; typically floats are 4 bytes long and doubles are 8 bytes long. As a result, a double can store larger values and maintain higher accuracy. However, the greater memory consumption of doubles, which may not be important when we're dealing with a few values, can be quite important if we have a Vec or array of thousands or millions of elements and don't need very high accuracy; in that case, we may want to use floats instead of doubles.[1]

[1] The C++ standard requires only that a double be at least as long as a float, but in most implementations a double is twice the size of a float.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset