以下函数接受一个 DPI 值并返回一个百分比:
100% if the value is bigger than 200
50% to 100% if the value is between 100 and 200
25% to 50% if the value is between 80 and 100
0% to 25% if the value is lower than 80
static public function interpretDPI($x) {
if ($x >= 200) return 100;
if ($x >= 100 && $x < 200) return 50 + 50 * (($x - 100) / 100);
if ($x >= 80 && $x < 100) return 25 + 25 * (($x - 80) / 20);
if ($x < 80) return 25 * ($x / 80);
}
现在我必须根据这些规则更改此函数,返回:
100% if the value is bigger than 100
75% to 100% if the value is between 72 and 100
50% to 75% if the value is between 50 and 72
0% to 50% if the value is lower than 50
为了实现这一点,我尝试根据我对其行为的理解来重新建模该函数:
static public function interpretDPI($x) {
if ($x >= 100) return 100;
if ($x >= 72 && $x < 100) return 75 + 75 * (($x - 72) / 28);
if ($x >= 50 && $x < 72) return 50 + 50 * (($x - 50) / 22);
if ($x < 50) return 25 * ($x / 50);
}
但结果完全错误。例如,96 的 DPI 会给我 141%。显然这是错误的,但我缺乏数学理解来知道为什么 - 以及如何解决它。
我一定是误解了函数的工作原理。
谁能详细说明一下?
最佳答案
像这样编辑函数代码
static public function interpretDPI($x) {
if ($x > 100) return 100;
if ($x > 72 && $x <= 100) return 75 + 75 * (($x - 72) / 28);
if ($x >= 50 && $x <= 72) return 50 + 50 * (($x - 50) / 22);
if ($x < 50) return 25 * ($x / 50);
}
它会根据你的要求工作
关于php - 以百分比计算值范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16561404/