5.4.22 · HinglishScientific Computing (Python)

Floating point gotchas — catastrophic cancellation, associativity failure

1,907 words9 min readRead in English

5.4.22 · Coding › Scientific Computing (Python)


1. WHAT is a floating point number?

WHY ? Implicit leading 1 ke baad 52 fraction bits hone ke saath, exponent par least significant bit ka weight hai. Yahi ke paas resolution hai.

Key word hai relative: error number ki magnitude ke saath scale hoti hai, absolutely nahi.


2. Catastrophic cancellation

Figure — Floating point gotchas — catastrophic cancellation, associativity failure

3. Associativity failure


4. Common mistakes (steel-manned)


5. Recall

Recall Active recall — answers cover karo
  • mein relative-error amplifier kya hai? → .
  • Kya near-equal numbers ki float subtraction khud inaccurate hoti hai? → Nahi, yeh exact hai; yeh sirf prior error ko expose karti hai.
  • kyun? → Har + ek aise jagah round karta hai jo partial sum ki magnitude par depend karta hai.
  • Chhote quadratic root ka fix? → .
  • Kahan summation kya track karta hai? → Har addition mein lost low-order bits.
Recall Feynman: 12-saal ke bachche ko explain karo

Socho tumhara calculator sirf 4 digits dikha sakta hai. likho aur add karo — calculator phir bhi dikhata hai, bas edge se gir gaya. Ab agar do giant numbers lagbhag same hain, jaise aur , aur tum unhe subtract karo, toh calculator kehta hai — lekin asli answer ek tiny number ho sakta tha jo pehle hi kha liya gaya. Isliye big-minus-big dangerous hai: answer chhota hai lekin galtiyan badi hain. Aur ho sakta hai agar tum left-to-right karo lekin agar tum pehle huge wale cancel karo — adding ki order answer badal deti hai!


Flashcards

What is machine epsilon for IEEE-754 double?
, yeh aur uske baad wale representable double ke beech ka gap hai.
State the floating rounding model.
jahaan .
What is catastrophic cancellation?
Do nearly-equal numbers subtract karne par significant digits ka loss; chhoti absolute errors badi relative errors ban jaati hain.
Relative-error amplification factor for ?
, jo hone par blow up ho jaata hai.
Is the subtraction step itself the source of error?
Nahi — near-equal operands ke liye yeh exact hai (Sterbenz); yeh sirf operands mein already present error ko reveal karta hai.
Stable way to get the small root of ?
Large-magnitude root compute karo, phir use karo (roots ka product).
Stable form of near 0?
.
Stable form of ?
.
Why is float addition not associative?
Har + ek aise jagah round karta hai jo running magnitude par depend karta hai; alag groupings alag tarah round karti hain.
Give values where .
→ 0 vs 1 deta hai.
What error does naive summation of terms accumulate?
, ke saath badhta hai.
What does Kahan summation achieve and how?
Sum error ko tak reduce karta hai (independent of ) ek compensation variable mein lost low-order bits carry karke.
Why is 0.1+0.2==0.3 False?
Woh teeno binary mein exactly representable nahi hain; rounded sum, rounded 0.3 se differ karta hai.
Correct way to compare floats?
Tolerance test: abs(x-y) <= atol + rtol*max(|x|,|y|) (jaise math.isclose).

Connections

Concept Map

keeps ~15-16 digits

defines

bounds

error is

scales with magnitude

revealed by subtraction

amplification factor

blows up when a approx b

each plus rounds

a+b +c not equal a+ b+c

classic case

fix by

Finite precision doubles

52-bit mantissa

Machine epsilon 2^-52

Rounding model fl x = x 1+delta

Relative error <= u

Absolute error grows with size

Catastrophic cancellation

a plus b over a minus b

Relative error explodes

Associativity failure

Order changes result

Quadratic formula loses precision

Rewrite formula to avoid subtraction