Own Big Float in C++ -


i want write own variable "type" homework in c++. should arbitrarily long float. thinking of structure like...

code:

class bigfloat { public:     bigfloat(arguments);     ~bigfloat();  private:     std::vector<char> before; // numbers before decimal point     std::vector<char> after; // numbers after decimal point     int pos; // position of decimal point }; 

where if have number like: 3.1415 before = '3'; after = '1415'; pos = 1; if makes sense you... assignment wants me save memory, don't because every number allocate about 1 byte, guess.

question:

how represent arbitrarily long numbers? (sorry bad english, hope post makes sense)

if need preserve memory, means need use memory efficiently possible. in other words, given value you're storing, shouldn't waste bytes.

example:

  1. 255 doesn't need 32 bits

i think vector of chars fine. if you're allowed use c++11 compiler, i'd change vector of uint8_t , make sure when i'm storing value can store value 0 255 in vector of size 1.

however, that's not end of it. sounds of it, you're after arbitrary number of significant digits. however, true float representation, need allocate storage the base , exponent, after deciding base type. there question of whether want exponent arbitrarily long too. let's assume so.

so, i'd use members of class:

//assuming base of 10. static uint8_t const base = 10; std::vector<uint8_t> digits_before_decimal; std::vector<uint8_t> digits_after_decimal; std::vector<uint8_t> exponent; std::bitset<1> sign; 

it matter of implementing various operators type , testing various scenarios make sure solution works.

if want thorough, use a simple testing framework make sure problems fix along way, stay fixed.

in memory, binary representation of number.

for example: 65535 be: before_decimal =<0xff,0xff>, after_decimal vector empty 255.255 be: before_decimal =<0xff>, after_decimal=<0xff> 255255 be: before_decimal =<0x03,0xe5,0x17>, after_decimal vector empty 255255.0 be: before_decimal =<0x03,0xe5,0x17>, after_decimal: <0> 

as others have mentioned, don't need 2 vectors before , after decimal. however, i'm using 2 in answer because makes easier understand , don't have keep track of decimal. memory requirements of 2 vs 1 vector aren't different when you're dealing long string of digits.

i should note using integer record position of decimal point limits number of digits 2 billion, not arbitrarily long number.

update: if homework, check whoever has given homework if need support floating point special cases, simplest of nans. there other special cases too, trying implement of them turn homework assignment thesis. luck :)


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -