0

我所有的单元测试都在 Python 2.6.5 中成功运行;当我运行 Python 2.7.3 时,一个失败了。被测试的代码很复杂,涉及大量的浮点数工作,并在此过程中转换为十进制,如 Python 2.6 中需要的那样首先转换为 str。

在我开始挖掘之前,我想知道我是否可以有点懒惰,看看是否有人以前看过这个并且有关于搜索什么的建议。这是测试运行的结果:

======================================================================
FAIL: test_hor_tpost_winsize_inside_mm (__main__.Test_ShutterCalculator)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_ShutterCalculator.py", line 506, in test_hor_tpost_winsize_inside_mm
    self.assertEqual(o.net_width_closing_tolerance, Decimal("6.4"))
AssertionError: Decimal('6.3') != Decimal('6.4')

----------------------------------------------------------------------

这是单元测试代码test_hor_tpost_winsize_inside_mm()

490     def test_hor_tpost_winsize_inside_mm(self):
491         """
492         Same as above but test mm
493         """
494         o = self.opening
495         o.unit_of_measure = "millimeters"
496         o.formula_mode = "winsize"
497         o.mount = "inside"
498         o.given_width = Decimal("1117.6")
499         o.given_height = Decimal("2365.4")
500         o.louver_spacing = Decimal("101.6")
501         self.make4SidedFrame("9613", '9613: 2-1/2" Face Deco Z', Decimal("63.5"), Decimal("19.1"))
502         so1 = o.subopenings[(0,0)]
503         so1.fold_left = 1
504         so1.fold_right = 1
505         self.calc()
506         self.assertEqual(o.net_width_closing_tolerance, Decimal("6.4"))
507         self.assertEqual(o.net_height_closing_tolerance, Decimal("6.4"))
508         self.assertEqual(o.horizontal_shim, Decimal(".125"))  # in inches
509         self.assertEqual(o.vertical_shim, Decimal(".125"))  # in inches
510         self.assertEqual(o.width, Decimal("1069.8"))  ## 1070 converted directly from inches
511         self.assertEqual(o.height, Decimal("2317.6")) ## 2317.8 converted directy from inches
512         tpost = o.add_hor_tpost()
513         so2 = o.subopenings[(0,1)]
514         so2.fold_left = 1
515         so2.fold_right = 1
516         self.calc()
517         #self.cs()
518         self.assertEqual(o.net_width_closing_tolerance, Decimal("6.4"))
519         self.assertEqual(o.net_height_closing_tolerance, Decimal("12.7"))
520         self.assertEqual(o.horizontal_shim, Decimal(".125"))  # in inches
521         self.assertEqual(o.vertical_shim, Decimal(".125"))  # in inches
522         self.assertEqual(o.width, Decimal("1069.8"))  ## Rick had 42 but agreed that mine is right
523         self.assertEqual(o.height, Decimal("2311.3"))
524         self.assertEqual(so1.width, Decimal("1069.8"))
525         self.assertEqual(so2.width, Decimal("1069.8"))
526         self.assertEqual(so1.height, Decimal("1139.7"))  ## Rick had 44.8125 but agreed mine is right
527         self.assertEqual(so2.height, Decimal("1139.7"))
528         self.assertEqual(tpost.center_pos, Decimal("1182.7"))
529         top_panel_section = so1.panels[0].sections[(0,0)]
530         bottom_panel_section = so2.panels[0].sections[(0,0)]
531         self.assertEqual(top_panel_section.louver_count, 9)
532         self.assertEqual(bottom_panel_section.louver_count, 9)
533         self.assertEqual(top_panel_section.top_rail.width, Decimal("112.6"))  ## Rick had 4.40625, but given the changes to net
534         self.assertEqual(bottom_panel_section.bottom_rail.width, Decimal("112.7"))
535         self.assertEqual(top_panel_section.bottom_rail.width, Decimal("112.7"))
536         self.assertEqual(bottom_panel_section.top_rail.width, Decimal("112.6"))

关于在我的代码中搜索什么以找到差异来源的任何提示?

4

1 回答 1

1

Python 2.7 引入了对Decimal类和float类型的更改,以帮助提高从字符串转换时的准确性。这可能是变化的根源。

浮点数和字符串之间的转换现在在大多数平台上都可以正确舍入。这些转换发生在许多不同的地方:浮点数和复数上的 str();float 和 complex 构造函数;数字格式;使用 marshal、pickle 和 json 模块序列化和反序列化浮点数和复数;解析 Python 代码中的浮点数和虚数文字;和小数到浮点数的转换。

您可以在此处查看更改详情,在“其他语言更改”下

于 2013-01-19T15:23:31.423 回答