如何用C语言while循环生成指定光速百分比序列并计算相关公式?
Generating Light Speed Percentage Sequence with Loops & Operators
Got it, let's break down how to generate this specific sequence of light speed percentages using loops and basic operators. The sequence has clear phases, so we can handle each part systematically without hardcoding every single value.
Sequence Breakdown
First, let's confirm the pattern you need:
- Phase 1: 30%, 60%, 90% (step of 30%, stopping at 90%)
- Phase 2+: 99%, 99.3%, 99.6%, 99.9%, 99.93%, 99.96%, 99.99%, ..., 99.999% (stepping by 0.3 at each precision level, then shrinking the step by 10x once we hit the 9 threshold for that precision)
Python Implementation
Here's a clean, loop-based solution that avoids hardcoding all values:
# Initialize list to store our formatted percentages light_speed_percentages = [] # Phase 1: Generate 30%, 60%, 90% for p in range(30, 91, 30): light_speed_percentages.append(f"{p}%") # Phase 2+: Generate values from 99% up to 99.999% current = 99.0 step = 0.3 target = 99.999 while current <= target: # Format to avoid trailing .0 for whole numbers (e.g., "99%" instead of "99.0%") formatted = f"{int(current)}%" if current % 1 == 0 else f"{current}%" light_speed_percentages.append(formatted) next_value = current + step # Calculate the upper limit for the current precision (e.g., 99.9 for step=0.3) precision = len(str(step).split(".")[1]) if "." in str(step) else 0 upper_limit = int(current) + (0.9 * (10 ** -(precision - 1))) if precision > 0 else int(current) + 0.9 # Check if we've hit the end of the current precision level if next_value > upper_limit: # Shrink step by 10x and move to the next precision tier step /= 10 current = round(current + step, 5) # Round to avoid floating point errors else: current = round(next_value, 5) # Print the full sequence to verify for percentage in light_speed_percentages: print(percentage)
Key Details:
- Phase 1: Uses a simple
range()loop with a step of 30 to generate the initial values. - Phase 2: Uses a
whileloop to iterate through precision tiers. We track the current value and step size, shrinking the step by 10x each time we hit the 9 threshold (like 99.9 → next tier uses step=0.03). - Floating Point Handling: The
round()function prevents precision issues (e.g., avoiding 99.9999999999 instead of 99.999).
JavaScript Alternative
If you need this in JavaScript, the logic is identical—only syntax changes:
let lightSpeedPercentages = []; // Phase 1: Generate 30%, 60%, 90% for (let p = 30; p <= 90; p += 30) { lightSpeedPercentages.push(`${p}%`); } // Phase 2+: Generate values from 99% up to 99.999% let current = 99.0; let step = 0.3; const target = 99.999; while (current <= target) { // Format to clean up whole numbers let formatted = current % 1 !== 0 ? `${current}%` : `${Math.floor(current)}%`; lightSpeedPercentages.push(formatted); let nextValue = current + step; // Calculate upper limit for current precision const stepStr = step.toString(); const precision = stepStr.includes('.') ? stepStr.split('.')[1].length : 0; const upperLimit = Math.floor(current) + (0.9 * (10 ** -(precision - 1))) || Math.floor(current) + 0.9; if (nextValue > upperLimit) { step /= 10; current = parseFloat((current + step).toFixed(5)); } else { current = parseFloat(nextValue.toFixed(5)); } } // Log the sequence console.log(lightSpeedPercentages);
Extending the Sequence
If you ever need to go beyond 99.999% (e.g., 99.9999%), just update the target value— the loop will automatically handle the new precision tier.
内容的提问来源于stack exchange,提问作者w3stw0od
相关产品推荐
相关产品推荐

