LESSON 29 OF 52 · CHAPTER 04

Float Smoothing

Float-only weighted blending, default arguments and needs contracts

ORIGINAL SOURCE

05-float-smoothing.srp

// Exponential smoothing keeps fractional state without implicit conversions.
pure funcra blend(current: float, sample: float, weight: float = 0.25) -> float
    needs weight >= 0.0 and weight <= 1.0;
{
    return current * (1.0 - weight) + sample * weight;
}

var estimate = 20.0;
repeat 4 as iteration {
    estimate = blend(estimate, sample: 28.0);
}
print("smoothed", estimate);
print("balanced", blend(10.0, 14.0, 0.5));
EXPECTED OUTPUT

stdout

Verified
smoothed 25.46875
balanced 12.0

UNDERSTAND THE CODE

Explanation & practice

What this example teaches

Float-only weighted blending, default arguments and needs contracts

Expected behavior

smoothed 25.46875, balanced 12.0

Practice: Read the source before looking at the output. Predict the values and their order, then use the pinned compiler to execute this lesson and compare the exact fixture. Identify which operation produces the result.

दशमलव स्मूथिंग

blend(current, sample, weight) दो मानों को वज़न के आधार पर मिलाता है। weight का डिफ़ॉल्ट मान 0.25 है और needs से उसका दायरा 0–1 रखा गया है। 20.0 का शुरुआती अनुमान चार बार 28.0 की ओर बढ़ता है।

आउटपुट: smoothed 25.46875 और balanced 12.0।

अभ्यास: अगर वज़न 0.5 हो तो 10.0 और 14.0 का मिश्रण 12.0 क्यों है?

Explanations are sourced from the project's English chapter guide and हिन्दी अध्याय. The Scarp program and output are unchanged.

TRY IT LOCALLY

Verify, then execute

Use the pinned compiler from the course root. Keep any imported _support files next to the source.

scarp verify examples/04-collections/05-float-smoothing.srp scarp exec examples/04-collections/05-float-smoothing.srp

The website does not run code. Read the CLI guide ↗

Pinned compiler: Scarp v0.0.6-alpha.3 · ceff07d9014f. No simulated output.