@@ -65,23 +65,37 @@ class Context(object):
6565 absolute value in the range [0.5, 1.0), and that ``emin`` takes
6666 the subnormal range into account when ``subnormalize`` is ``True``.
6767 """
68+
6869 # Contexts are supposed to be immutable. We make the attributes
6970 # of a Context private, and provide properties to access them in
7071 # order to discourage users from trying to set the attributes
7172 # directly.
7273
73- def __new__ (cls , precision = None , emin = None , emax = None , subnormalize = None ,
74- rounding = None ):
75- if precision is not None and \
76- not (PRECISION_MIN <= precision <= PRECISION_MAX ):
77- raise ValueError ("Precision p should satisfy %d <= p <= %d." %
78- (PRECISION_MIN , PRECISION_MAX ))
74+ def __new__ (
75+ cls ,
76+ precision = None ,
77+ emin = None ,
78+ emax = None ,
79+ subnormalize = None ,
80+ rounding = None ,
81+ ):
82+ if precision is not None and not (
83+ PRECISION_MIN <= precision <= PRECISION_MAX
84+ ):
85+ raise ValueError (
86+ "Precision p should satisfy %d <= p <= %d."
87+ % (PRECISION_MIN , PRECISION_MAX )
88+ )
7989 if emin is not None and not EMIN_MIN <= emin <= EMIN_MAX :
80- raise ValueError ("exponent bound emin should satisfy "
81- "%d <= emin <= %d" % (EMIN_MIN , EMIN_MAX ))
90+ raise ValueError (
91+ "exponent bound emin should satisfy "
92+ "%d <= emin <= %d" % (EMIN_MIN , EMIN_MAX )
93+ )
8294 if emax is not None and not EMAX_MIN <= emax <= EMAX_MAX :
83- raise ValueError ("exponent bound emax should satisfy "
84- "%d <= emax <= %d" % (EMAX_MIN , EMAX_MAX ))
95+ raise ValueError (
96+ "exponent bound emax should satisfy "
97+ "%d <= emax <= %d" % (EMAX_MIN , EMAX_MAX )
98+ )
8599 if rounding is not None :
86100 rounding = RoundingMode (rounding )
87101 if subnormalize is not None and subnormalize not in [False , True ]:
@@ -101,34 +115,45 @@ def __add__(self, other):
101115 precedence."""
102116
103117 return Context (
104- precision = (other .precision
105- if other .precision is not None
106- else self .precision ),
118+ precision = (
119+ other .precision
120+ if other .precision is not None
121+ else self .precision
122+ ),
107123 emin = other .emin if other .emin is not None else self .emin ,
108124 emax = other .emax if other .emax is not None else self .emax ,
109- subnormalize = (other .subnormalize
110- if other .subnormalize is not None
111- else self .subnormalize ),
112- rounding = (other .rounding
113- if other .rounding is not None
114- else self .rounding ),
125+ subnormalize = (
126+ other .subnormalize
127+ if other .subnormalize is not None
128+ else self .subnormalize
129+ ),
130+ rounding = (
131+ other .rounding if other .rounding is not None else self .rounding
132+ ),
115133 )
116134
117135 def __eq__ (self , other ):
118136 return (
119- self .precision == other .precision and
120- self .emin == other .emin and
121- self .emax == other .emax and
122- self .subnormalize == other .subnormalize and
123- self .rounding == other .rounding
137+ self .precision == other .precision
138+ and self .emin == other .emin
139+ and self .emax == other .emax
140+ and self .subnormalize == other .subnormalize
141+ and self .rounding == other .rounding
124142 )
125143
126144 def __ne__ (self , other ):
127145 return not (self == other )
128146
129147 def __hash__ (self ):
130- return hash ((self .precision , self .emin , self .emax ,
131- self .subnormalize , self .rounding ))
148+ return hash (
149+ (
150+ self .precision ,
151+ self .emin ,
152+ self .emax ,
153+ self .subnormalize ,
154+ self .rounding ,
155+ )
156+ )
132157
133158 @property
134159 def precision (self ):
@@ -153,16 +178,16 @@ def subnormalize(self):
153178 def __repr__ (self ):
154179 args = []
155180 if self .precision is not None :
156- args .append (' precision=%r' % self .precision )
181+ args .append (" precision=%r" % self .precision )
157182 if self .emax is not None :
158- args .append (' emax=%r' % self .emax )
183+ args .append (" emax=%r" % self .emax )
159184 if self .emin is not None :
160- args .append (' emin=%r' % self .emin )
185+ args .append (" emin=%r" % self .emin )
161186 if self .subnormalize is not None :
162- args .append (' subnormalize=%r' % self .subnormalize )
187+ args .append (" subnormalize=%r" % self .subnormalize )
163188 if self .rounding is not None :
164- args .append (' rounding=%r' % self .rounding )
165- return ' Context(%s)' % ', ' .join (args )
189+ args .append (" rounding=%r" % self .rounding )
190+ return " Context(%s)" % ", " .join (args )
166191
167192 __str__ = __repr__
168193
@@ -191,9 +216,7 @@ def __exit__(self, *args):
191216# WideExponentContext has the largest exponent range allowed
192217# by MPFR; precision and rounding mode are not specified.
193218WideExponentContext = Context (
194- emax = EMAX_MAX ,
195- emin = EMIN_MIN ,
196- subnormalize = False ,
219+ emax = EMAX_MAX , emin = EMIN_MIN , subnormalize = False ,
197220)
198221
199222
@@ -328,9 +351,13 @@ def _apply_function_in_context(cls, f, args, context):
328351 # if bf is zero but ternary is nonzero, the underflow
329352 # flag will already have been set by mpfr_check_range;
330353 underflow = (
331- mpfr .mpfr_number_p (bf ) and
332- not mpfr .mpfr_zero_p (bf ) and
333- mpfr .mpfr_get_exp (bf ) < context .precision - 1 + context .emin )
354+ mpfr .mpfr_number_p (bf )
355+ and not mpfr .mpfr_zero_p (bf )
356+ and (
357+ mpfr .mpfr_get_exp (bf )
358+ < context .precision - 1 + context .emin
359+ )
360+ )
334361 if underflow :
335362 mpfr .mpfr_set_underflow ()
336363 ternary = mpfr .mpfr_subnormalize (bf , ternary , rounding )
0 commit comments