Fix multi-units drawing re precision handling.
authorBastien Montagne <montagne29@wanadoo.fr>
Tue, 1 Aug 2017 14:35:07 +0000 (16:35 +0200)
committerBastien Montagne <montagne29@wanadoo.fr>
Tue, 1 Aug 2017 14:42:35 +0000 (16:42 +0200)
This is still far from prefect, but yet much better than what we had so
far (more consistent with inheritent precision available in floats).

Note that this fixes some (currently commented out) units unittests, and
requires adjusting some others, will be done in next commit.

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

index f97b89f1fd5a9742c464b426f0d95f932932b8e9..8606da0743b6a71c976ab04aa8431f5084462729 100644 (file)
@@ -375,8 +375,7 @@ static size_t unit_as_string(char *str, int len_max, double value, int prec, con
        /* 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);
+       prec -= integer_digits_d(value_conv);
        CLAMP(prec, 0, 6);
 
        /* Convert to a string */
@@ -449,12 +448,15 @@ size_t bUnit_AsString(char *str, int len_max, double value, int prec, int system
                        size_t i;
                        i = unit_as_string(str, len_max, value_a, prec, usys, unit_a, '\0');
 
+                       prec -= integer_digits_d(value_a / unit_b->scalar) - integer_digits_d(value_b / unit_b->scalar);
+                       prec = max_ii(prec, 0);
+
                        /* is there enough space for at least 1 char of the next unit? */
                        if (i + 2 < len_max) {
                                str[i++] = ' ';
 
                                /* use low precision since this is a smaller unit */
-                               i += unit_as_string(str + i, len_max - i, value_b, prec ? 1 : 0, usys, unit_b, '\0');
+                               i += unit_as_string(str + i, len_max - i, value_b, prec, usys, unit_b, '\0');
                        }
                        return i;
                }
index a013f75f9e8c59bec98a261601d5860d42068cd2..7ab4e1d9c35576472ae830866f888b09e0f4f0f7 100644 (file)
@@ -2228,20 +2228,24 @@ void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int
                        else {
                                int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision;
                                if (use_exp_float) {
-                                       const int l10 = (value == 0.0f) ? 0 : (int)log10(fabs(value));
-                                       if (l10 < -6 || l10 > 12) {
+                                       const int int_digits_num = integer_digits_f(value);
+                                       if (int_digits_num < -6 || int_digits_num > 12) {
                                                BLI_snprintf(str, maxlen, "%.*g", prec, value);
                                                if (r_use_exp_float) {
                                                        *r_use_exp_float = true;
                                                }
                                        }
                                        else {
-                                               prec -= l10 + (int)(l10 < 0);
+                                               prec -= int_digits_num;
                                                CLAMP(prec, 0, UI_PRECISION_FLOAT_MAX);
                                                BLI_snprintf(str, maxlen, "%.*f", prec, value);
                                        }
                                }
                                else {
+#if 0                          /* TODO, but will likely break some stuff, so better after 2.79 release. */
+                                       prec -= int_digits_num;
+                                       CLAMP(prec, 0, UI_PRECISION_FLOAT_MAX);
+#endif
                                        BLI_snprintf(str, maxlen, "%.*f", prec, value);
                                }
                        }