Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason they are not stored this way by default is that the range of valid values that can fit in a fixed set of bits is smaller. Your <code>float</code> class can store numbers between 1/MAXINT and MAXINT (plus or minus). A C/C++ <code>float</code> can represent numbers between 1E+37 and 1E-37 (plus or minus). In other words, a standard <code>float</code> can represent values 26 orders of magnitude bigger and 26 orders of magnitude smaller then yours despite taking half the number of bits. In general, it's more convenient to be able to represent very large and very small values than to be perfectly precise. This is especially true since rounding tends to give us the right answers with small fractions like 1/3. In g++, the following gives 1:</p> <pre><code>std::cout &lt;&lt; ((1.0/3.0) * 3.0) &lt;&lt; std::endl; </code></pre> <p>Remember that types in C++ have a fixed size in bits. Thus a datatype in 32 bits has at most MAX_UINT values. If you change the way it is represented, you're just changing which values can be precisely represented, not increasing them. You can't cram more in, and thus can't be "more precise". You trade being able to represent 1/3 precisely for not being able to represent other values precisely, like 5.4235E+25.</p> <p>It is true that your <code>float</code> can represent values more precisely between 1E-9 and 1E+9 (assuming 32 bit ints) but at a cost of being completely unable to represent values outside of this range. Worse, while the standard <code>float</code> always has 6 digits of precision, your <code>float</code> would have precision that varied depending on how close to zero the values were. (And note that you are using twice the bits that <code>float</code> does.)</p> <p>(I'm assuming 32 bit <code>int</code>s. Same argument applies for 64 bit <code>int</code>s.)</p> <p><strong>Edit</strong>: Also note that most data people use <code>float</code>s for is not precise anyway. If you are reading data off of a sensor, you've already got imprecision, so being about to "perfectly" represent the value is pointless. If you are using a <code>float</code> in any sort of computing context, it's not going to matter. There is no point in perfectly describing '1/3' if your purpose is to display a bit of text 1/3rd of the way across the screen.</p> <p>The only people who really need perfect precision are mathematicians, and they generally have software that gives them this. Very few others need precision beyond what <code>double</code> gives. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload