Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features: zero-padding and fixed-point #15

Open
spannella opened this issue Dec 31, 2022 · 2 comments
Open

Features: zero-padding and fixed-point #15

spannella opened this issue Dec 31, 2022 · 2 comments

Comments

@spannella
Copy link

I wanted to see how hard it might be to extend this to get fixed length zero-padded responses.

The other use-case is slightly less common but it can be often helpful to represent a fixed-point decimal number with a standard integer type like int32_t or int64_t where the number is stored as N * 10^D where D is the number of decimal digits supported. I was trying to think of how one could utilize your same approach but with the added issue of inserting the decimal place and potentially having leading zeros after the decimal but before the first digit.

Finally there is the fixed length, fixed-point decimal number, where you want to render the same number fixed-point decimal number but with a fixed number of decimal places and zero padding in the front for the rest

Mostly just thoughts on how this could be extended, I could provide naïve solutions to each of these if its helpful

@jeaiii
Copy link
Owner

jeaiii commented Jan 8, 2023

if you want to zero pad on the left, it may be better just to convert the number backwards, since you know the length ahead of time. Also, since you are writing out more digits than needed the speed advantage may be lost so I would tend to go with simple code.

One trick would be to add a large power of 10 like 1000000000 and print the number then zero out the leading 1, if you know all your numbers are less than 1e9. you could do the same trick to print base10 fixed point, something like the following for dollars.

char *to_text_as_dollars(char text[], unsigned int n)
{
    auto dollars = n / 100;
    auto cents = n % 100;
    text[0] = '$';
    auto tail = to_text_from_integer(to_text_from_integer(text + 1, dollars), cents + 100);
    tail[-3] = '.'
    return tail;
}

@jk-jeon
Copy link

jk-jeon commented Feb 1, 2023

I wanted to see how hard it might be to extend this to get fixed length zero-padded responses.

@spannella It's actually explained at the end of the article linked in the README.

TL;DR
You can do this.

I think it will perform better than the naive way of producing the digits in backward. The naive way should compute both the quotient and the remainder (thus two multiplications) for a digit pair, but the approach of this repo only needs one multiplication per a digit pair.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants