From 7f855d113bef60808dd55735e70ec86646582de1 Mon Sep 17 00:00:00 2001 From: Stefania Alborghetti Date: Fri, 22 May 2015 17:38:46 -0500 Subject: [PATCH] cqlsh: Improve default float precision behavior Patch by Stefania Alborghetti; reviewed by Tyler Hobbs for CASSANDRA-9224 --- CHANGES.txt | 1 + pylib/cqlshlib/formatting.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index ca125224be..a4430c0bb1 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 2.1.6 + * (cqlsh) Better float precision by default (CASSANDRA-9224) * Improve estimated row count (CASSANDRA-9107) * Optimize range tombstone memory footprint (CASSANDRA-8603) * Use configured gcgs in anticompaction (CASSANDRA-9397) diff --git a/pylib/cqlshlib/formatting.py b/pylib/cqlshlib/formatting.py index e9d22fda4d..2a99e23a71 100644 --- a/pylib/cqlshlib/formatting.py +++ b/pylib/cqlshlib/formatting.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import sys import re import time import calendar @@ -144,6 +145,13 @@ def format_floating_point_type(val, colormap, float_precision, **_): elif math.isinf(val): bval = 'Infinity' else: + exponent = int(math.log10(abs(val))) if abs(val) > sys.float_info.epsilon else -sys.maxint -1 + if -4 <= exponent < float_precision: + # when this is true %g will not use scientific notation, + # increasing precision should not change this decision + # so we increase the precision to take into account the + # digits to the left of the decimal point + float_precision = float_precision + exponent + 1 bval = '%.*g' % (float_precision, val) return colorme(bval, colormap, 'float')