php - Calculating a value range in percent -
the following function takes dpi value , returns percentage:
100% if value bigger 200 50% 100% if value between 100 , 200 25% 50% if value between 80 , 100 0% 25% if value lower 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); } now have change function according these rules, returning:
100% if value bigger 100 75% 100% if value between 72 , 100 50% 75% if value between 50 , 72 0% 50% if value lower 50 in order achieve this, tried re-model function according how understood behaviour:
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); } but results plain wrong. dpi of 96 instance give me 141% result. wrong lack mathematical understanding know why - , how fix it.
i must have misunderstood how function works.
can elaborate on this?
that's right idea coefficient numbers in formulas wrong, results 141%.
you should try :
static public function interpretdpi($x) { if ($x > 100) return 100; if ($x > 72 && $x <= 100) return 75 + 25 * (($x - 72) / 28); if ($x >= 50 && $x <= 72) return 50 + 25 * (($x - 50) / 22); if ($x < 50) return 50 * ($x / 50); } i think you'll results want, checked it, looks :)
Comments
Post a Comment