@@ -2219,7 +2219,42 @@ def is_root_correctly_rounded(x: Fraction, root: float) -> bool:
2219
2219
statistics ._sqrt_frac (1 , 0 )
2220
2220
2221
2221
# The result is well defined if both inputs are negative
2222
- self .assertAlmostEqual (statistics ._sqrt_frac (- 2 , - 1 ), math .sqrt (2.0 ))
2222
+ self .assertEqual (statistics ._sqrt_frac (- 2 , - 1 ), statistics ._sqrt_frac (2 , 1 ))
2223
+
2224
+ def test_deci_sqrt (self ):
2225
+ root : Decimal
2226
+ numerator : int
2227
+ denominator : int
2228
+
2229
+ for root , numerator , denominator in [
2230
+ (Decimal ('0.4481904599041192673635338663' ), 200874688349065940678243576378 , 1000000000000000000000000000000 ), # No adj
2231
+ (Decimal ('0.7924949131383786609961759598' ), 628048187350206338833590574929 , 1000000000000000000000000000000 ), # Adj up
2232
+ (Decimal ('0.8500554152289934068192208727' ), 722594208960136395984391238251 , 1000000000000000000000000000000 ), # Adj down
2233
+ ]:
2234
+ with decimal .localcontext (decimal .DefaultContext ):
2235
+ self .assertEqual (statistics ._deci_sqrt (numerator , denominator ), root )
2236
+
2237
+ # Confirm expected root with a quad precision decimal computation
2238
+ with decimal .localcontext (decimal .DefaultContext ) as ctx :
2239
+ ctx .prec *= 4
2240
+ high_prec_root = (Decimal (numerator ) / Decimal (denominator )).sqrt ()
2241
+ with decimal .localcontext (decimal .DefaultContext ):
2242
+ target_root = + high_prec_root
2243
+ self .assertEqual (root , target_root )
2244
+
2245
+ # Verify that corner cases and error handling match Decimal.sqrt()
2246
+ self .assertEqual (statistics ._deci_sqrt (0 , 1 ), 0.0 )
2247
+ with self .assertRaises (decimal .InvalidOperation ):
2248
+ statistics ._deci_sqrt (- 1 , 1 )
2249
+ with self .assertRaises (decimal .InvalidOperation ):
2250
+ statistics ._deci_sqrt (1 , - 1 )
2251
+
2252
+ # Error handling for zero denominator matches that for Fraction(1, 0)
2253
+ with self .assertRaises (ZeroDivisionError ):
2254
+ statistics ._deci_sqrt (1 , 0 )
2255
+
2256
+ # The result is well defined if both inputs are negative
2257
+ self .assertEqual (statistics ._deci_sqrt (- 2 , - 1 ), statistics ._deci_sqrt (2 , 1 ))
2223
2258
2224
2259
2225
2260
class TestStdev (VarianceStdevMixin , NumericTestCase ):
0 commit comments