Fix two issues with recent changes to number display while editing them.
authorBastien Montagne <montagne29@wanadoo.fr>
Mon, 31 Jul 2017 13:40:26 +0000 (15:40 +0200)
committerBastien Montagne <montagne29@wanadoo.fr>
Mon, 31 Jul 2017 13:43:14 +0000 (15:43 +0200)
* Numbers with units (especially, angles) where not handled correctly
regarding number of significant digits (spotted by @brecht in T52222
comment, thanks).
* Zero value has no valid log, need to take that into account!

source/blender/blenkernel/intern/unit.c
source/blender/editors/interface/interface.c

index c0a373395dcd01cc507365d0db4fb74c9dd7e95d..f97b89f1fd5a9742c464b426f0d95f932932b8e9 100644 (file)
@@ -372,6 +372,13 @@ static size_t unit_as_string(char *str, int len_max, double value, int prec, con
 
        value_conv = value / unit->scalar;
 
+       /* Adjust precision to expected number of significant digits.
+        * Note that here, we shall not have to worry about very big/small numbers, units are expected to replace
+        * 'scientific notation' in those cases. */
+       const int l10 = (value_conv == 0.0) ? 0 : (int)log10(fabs(value_conv));
+       prec -= l10 + (int)(l10 < 0);
+       CLAMP(prec, 0, 6);
+
        /* Convert to a string */
        len = BLI_snprintf_rlen(str, len_max, "%.*f", prec, value_conv);
 
index 0dc7c6ccbecc685f1fe09ccaf27eac1c20a5c518..a013f75f9e8c59bec98a261601d5860d42068cd2 100644 (file)
@@ -2226,9 +2226,9 @@ void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int
                                ui_get_but_string_unit(but, str, maxlen, value, false, float_precision);
                        }
                        else {
-                               const int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision;
+                               int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision;
                                if (use_exp_float) {
-                                       const int l10 = (int)log10(fabs(value));
+                                       const int l10 = (value == 0.0f) ? 0 : (int)log10(fabs(value));
                                        if (l10 < -6 || l10 > 12) {
                                                BLI_snprintf(str, maxlen, "%.*g", prec, value);
                                                if (r_use_exp_float) {
@@ -2236,7 +2236,9 @@ void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int
                                                }
                                        }
                                        else {
-                                               BLI_snprintf(str, maxlen, "%.*f", prec - l10 + (int)(l10 < 0), value);
+                                               prec -= l10 + (int)(l10 < 0);
+                                               CLAMP(prec, 0, UI_PRECISION_FLOAT_MAX);
+                                               BLI_snprintf(str, maxlen, "%.*f", prec, value);
                                        }
                                }
                                else {