2006-08-30 15:08:13 +08:00
|
|
|
/*
|
|
|
|
* percent.c - Take percentage of a number
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Theodore Ts'o <tytso@mit.edu>
|
2008-08-28 11:07:54 +08:00
|
|
|
*
|
2010-05-18 09:31:56 +08:00
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Library
|
|
|
|
* General Public License, version 2.
|
|
|
|
* %End-Header%
|
2006-08-30 15:08:13 +08:00
|
|
|
*/
|
|
|
|
|
2011-09-19 05:34:37 +08:00
|
|
|
#include "config.h"
|
2006-08-30 15:08:13 +08:00
|
|
|
#include "e2p.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We work really hard to calculate this accurately, while avoiding
|
|
|
|
* an overflow. "Is there a hyphen in anal-retentive?" :-)
|
|
|
|
*/
|
|
|
|
unsigned int e2p_percent(int percent, unsigned int base)
|
|
|
|
{
|
|
|
|
unsigned int mask = ~((1 << (sizeof(unsigned int) - 1) * 8) - 1);
|
|
|
|
|
2006-09-29 22:23:16 +08:00
|
|
|
if (!percent)
|
|
|
|
return 0;
|
2006-08-30 15:08:13 +08:00
|
|
|
if (100 % percent == 0)
|
|
|
|
return base / (100 / percent);
|
2008-08-28 11:07:54 +08:00
|
|
|
if (mask & base)
|
2006-08-30 15:08:13 +08:00
|
|
|
return (base / 100) * percent;
|
|
|
|
return base * percent / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
unsigned int base;
|
|
|
|
int percent;
|
|
|
|
char *p;
|
|
|
|
int log_block_size = 0;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: %s percent base\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
percent = strtoul(argv[1], &p, 0);
|
|
|
|
if (p[0] && p[1]) {
|
|
|
|
fprintf(stderr, "Bad percent: %s\n", argv[1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-08-28 11:07:54 +08:00
|
|
|
|
2006-08-30 15:08:13 +08:00
|
|
|
base = strtoul(argv[2], &p, 0);
|
|
|
|
if (p[0] && p[1]) {
|
|
|
|
fprintf(stderr, "Bad base: %s\n", argv[2]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%d percent of %u is %u.\n", percent, base,
|
|
|
|
e2p_percent(percent, base));
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
#endif
|