Floats Are Weird

Floating-point arithmetic अक्सर programmers को चौंकाती है क्योंकि यह real numbers का approximation है और `exp(x) - 1` जैसे सामान्य operations में precision खो सकती है, जैसा कि `exp(x) - 1` वाले उदाहरणों और numerical noise को “cancel out” करने की techniques से दिखाया गया है। Commenters catastrophic cancellation, stable reformulations (जैसे `expm1` या वैकल्पिक algebraic forms का उपयोग), और Kahan summation या Herbie जैसे tools की चर्चा करते हैं जो बेहतर accuracy के लिए formulas को rewrite करने में मदद करते हैं। एक बड़ा विषय यह है कि floats कहाँ अनुपयुक्त हैं—खासकर money और accounting में—और numerics को सुरक्षित रूप से उपयोग करने के लिए education, libraries, तथा alternative numeric types (decimal, fixed point, big integers) कितने महत्वपूर्ण हैं.

Float व्यवहार और catastrophic cancellation

  • कई टिप्पणियाँ इस बात पर ज़ोर देती हैं कि floats इतने “weird” नहीं हैं, बल्कि reals के सीमित-परिशुद्धता वाले approximation हैं; लगभग बराबर मानों को घटाने पर अनिवार्य रूप से significant digits खो जाते हैं।
  • लेख के “magic” उदाहरण पर चर्चा होती है: function g(x) = (exp(x)-1)/log(exp(x)) numerator और denominator के साझा rounding noise को काफी हद तक cancel कर देता है, जबकि f(x) = (exp(x)-1)/x ऐसा नहीं करता।
  • यह cancellation केवल एक सीमा तक काम करता है; बहुत छोटे x के लिए यह टूट जाता है (जैसे log(exp(x)) के 0 पर round हो जाने से division by zero)।
  • कई लोग नोट करते हैं कि यह algorithmic stability का मामला है, सिर्फ “floats खराब हैं” का नहीं।

Numerical techniques & tools

  • सुझाव: zero के पास exp(x)-1 के लिए expm1(x) का उपयोग करें; Python का math.expm1 विशेष रूप से उल्लेखित है, जिसमें Kahan-प्रेरित implementation शामिल है।
  • सामान्य नियम:
    • लगभग बराबर संख्याओं को घटाने से बचें।
    • बहुत अलग magnitude वाली संख्याएँ जोड़ने से बचें।
    • sums के लिए Kahan या pairwise summation का उपयोग करें, या छोटे magnitude वाले terms को पहले जोड़ें।
    • जब मान 1 के क़रीब हों, तो 1 से offsets की गणना करें (जैसे expm1)।
  • Herbie का उल्लेख एक ऐसे tool के रूप में है जो अधिक stable reformulations सुझाता है।
  • कुछ libraries का भी उल्लेख है (जैसे decimal.js), और यह कि कई भाषाएँ IEEE-754 और C libm functions उपलब्ध कराती हैं, कभी-कभी ज्ञात inaccuracies के लिए अपने fixes के साथ।

Floats vs money and business calculations

  • मज़बूत राय: accounting के लिए floats कभी न इस्तेमाल करें; invoices, pricing, और customer-visible totals में rounding surprises से बचने के लिए integers या decimal types का उपयोग करें।
  • विपरीत राय: कई financial models (interest, bonds, options, risk simulations) के लिए floats सही tool हैं; inputs की uncertainty cent-level rounding से अधिक होती है, और integer/fixed-point approaches जटिल तथा fragile हो जाती हैं।
  • मध्यम रास्ता:
    • storage, settlement, और customer-facing arithmetic के लिए exact integer/decimal formats का उपयोग करें।
    • analytical/forecasting layers में floats का उपयोग करें, जहाँ exact cents कम महत्वपूर्ण हैं।
  • Decimal floating point (IEEE decimal, IBM hardware) और fixed-point “integer cents” को alternatives के रूप में चर्चा की गई है, जिनमें प्रत्येक के trade-offs हैं।

Education, numerics as a discipline

  • कई लोगों का तर्क है कि गलतफ़हमियाँ खराब शिक्षा से पैदा होती हैं: छात्रों को reals पढ़ाए जाते हैं, फिर बिना representation, rounding, या stability सिखाए floats दे दिए जाते हैं।
  • Numerical analysis को एक पूर्ण discipline के रूप में रेखांकित किया गया है; algorithm stability और error analysis, number format के चयन जितने ही महत्वपूर्ण हैं।