求百分比驱动的双区间数值转换数学公式
Let's break down your requirements step by step and derive the exact mathematical formulas you need. First, let's restate your key parameters for clarity:
minNumber = 12maxNumber = 24maxLimit = 30percentageranges from1.0down to0.0
First, Clarify the Conflict in Your Requirement
You mentioned two slightly conflicting points:
- When
percentagedrops from1.0to0.5, the result should increase fromminNumber (12)tomaxLimit (30) - When
percentage = 1.0,result = 0
I'll provide solutions for both scenarios below, starting with the first (consistent with your primary dual-range logic).
Scenario 1: Follow the Dual-Range Increment/Decrement Logic (1.0 → 12, 0.5 →30, 0.0→24)
We split the formula into two conditional segments based on where percentage falls:
1. When percentage ≥ 0.5 (1.0 down to 0.5)
This maps the percentage range [1.0, 0.5] to the result range [12, 30]. The formula is:
result = 48 - 36 * percentage
Verification:
percentage = 1.0:48 - 36*1 = 12✔️percentage = 0.5:48 - 36*0.5 = 30✔️
2. When percentage < 0.5 (0.5 down to 0.0)
This maps the percentage range [0.5, 0.0] to the result range [30, 24]. The formula is:
result = 24 + 12 * percentage
Verification:
percentage = 0.5:24 + 12*0.5 = 30✔️percentage = 0.0:24 + 12*0 = 24✔️
If you need this as a single conditional expression (for code implementation), it would look like:
if percentage >= 0.5: result = 48 - 36 * percentage else: result = 24 + 12 * percentage
Scenario 2: Honor the percentage=1.0 → result=0 Rule
If you strictly need result=0 when percentage=1.0 (even though it conflicts with the first range's stated starting point), adjust the first segment formula to:
1. When percentage ≥ 0.5
result = 60 * (1.0 - percentage)
Verification:
percentage=1.0:60*(1-1)=0✔️percentage=0.5:60*(1-0.5)=30✔️
The second segment formula remains identical to Scenario 1.
How We Derived These Formulas
For linear mapping between two ranges, we use the standard linear interpolation framework:result = start_value + (end_value - start_value) * normalized_percentage
- For the first range, we normalized
percentageto a 0-1 scale by calculating(1.0 - percentage)/0.5(since the percentage spans 0.5 units from 1.0 to 0.5) - For the second range, we normalized with
(0.5 - percentage)/0.5to map 0.5→0 and 0.0→1, ensuring the result decreases from 30 to 24.
内容的提问来源于stack exchange,提问作者Scar

