From 52ced6bf5d5f0c509f5f1e72a0cf74d119c6fa5e Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Sun, 26 Mar 2017 18:21:17 -0500 Subject: [PATCH] Make BigIntegerKiller's warning message more precise. --- sumpy/codegen.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/sumpy/codegen.py b/sumpy/codegen.py index 0cb14028..62bcc3cb 100644 --- a/sumpy/codegen.py +++ b/sumpy/codegen.py @@ -437,14 +437,16 @@ from loopy.tools import is_integer class BigIntegerKiller(CSECachingMapperMixin, IdentityMapper): - def __init__(self, warn_on_first=True, int_type=np.int64, float_type=np.float64): + def __init__(self, warn_on_digit_loss=True, int_type=np.int64, + float_type=np.float64): IdentityMapper.__init__(self) - self.bits = 64 - self.warn = warn_on_first + self.warn = warn_on_digit_loss self.float_type = float_type self.iinfo = np.iinfo(int_type) def map_constant(self, expr): + """Convert integer values not within the range of `self.int_type` to float. + """ if not is_integer(expr): return IdentityMapper.map_constant(self, expr) @@ -452,11 +454,15 @@ class BigIntegerKiller(CSECachingMapperMixin, IdentityMapper): return expr if self.warn: - from warnings import warn - warn("Converting '%d' to float: this may result in " - "loss of digits." % expr) + expr_as_float = self.float_type(expr) + if int(expr_as_float) != int(expr): + from warnings import warn + warn("Converting to '%d' to '%s' loses digits" + % (expr, self.float_type.__name__)) + # Suppress further warnings. self.warn = False + return expr_as_float return self.float_type(expr) -- GitLab