@@ -389,6 +389,88 @@ def test_short_repr(self):
389
389
self .assertEqual (s , repr (float (s )))
390
390
self .assertEqual (negs , repr (float (negs )))
391
391
392
+ class RoundTestCase (unittest .TestCase ):
393
+ @unittest .skipUnless (float .__getformat__ ("double" ).startswith ("IEEE" ),
394
+ "test requires IEEE 754 doubles" )
395
+ def test_inf_nan (self ):
396
+ self .assertRaises (OverflowError , round , INF )
397
+ self .assertRaises (OverflowError , round , - INF )
398
+ self .assertRaises (ValueError , round , NAN )
399
+
400
+ @unittest .skipUnless (float .__getformat__ ("double" ).startswith ("IEEE" ),
401
+ "test requires IEEE 754 doubles" )
402
+ def test_large_n (self ):
403
+ for n in [324 , 325 , 400 , 2 ** 31 - 1 , 2 ** 31 , 2 ** 32 , 2 ** 100 ]:
404
+ self .assertEqual (round (123.456 , n ), 123.456 )
405
+ self .assertEqual (round (- 123.456 , n ), - 123.456 )
406
+ self .assertEqual (round (1e300 , n ), 1e300 )
407
+ self .assertEqual (round (1e-320 , n ), 1e-320 )
408
+ self .assertEqual (round (1e150 , 300 ), 1e150 )
409
+ self .assertEqual (round (1e300 , 307 ), 1e300 )
410
+ self .assertEqual (round (- 3.1415 , 308 ), - 3.1415 )
411
+ self .assertEqual (round (1e150 , 309 ), 1e150 )
412
+ self .assertEqual (round (1.4e-315 , 315 ), 1e-315 )
413
+
414
+ @unittest .skipUnless (float .__getformat__ ("double" ).startswith ("IEEE" ),
415
+ "test requires IEEE 754 doubles" )
416
+ def test_small_n (self ):
417
+ for n in [- 308 , - 309 , - 400 , 1 - 2 ** 31 , - 2 ** 31 , - 2 ** 31 - 1 , - 2 ** 100 ]:
418
+ self .assertEqual (round (123.456 , n ), 0.0 )
419
+ self .assertEqual (round (- 123.456 , n ), - 0.0 )
420
+ self .assertEqual (round (1e300 , n ), 0.0 )
421
+ self .assertEqual (round (1e-320 , n ), 0.0 )
422
+
423
+ @unittest .skipUnless (float .__getformat__ ("double" ).startswith ("IEEE" ),
424
+ "test requires IEEE 754 doubles" )
425
+ def test_overflow (self ):
426
+ self .assertRaises (OverflowError , round , 1.6e308 , - 308 )
427
+ self .assertRaises (OverflowError , round , - 1.7e308 , - 308 )
428
+
429
+ @unittest .skipUnless (getattr (sys , 'float_repr_style' , '' ) == 'short' ,
430
+ "applies only when using short float repr style" )
431
+ def test_previous_round_bugs (self ):
432
+ # particular cases that have occurred in bug reports
433
+ self .assertEqual (round (562949953421312.5 , 1 ),
434
+ 562949953421312.5 )
435
+ self .assertEqual (round (56294995342131.5 , 3 ),
436
+ 56294995342131.5 )
437
+ # round-half-even
438
+ self .assertEqual (round (25.0 , - 1 ), 20.0 )
439
+ self .assertEqual (round (35.0 , - 1 ), 40.0 )
440
+ self .assertEqual (round (45.0 , - 1 ), 40.0 )
441
+ self .assertEqual (round (55.0 , - 1 ), 60.0 )
442
+ self .assertEqual (round (65.0 , - 1 ), 60.0 )
443
+ self .assertEqual (round (75.0 , - 1 ), 80.0 )
444
+ self .assertEqual (round (85.0 , - 1 ), 80.0 )
445
+ self .assertEqual (round (95.0 , - 1 ), 100.0 )
446
+
447
+ @unittest .skipUnless (getattr (sys , 'float_repr_style' , '' ) == 'short' ,
448
+ "applies only when using short float repr style" )
449
+ def test_matches_float_format (self ):
450
+ # round should give the same results as float formatting
451
+ for i in range (500 ):
452
+ x = i / 1000.
453
+ self .assertEqual (float (format (x , '.0f' )), round (x , 0 ))
454
+ self .assertEqual (float (format (x , '.1f' )), round (x , 1 ))
455
+ self .assertEqual (float (format (x , '.2f' )), round (x , 2 ))
456
+ self .assertEqual (float (format (x , '.3f' )), round (x , 3 ))
457
+
458
+ for i in range (5 , 5000 , 10 ):
459
+ x = i / 1000.
460
+ self .assertEqual (float (format (x , '.0f' )), round (x , 0 ))
461
+ self .assertEqual (float (format (x , '.1f' )), round (x , 1 ))
462
+ self .assertEqual (float (format (x , '.2f' )), round (x , 2 ))
463
+ self .assertEqual (float (format (x , '.3f' )), round (x , 3 ))
464
+
465
+ for i in range (500 ):
466
+ x = random .random ()
467
+ self .assertEqual (float (format (x , '.0f' )), round (x , 0 ))
468
+ self .assertEqual (float (format (x , '.1f' )), round (x , 1 ))
469
+ self .assertEqual (float (format (x , '.2f' )), round (x , 2 ))
470
+ self .assertEqual (float (format (x , '.3f' )), round (x , 3 ))
471
+
472
+
473
+
392
474
# Beginning with Python 2.6 float has cross platform compatible
393
475
# ways to create and represent inf and nan
394
476
class InfNanTest (unittest .TestCase ):
@@ -878,6 +960,7 @@ def test_main():
878
960
IEEEFormatTestCase ,
879
961
FormatTestCase ,
880
962
ReprTestCase ,
963
+ RoundTestCase ,
881
964
InfNanTest ,
882
965
HexFloatTestCase ,
883
966
)
0 commit comments