Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need to preserve memory, all that means is that you need to use memory as efficiently as possible. In other words, given the value you're storing, you shouldn't waste bytes.</p> <p>Example: </p> <ol> <li>255 doesn't need 32 bits</li> </ol> <p>I think your vector of chars is fine. If you're allowed to use a C++11 compiler, I'd probably change that to a vector of <a href="http://en.cppreference.com/w/cpp/types/integer" rel="nofollow">uint8_t</a> and make sure when I'm storing the value that I can store a value from 0 to 255 in a vector of size 1. </p> <p>However, that's not the end of it. From the sounds of it, what you're after is an arbitrary number of significant digits. However, for a true float representation, you also need to allocate storage for <a href="https://en.wikipedia.org/wiki/Floating_point" rel="nofollow">the base and exponent</a>, after deciding what the base will be for your type. There is also the question of whether you want your exponent to be arbitrarily long too. Let's assume so.</p> <p>So, I'd probably use something like this for members of your class:</p> <pre><code>//Assuming a base of 10. static uint8_t const base = 10; std::vector&lt;uint8_t&gt; digits_before_decimal; std::vector&lt;uint8_t&gt; digits_after_decimal; std::vector&lt;uint8_t&gt; exponent; std::bitset&lt;1&gt; sign; </code></pre> <p>It is then a matter of implementing the various operators for your type and testing various scenarios to make sure your solution works. </p> <p>If you really want to be thorough, you could use <a href="https://code.google.com/p/googletest/wiki/V1_6_Documentation" rel="nofollow">a simple testing framework</a> to make sure that problems you fix along the way, stay fixed.</p> <p>In memory, it will essentially look like a binary representation of the number.</p> <pre><code>For example: 65535 will be: before_decimal =&lt;0xff,0xff&gt;, after_decimal vector is empty 255.255 will be: before_decimal =&lt;0xff&gt;, after_decimal=&lt;0xff&gt; 255255 will be: before_decimal =&lt;0x03,0xe5,0x17&gt;, after_decimal vector is empty 255255.0 will be: before_decimal =&lt;0x03,0xe5,0x17&gt;, after_decimal: &lt;0&gt; </code></pre> <p>As others have mentioned, you don't really need two vectors for before and after the decimal. However, I'm using two in my answer because it makes it easier to understand and you don't have to keep track of the decimal. The memory requirements of two vs one vector really aren't that different when you're dealing with a long string of digits.</p> <p>I should also note that using an integer to record position of the decimal point limits your number of digits to 2 billion, which is not an arbitrarily long number. </p> <p>UPDATE: If this actually is homework, I would check with whoever has given you the homework if you need to support any floating point special cases, the simplest of which would be <a href="https://en.wikipedia.org/wiki/Floating_point#NaNs" rel="nofollow">NaNs</a>. There are <a href="https://en.wikipedia.org/wiki/Floating_point#Special_values" rel="nofollow">other special cases</a> too, but trying to implement all of them will very quickly turn this from a homework assignment into a thesis. Good luck :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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