diff options
author | Jacob Gilbert <mrjacobagilbert@gmail.com> | 2018-02-07 09:00:01 -0800 |
---|---|---|
committer | Marcus Müller <marcus.mueller@ettus.com> | 2018-03-30 16:50:31 +0200 |
commit | e6adaad90462fbc03663cc81d2be3a39f35dc8cb (patch) | |
tree | 45a34e52dc0794b9a452a26708e7ecc6f897074d /gnuradio-runtime/python/gnuradio/eng_notation.py | |
parent | bdc28a843125a8cab4638c0f19ea136845ff9c74 (diff) |
runtime: address floating point roundoff concerns in num_to_str() in eng_notation - attempt 2
Diffstat (limited to 'gnuradio-runtime/python/gnuradio/eng_notation.py')
-rw-r--r-- | gnuradio-runtime/python/gnuradio/eng_notation.py | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/gnuradio-runtime/python/gnuradio/eng_notation.py b/gnuradio-runtime/python/gnuradio/eng_notation.py index d23f9005f0..12332aef7d 100644 --- a/gnuradio-runtime/python/gnuradio/eng_notation.py +++ b/gnuradio-runtime/python/gnuradio/eng_notation.py @@ -36,29 +36,30 @@ scale_factor['p'] = 1e-12 scale_factor['f'] = 1e-15 scale_factor['a'] = 1e-18 -def num_to_str (n): +def num_to_str (n, precision=6): '''Convert a number to a string in engineering notation. E.g., 5e-9 -> 5n''' m = abs(n) + format_spec = '%.' + repr(int(precision)) + 'g' if m >= 1e9: - return "%gG" % (n * 1e-9) + return '%sG' % float(format_spec % (n * 1e-9)) elif m >= 1e6: - return "%gM" % (n * 1e-6) + return '%sM' % float(format_spec % (n * 1e-6)) elif m >= 1e3: - return "%gk" % (n * 1e-3) + return '%sk' % float(format_spec % (n * 1e-3)) elif m >= 1: - return "%g" % (n) + return '%s' % float(format_spec % (n)) elif m >= 1e-3: - return "%gm" % (n * 1e3) + return '%sm' % float(format_spec % (n * 1e3)) elif m >= 1e-6: - return "%gu" % (n * 1e6) # where's that mu when you need it (unicode?) + return '%su' % float(format_spec % (n * 1e6)) elif m >= 1e-9: - return "%gn" % (n * 1e9) + return '%sn' % float(format_spec % (n * 1e9)) elif m >= 1e-12: - return "%gp" % (n * 1e12) + return '%sp' % float(format_spec % (n * 1e12)) elif m >= 1e-15: - return "%gf" % (n * 1e15) + return '%sf' % float(format_spec % (n * 1e15)) else: - return "%g" % (n) + return '%s' % float(format_spec % (n)) def str_to_num (value): |