TestCenter Reference
Math.py
Go to the documentation of this file.
1 #
2 # Copyright 2010, MeVis Medical Solutions AG
3 #
4 # The user may use this file in accordance with the license agreement provided with
5 # the Software or, alternatively, in accordance with the terms contained in a
6 # written agreement between the user and MeVis Medical Solutions AG.
7 #
8 # For further information use the contact form at https://www.mevislab.de/contact
9 #
10 
11 from builtins import range
12 
13 # Already ported to Python 3
14 
16 
17 # -- system imports ----------------------------------------------------------------------------{{{-
18 from numbers import Number
19 # ----------------------------------------------------------------------------------------------}}}-
20 
21 
22 def _areInstancesOfNumber( a, b ):
23  return ( isinstance( a, Number ) and isinstance( b, Number ) )
24 
25 def _areOfTypeListOrTuple( a, b ):
26  return ( ( type( a ) in ( list, tuple ) ) and ( type( b ) in ( list, tuple ) ))
27 
28 def _haveEqualLength( a, b ):
29  return ( len( a ) == len( b ) )
30 
31 
32 def _areEqual( a, b, epsilon ):
33  return ( abs( a - b ) < epsilon )
34 
35 def _isLessThan( a, b ):
36  return ( a < b )
37 
38 
39 # -- def compareFloatEqual ---------------------------------------------------------------------{{{-
40 
45 def compareFloatEqual ( a, b, epsilon = 0.0001 ):
46  result = False
47  if ( _areInstancesOfNumber( a, b ) ):
48  result = _areEqual( a, b, epsilon )
49 
50  elif ( _areOfTypeListOrTuple( a, b ) ):
51  if ( _haveEqualLength( a, b ) ):
52  result = True
53  for i in range( 0, len( a ) ):
54  result = compareFloatEqual ( a[i], b[i], epsilon )
55  if ( not result ):
56  break
57 
58  else:
59  Logging_error( "Only lists or tuples of equal length can be compared!" )
60 
61  else:
62  Logging_error( "Only numbers can be compared!" )
63 
64  return result
65 
66 # ----------------------------------------------------------------------------------------------}}}-
67 
68 # -- def compareFloatLessThan ------------------------------------------------------------------{{{-
69 
73 def compareFloatLessThan ( a, b ):
74  result = False
75  if ( _areInstancesOfNumber( a, b ) ):
76  result = _isLessThan( a, b )
77 
78  elif ( _areOfTypeListOrTuple( a, b ) ):
79  if ( _haveEqualLength( a, b ) ):
80  for i in range( 0, len( a ) ):
81  result = compareFloatLessThan ( a[i], b[i] )
82  if ( not result ):
83  break
84 
85  else:
86  Logging_error( "Only lists or tuples of equal length can be compared!" )
87 
88  else:
89  Logging_error( "Only numbers can be compared!" )
90 
91  return result
92 
93 # ----------------------------------------------------------------------------------------------}}}-
94 
95 # -- def compareFloatLessThanOrEqual -----------------------------------------------------------{{{-
96 
102 def compareFloatLessThanOrEqual ( a, b, epsilon = 0.0001 ):
103  return ( compareFloatLessThan( a, b ) or compareFloatEqual( a, b, epsilon ) )
104 
105 # ----------------------------------------------------------------------------------------------}}}-
106 
107 from .Logging import error as Logging_error, warning as Logging_warning, info as Logging_info
def compareFloatEqual(a, b, epsilon=0.0001)
Compare the given float values or lists of float values for equality.
Definition: Math.py:45
def compareFloatLessThan(a, b)
Compare the two given float values if the first is less than the second.
Definition: Math.py:73
def compareFloatLessThanOrEqual(a, b, epsilon=0.0001)
Compare the two given float values if the first is less than or equal to the second.
Definition: Math.py:102