From: Junio C Hamano <hidden> Date: 2016-06-15 22:47:12
Ryan Flynn [off-list ref] writes:
given n, tried to find i greater than n via i=1, iterate i *= 10.
given n sufficiently close to UINT_MAX this will overflow; which can
produce i==0, which results in an infinite loop. iteratively dividing
n /= 10 does not have this problem, and though division is slower than
multiplication this only runs once per `git format-patch
--cover-letter`
Signed-off-by: pizza <redacted>
Pizza?
This is somewhat amusing.
- digits_in_number() is called only with opt->total that is "int";
- opt->total is the total number of patches.
- the return value is used like this:
sprintf(buf, "%0*d", digits_in_number(opt->total), opt->nr);
and opt->nr runs from 1 to opt->total; the use of "d" would be already
wrong anyway even if you computed digits_in_number() correctly.
Perhaps we should get rid of this function altogether?
From: Junio C Hamano <hidden> Date: 2016-06-15 22:47:12
Junio C Hamano [off-list ref] writes:
Ryan Flynn [off-list ref] writes:
quoted
given n, tried to find i greater than n via i=1, iterate i *= 10.
given n sufficiently close to UINT_MAX this will overflow; which can
produce i==0, which results in an infinite loop. iteratively dividing
n /= 10 does not have this problem, and though division is slower than
multiplication this only runs once per `git format-patch
--cover-letter`
Signed-off-by: pizza <redacted>
Pizza?
This is somewhat amusing.
- digits_in_number() is called only with opt->total that is "int";
- opt->total is the total number of patches.
- the return value is used like this:
sprintf(buf, "%0*d", digits_in_number(opt->total), opt->nr);
and opt->nr runs from 1 to opt->total; the use of "d" would be already
wrong anyway even if you computed digits_in_number() correctly.
Perhaps we should get rid of this function altogether?
how about
rev.num_width = (int)log10((double)rev.total) + 1;
hm?
log10() appears to be C99, but can be emulated on earlier C-versions by doing
#define log10(x) (log(x) / log(10.0))
--
Erik "kusma" Faye-Lund
kusmabite@gmail.com
(+47) 986 59 656
From: Ryan Flynn <hidden> Date: 2016-06-15 22:47:12
On Sun, Aug 9, 2009 at 3:38 AM, Junio C Hamano[off-list ref] wrote:
quoted hunk
Junio C Hamano [off-list ref] writes:
quoted
Ryan Flynn [off-list ref] writes:
quoted
given n, tried to find i greater than n via i=1, iterate i *= 10.
given n sufficiently close to UINT_MAX this will overflow; which can
produce i==0, which results in an infinite loop. iteratively dividing
n /= 10 does not have this problem, and though division is slower than
multiplication this only runs once per `git format-patch
--cover-letter`
Signed-off-by: pizza <redacted>
Pizza?
This is somewhat amusing.
- digits_in_number() is called only with opt->total that is "int";
- opt->total is the total number of patches.
- the return value is used like this:
sprintf(buf, "%0*d", digits_in_number(opt->total), opt->nr);
and opt->nr runs from 1 to opt->total; the use of "d" would be already
wrong anyway even if you computed digits_in_number() correctly.
Perhaps we should get rid of this function altogether?
strbuf_addch(sb, '\n');
}
-static unsigned int digits_in_number(unsigned int number)
-{
- unsigned int i = 10, result = 1;
- while (i <= number) {
- i *= 10;
- result++;
- }
- return result;
-}
-
static int has_non_ascii(const char *s)
{
int ch;
how about
rev.num_width = (int)log10((double)rev.total) + 1;
hm?
log10() appears to be C99, but can be emulated on earlier C-versions by
doing #define log10(x) (log(x) / log(10.0))
That would mean linking with -lm?
Regards,
Christian.
From: Johannes Schindelin <hidden> Date: 2016-06-15 22:47:12
Hi,
On Mon, 10 Aug 2009, Erik Faye-Lund wrote:
On Mon, Aug 10, 2009 at 7:24 AM, Christian
Couder[off-list ref] wrote:
quoted
quoted
log10() appears to be C99, but can be emulated on earlier C-versions by
doing #define log10(x) (log(x) / log(10.0))
That would mean linking with -lm?
I guess so. Are we currently trying to avoid linking to the math-parts
of libc?
Yes.
I guess we could fix the overflow thing very easily, though:
static unsigned int digits_of_number(unsigned int number) {
unsigned int result;
for (result = 1; number; number /= 10, result++)
; /* do nothing */
return result;
}
Ciao,
Dscho
From: Ryan Flynn <hidden> Date: 2016-06-15 22:47:13
On Mon, Aug 10, 2009 at 8:24 AM, Johannes
Schindelin[off-list ref] wrote:
Hi,
On Mon, 10 Aug 2009, Erik Faye-Lund wrote:
quoted
On Mon, Aug 10, 2009 at 7:24 AM, Christian
Couder[off-list ref] wrote:
quoted
quoted
log10() appears to be C99, but can be emulated on earlier C-versions by
doing #define log10(x) (log(x) / log(10.0))
That would mean linking with -lm?
I guess so. Are we currently trying to avoid linking to the math-parts
of libc?
Yes.
I guess we could fix the overflow thing very easily, though:
static unsigned int digits_of_number(unsigned int number) {
unsigned int result;
for (result = 1; number; number /= 10, result++)
; /* do nothing */
return result;
}
Ciao,
Dscho
From: Ryan Flynn <hidden> Date: 2016-06-15 22:47:13
On Mon, Aug 10, 2009 at 8:24 AM, Johannes
Schindelin[off-list ref] wrote:
Hi,
On Mon, 10 Aug 2009, Erik Faye-Lund wrote:
quoted
On Mon, Aug 10, 2009 at 7:24 AM, Christian
Couder[off-list ref] wrote:
quoted
quoted
log10() appears to be C99, but can be emulated on earlier C-versions by
doing #define log10(x) (log(x) / log(10.0))
That would mean linking with -lm?
I guess so. Are we currently trying to avoid linking to the math-parts
of libc?
Yes.
I guess we could fix the overflow thing very easily, though:
static unsigned int digits_of_number(unsigned int number) {
unsigned int result;
for (result = 1; number; number /= 10, result++)
; /* do nothing */
return result;
}
Ciao,
Dscho
From: Johannes Schindelin <hidden> Date: 2016-06-15 22:47:13
Hi,
[please cull the quoted text to what you are actually replying to.
Thanks]
On Mon, 10 Aug 2009, Ryan Flynn wrote:
On Mon, Aug 10, 2009 at 8:24 AM, Johannes
Schindelin[off-list ref] wrote:
quoted
static unsigned int digits_of_number(unsigned int number) {
unsigned int result;
for (result = 1; number; number /= 10, result++)
; /* do nothing */
return result;
}
whoops, actually yours: digits_of_number(1) -> 2
static unsigned int digits(unsigned int number)
{
unsigned int result;
for (result = 1; (number /= 10); result++)
; /* do nothing */
return result;
}
I'm sorry, I forgot the "something like this" in my mail.
This version is actually tested.
It has non-optimal runtime, but then, it does not really matter.
Ciao,
Dscho
From: Jeff Epler <hidden> Date: 2016-06-15 22:47:13
On Sun, Aug 09, 2009 at 02:25:40PM +0200, Erik Faye-Lund wrote:
log10() appears to be C99, but can be emulated on earlier C-versions by doing
#define log10(x) (log(x) / log(10.0))
I don't think you'll like the results of this very much.
#include <math.h>
#include <stdio.h>
int main(void) {
double n=1;
int i, j;
for(i=0; i<10; i++, n*=10) {
j = (int)(log(n)/log(10));
if(i != j) printf("%d %d\n", i, (int)j);
}
return 0;
}
(on my system, 3 of the 10 tested cases give the wrong answer due to
rounding)
For a tour of some of the difficulties of implementing log10,
http://www.cs.berkeley.edu/~wkahan/LOG10HAF.TXT
Jeff