H
henrytcy
Hi,
How can I get a digit from integer for example, 12311, one by one for
comparision?
Thanks!
How can I get a digit from integer for example, 12311, one by one for
comparision?
Thanks!
Hi,
How can I get a digit from integer for example, 12311, one by one for
comparision?
How do you know this?
Niklas Norrthon said:I don't, and later I also say consider snprintf.
(But platforms with ints larger than 3000 bits are
not too common yet...)
There is a good method for getting a reasonable, not too low and only
barely too high, upper bound on the length of a decimally expanded int.
It is, however, left as an exercise for the OP.
Dag-Erling Smørgrav said:There is a good method for getting a reasonable, not too low and only
barely too high, upper bound on the length of a decimally expanded int.
It is, however, left as an exercise for the OP.
There are several. Which one you choose depends on the relative
scarcity of CPU cycles and memory.
This one is slightly wasteful of memory, but is computed entirely at
compile time:
#include <limits.h>
char number[1+(sizeof(int)*CHAR_BIT)/3+1];
The resulting array will be large enough to store the null-terminated
decimal representation of any integer in the range (INT_MIN,INT_MAX).
Proof of this is left as an exercise for the reader.
For extra credits, show how and why the code can or must be modified to
accomodate the range (UINT_MIN,UINT_MAX) instead.
Dag-Erling Smørgrav said:This one is slightly wasteful of memory, but is computed entirely at
compile time:
#include <limits.h>
char number[1+(sizeof(int)*CHAR_BIT)/3+1];
Actually, please show why this works, especially why this works for
INT_MIN. It's easy enough to see N / 3 + 1 can approximate
ceil(log10(2^N)), but I can't get the details quite right for signed
integers. I'm sure I'm overlooking something obvious.
Interesting. Yes, that's probably more straightforward than the brute forceRichard said:Skarmander said:
Dag-Erling Smørgrav said:This one is slightly wasteful of memory, but is computed entirely at
compile time:
#include <limits.h>
char number[1+(sizeof(int)*CHAR_BIT)/3+1];
That looks familiar, but... well, enough of that later.
Actually, please show why this works, especially why this works for
INT_MIN. It's easy enough to see N / 3 + 1 can approximate
ceil(log10(2^N)), but I can't get the details quite right for signed
integers. I'm sure I'm overlooking something obvious.
I can explain it easily enough, for the simple reason that I invented it.
(I'm quite sure there are other people who've invented it too, and
long before I did, but at any rate I derived it independently, and so I
know why it works.)
An int comprises sizeof(int) * CHAR_BIT bits. Since three bits can always
represent any octal digit, and leaving signs and terminators aside for a
second, it is clear that (sizeof(int) * CHAR_BIT) / 3 characters are
sufficient to store the octal representation of the number (but see below).
Since decimal can represent more efficiently than octal, what's good enough
for octal is also good enough for decimal.
Now we add in 1 for the sign, and 1 for the null terminator, and that's
where the above expression comes from.
<snip>BUT: consider the possibility that the integer division truncates (which it
will do if sizeof(int) * CHAR_BIT is not a multiple of 3). Under such
circumstances, you could be forgiven for wanting some bodge factor in
there. That's why I use:
char number[1 + (sizeof(int) * CHAR_BIT + 2) / 3 + 1];
The + 2 is always sufficient to counter the effects of any truncation after
division by 3, but doesn't inflate the result by more than one character at
most.
Skarmander said:Actually, please show why this works, especially why this works forDag-Erling Smørgrav said:#include <limits.h>
char number[1+(sizeof(int)*CHAR_BIT)/3+1];
The resulting array will be large enough to store the null-terminated
decimal representation of any integer in the range (INT_MIN,INT_MAX).
Proof of this is left as an exercise for the reader.
INT_MIN. It's easy enough to see N / 3 + 1 can approximate
ceil(log10(2^N)), but I can't get the details quite right for signed
integers. I'm sure I'm overlooking something obvious.
So sizeof(int) * CHAR_BIT / 3 is the term supposed to approximate the numberDag-Erling Smørgrav said:Skarmander said:Actually, please show why this works, especially why this works forDag-Erling Smørgrav said:#include <limits.h>
char number[1+(sizeof(int)*CHAR_BIT)/3+1];
The resulting array will be large enough to store the null-terminated
decimal representation of any integer in the range (INT_MIN,INT_MAX).
Proof of this is left as an exercise for the reader.
INT_MIN. It's easy enough to see N / 3 + 1 can approximate
ceil(log10(2^N)), but I can't get the details quite right for signed
integers. I'm sure I'm overlooking something obvious.
There are three terms in the addition.
Richard Heathfield said:I can explain it easily enough, for the simple reason that I
invented it.
An int comprises sizeof(int) * CHAR_BIT bits. Since three bits can
always represent any octal digit, and leaving signs and terminators
aside for a second, it is clear that (sizeof(int) * CHAR_BIT) / 3
characters are sufficient to store the octal representation of the
number (but see below). Since decimal can represent more
efficiently than octal, what's good enough for octal is also good
enough for decimal.
BUT: consider the possibility that the integer division truncates
(which it will do if sizeof(int) * CHAR_BIT is not a multiple of
3). Under such circumstances, you could be forgiven for wanting some
bodge factor in there. That's why I use:
char number[1 + (sizeof(int) * CHAR_BIT + 2) / 3 + 1];
Turns out the bodge factor is never necessary on a two's complement
machine.
That's a bit presumptive, don't you think?
It's an obvious solution to anyone who understands logarithms.
Jordan Abel said:this means if you try it with char rather than int, you'll get screwed.
I never intended it to be used for char. I was relying on the fact
that int cannot be smaller than 16 bits.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.