5.4.22 · Coding › Scientific Computing (Python)
Ek computer real numbers ko finite precision ke saath store karta hai. Doubles mein lagbhag 15–16 significant decimal digits hote hain (53 bits of mantissa). Zyaadatar time yeh kaafi hota hai. Lekin do cheezein hamari school-math ki instincts ko tod deti hain:
Catastrophic cancellation : do lagbhag-equal numbers ko subtract karna un mein chupi hui tiny relative error ko amplify kar deta hai.
Associativity failure : ( a + b ) + c = a + ( b + c ) generally, kyunki har + round karta hai.
WHY care? Ek formula mathematically perfect ho sakta hai phir bhi numerically garbage ho sakta hai. Yeh jaanna ki kahan precision khatam hoti hai, aapko formula rewrite karne mein madad karta hai taaki usse preserve kiya ja sake.
Definition IEEE-754 double
Ek double ek number ko is tarah represent karta hai:
x = ± ( 1. m 1 m 2 … m 52 ) 2 × 2 e
52-bit fraction (mantissa) ke saath. 1.0 aur uske baad wale representable number ke beech ka sabse chhota gap machine epsilon kehlaata hai:
ε mach = 2 − 52 ≈ 2.22 × 1 0 − 16 .
WHY 2 − 52 ? Implicit leading 1 ke baad 52 fraction bits hone ke saath, exponent 0 par least significant bit ka weight 2 − 52 hai. Yahi 1.0 ke paas resolution hai.
Key word hai relative : error number ki magnitude ke saath scale hoti hai, absolutely nahi.
Intuition Subtraction villain kyun hai
Har stored number ek relative error ∼ u carry karta hai. Yeh ek absolute error hai jo number ki size ke proportional hai. Jab aap do bade, lagbhag-equal numbers subtract karte ho, toh answer tiny hota hai lekin absolute errors bade rehte hain — isliye result ki relative error explode kar jaati hai.
Worked example Classic: quadratic formula
Solve karo x 2 − 200000 x + 1 = 0 . Roots x = 2 a − b ± b 2 − 4 a c se, a = 1 , b = − 200000 , c = 1 ke saath.
b 2 − 4 a c = 40000000000 − 4 ≈ 199999.99999 .
"+" root: x + = 2 200000 + 199999.99999 ≈ 199999.999995 — fine (similar-sign numbers add ho rahe hain).
"−" root: x − = 2 200000 − 199999.99999 — catastrophic cancellation! Do ~200000 numbers subtract hokar ~0.00001 ban jaate hain.
Fix (WHY it works): identity x + x − = c / a use karo. Pehle stable root compute karo, phir
x − = a x + c .
Near-equals ki subtraction nahi → relative error ∼ u rehti hai.
Why this step? Multiplication/division kabhi relative error amplify nahi karte; hum dangerous subtraction ke around route karte hain.
f ( x ) = x 2 1 − cos x near x = 0 ke paas (true limit = 2 1 )
x = 1 0 − 8 par, cos x ≈ 1 − 5 × 1 0 − 17 . Store hone par, cos x exactly 1 round ho jaata hai, isliye 1 − cos x = 0 → answer 0 , 0.5 nahi. Total loss.
Fix: 1 − cos x = 2 sin 2 ( x /2 ) , isliye
f ( x ) = x 2 2 s i n 2 ( x /2 ) = 2 1 ( x /2 s i n ( x /2 ) ) 2 .
Why this step? Humne near-equals ki subtraction ko ek sin se replace kar diya jo chhote arguments ke liye accurately compute hoti hai.
+ round karta hai, aur rounding ko brackets ki parwah nahi
Floating addition commutative hai (a + b = b + a ) lekin associative nahi . Kyun? Kyunki fl ( a + b ) intermediate result ko round karta hai, aur ek alag grouping ek alag jagah round karti hai.
Worked example Ek concrete break
Maano a = 1.0 , b = 1 0 16 , c = − 1 0 16 .
( a + b ) + c : 1 + 1 0 16 round hokar 1 0 16 ho jaata hai (1 us magnitude par ∼ 2 ke gap se neeche hai), phir + c = 0 . Result 0 .
a + ( b + c ) : b + c = 0 exactly, phir + a = 1 . Result 1 .
Same numbers, sirf brackets → answers 100% differ karte hain.
Why this step? Ek chhote number ko ek bahut bade number mein add karna representable gap se neeche chala jaata hai aur vanish ho jaata hai (absorption).
Definition Kahan summation (lost bits recover karne ki trick)
s = 0.0; c = 0.0
for x in data:
y = x - c # add back last lost bit
t = s + y # may lose low bits of y
c = (t - s) - y # recover exactly what was lost
s = t
WHY it works: (t - s) woh part hai y ka jo actually add hua ; y subtract karne se woh negated part milta hai jo drop hua tha. Hum use next iteration mein carry karte hain.
Common mistake "Floats compare karne ke liye bas
== use karo."
Why it feels right: math mein, equal computations equal numbers dete hain. Why it's wrong: 0.1 + 0.2 == 0.3 False hai kyunki 0.1 , 0.2 , 0.3 binary mein exactly representable nahi hain; rounded sum, rounded 0.3 se ∼ 1 0 − 17 differ karta hai.
Fix: tolerance ke saath compare karo, jaise abs(x-y) <= atol + rtol*max(|x|,|y|) (Python ka math.isclose).
Common mistake "Zyaada terms / higher precision hamesha accuracy fix kar deti hai."
Why it feels right: zyaada kaam = zyaada accuracy, usually. Why it's wrong: agar formula cancel karta hai, toh terms add karna ya float128 use karna sirf catastrophe ko delay karta hai; expression ki conditioning asli problem hai.
Fix: reformulate karo (identities use karo jaise 1 − cos x = 2 sin 2 ( x /2 ) , ya x + 1 − x = x + 1 + x 1 ) cancellation avoid karne ke liye.
Common mistake "Floats associative hain kyunki math mein addition associative hai."
Why it feels right: real-number axioms. Why it's wrong: rounding per operation hoti hai, isliye grouping change karta hai ki kaun se bits survive karte hain. Upar wala example 0 vs 1 deta hai.
Fix: careful order mein sum karo ya Kahan use karo; kabhi mat assume karo ki reorderings free hain.
Recall Active recall — answers cover karo
a − b mein relative-error amplifier kya hai? → ∣ a − b ∣ ∣ a ∣ + ∣ b ∣ .
Kya near-equal numbers ki float subtraction khud inaccurate hoti hai? → Nahi, yeh exact hai; yeh sirf prior error ko expose karti hai.
( a + b ) + c = a + ( b + c ) kyun? → Har + ek aise jagah round karta hai jo partial sum ki magnitude par depend karta hai.
Chhote quadratic root ka fix? → x − = c / ( a x + ) .
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. 12340000 likho aur 1 add karo — calculator phir bhi 12340000 dikhata hai, 1 bas edge se gir gaya . Ab agar do giant numbers lagbhag same hain, jaise 12340000 aur 12340000 , aur tum unhe subtract karo, toh calculator 0 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 1 + (huge) + ( − huge ) 0 ho sakta hai agar tum left-to-right karo lekin 1 agar tum pehle huge wale cancel karo — adding ki order answer badal deti hai!
"Near-twins subtract karo, digits kho do. Sums reorder karo, answer kho do."
Quadratic ke liye: "b ke saath same sign safe hai; doosra root, divide karo." (Same-sign root compute karo, doosra c / ( a x ) se nikalo.)
What is machine epsilon for IEEE-754 double? 2 − 52 ≈ 2.22 × 1 0 − 16 , yeh 1.0 aur uske baad wale representable double ke beech ka gap hai.
State the floating rounding model. fl ( x ) = x ( 1 + δ ) jahaan ∣ δ ∣ ≤ u = 2 1 ε mach .
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 a − b ? ∣ a − b ∣ ∣ a ∣ + ∣ b ∣ , jo a → b 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 x 2 + b x + c ? Large-magnitude root compute karo, phir x 2 = c / ( a x 1 ) use karo (roots ka product).
Stable form of 1 − cos x near 0? 2 sin 2 ( x /2 ) .
Stable form of x + 1 − x ? 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 ( a + b ) + c = a + ( b + c ) . a = 1 , b = 1 0 16 , c = − 1 0 16 → 0 vs 1 deta hai.
What error does naive summation of n terms accumulate? ∼ ( n − 1 ) u ∑ ∣ x i ∣ , n ke saath badhta hai.
What does Kahan summation achieve and how? Sum error ko ∼ u tak reduce karta hai (independent of n ) 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 ∼ 1 0 − 17 differ karta hai.
Correct way to compare floats? Tolerance test: abs(x-y) <= atol + rtol*max(|x|,|y|) (jaise math.isclose).
Rounding model fl x = x 1+delta
Absolute error grows with size
Catastrophic cancellation
Quadratic formula loses precision
Rewrite formula to avoid subtraction