0.00000000000000002025 is 21 digits, so obviously you need more than 18 digits.

Using a single byte for the fixed point will get me down to +-1e-127

I think you are misunderstanding "fixed point". Fixed point means the exponent part is always the same. For example, with 64 bit signed integers:

But you need more than 18 digits, so you need to cascade. Though I misspoke. What you cascade depends the underlying hardware. For a PC with 64 bit integers, you need to cascade 3 or 4 32 bit integers to your needed digits. For a PC with 32 bit integers, you need to cascade at least 5 16 bit integers.

Let's assume your PC has 64 bit integers. To add 2 numbers, A and B, you split them into Ah, Am, Al, Bh, Bm and Bl, then do the additions:

/* C */ sint64_t A, B, Ah, Am, Al, Bh, Bm, Bl, Ch, Cl, /* etc */; sint128 C; Al = A & 0xFFFFFFFF; Am = A >> 32; Ah = A >> 64; Bl = B & 0xFFFFFFFF; Bm = A >> 32; Bh = A >> 64; Cl = Al + Bl; Cm = Am + Bm + (Cl >> 32); Ch = Ah + Bh + (Cm >> 32); C = (Ch << 64) + ((Cm & 0xFFFFFFFF) << 32) + (Cl & 0xFFFFFFFF);

Of course, there are some easy optimizations to this I leave as an exercise for the reader. Also leaving subtraction, multiplication and division as exercises for the reader.

Note: sint64_t and sint128_t are the C99 standard type names for "exactly 64/128 bit, signed integer"

Welcome to the world of "bit bashing".


In reply to Re^5: Need more precision. by RonW
in thread Need more precision. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.