def test_geo_float_rounding(self): """ By default, the :py:module:`decimal` module uses the 'round-half-even' algorithm for rounding numbers. Since the rounding method can be set in a global context for the :py:module:`decimal` module, we want to make sure the :py:function:`openquake.shapes.geo_float` is unaffected context changes. """ decimal.getcontext().rounding = decimal.ROUND_FLOOR # changing the decimal context rounding should not affect the behavior # of geo_float self.assertEqual(-121.0000001, shapes.geo_float(-121.00000009)) # reset the global context so we don't potentially screw up other tests decimal.getcontext().rounding = decimal.ROUND_HALF_EVEN
def test_geo_float(self): """ This test exercises the :py:function:`openquake.shapes.geo_float` function. Basically, the function should take any float number and round it to a fixed number of decimal places (for example, 7 places). The rounding method used is the default for the :py:module:`decimal` module, which is ROUND_HALF_EVEN. For more information on 'half-even' rounding, there is a good explanation here: http://www.diycalculator.com/popup-m-round.shtml#A5 """ in_values = (29.000000000000004, -121.00000009, -121.00000001, 121.00000005, 121.00000006) out_values = (29.0, -121.0000001, -121.0, 121.0, 121.0000001) for i, val in enumerate(in_values): self.assertEqual(out_values[i], shapes.geo_float(in_values[i]))