MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / _patch_float_precision

Function _patch_float_precision

tools/sampcd_processor.py:101–173  ·  view source on GitHub ↗
(digits)

Source from the content-addressed store, hash-verified

99
100
101def _patch_float_precision(digits):
102 from xdoctest import checker
103
104 pattern_number = re.compile(
105 r"""
106 (?:
107 (?:(?<=[\s*\[\(\&#x27;\"\:])|^) # number starts
108 (?: # int/float or complex-real
109 (?:
110 [+-]?
111 (?:
112 (?: \d*\.\d+) | (?: \d+\.?) # int/float
113 )
114 )
115 (?:[Ee][+-]?\d+)?
116 )
117 (?: # complex-imag
118 (?:
119 (?:
120 [+-]?
121 (?:
122 (?: \d*\.\d+) | (?: \d+\.?)
123 )
124 )
125 (?:[Ee][+-]?\d+)?
126 )
127 (?:[Jj])
128 )?
129 )
130 """,
131 re.VERBOSE | re.DOTALL,
132 )
133
134 _check_output = checker.check_output
135
136 def _sub_number(match_obj, digits):
137 match_str = match_obj.group()
138
139 if 'j' in match_str or 'J' in match_str:
140 try:
141 match_num = complex(match_str)
142 except ValueError:
143 return match_str
144
145 return (
146 str(
147 complex(
148 round(match_num.real, digits),
149 round(match_num.imag, digits),
150 )
151 )
152 .strip('(')
153 .strip(')')
154 )
155 else:
156 try:
157 return str(round(float(match_str), digits))
158 except ValueError:

Callers 1

_patch_xdoctestMethod · 0.85

Calls 1

compileMethod · 0.45

Tested by

no test coverage detected