[Bf-blender-cvs] [59ccf1b] temp-pyapi-units: Fix loss of precision when converting string to value.
Bastien Montagne
noreply at git.blender.org
Tue Jun 17 15:06:58 CEST 2014
Commit: 59ccf1b4b119c11bd2126857d1a1da3252e9fe15
Author: Bastien Montagne
Date: Tue Jun 17 15:03:52 2014 +0200
https://developer.blender.org/rB59ccf1b4b119c11bd2126857d1a1da3252e9fe15
Fix loss of precision when converting string to value.
===================================================================
M source/blender/blenkernel/intern/unit.c
M source/tests/bl_pyapi_units.py
===================================================================
diff --git a/source/blender/blenkernel/intern/unit.c b/source/blender/blenkernel/intern/unit.c
index de6424f..8aca9f7 100644
--- a/source/blender/blenkernel/intern/unit.c
+++ b/source/blender/blenkernel/intern/unit.c
@@ -536,7 +536,7 @@ static int unit_scale_str(char *str, int len_max, char *str_tmp, double scale_pr
len_name = strlen(replace_str);
len_move = (len - (found_ofs + len_name)) + 1; /* 1+ to copy the string terminator */
- len_num = BLI_snprintf(str_tmp, TEMP_STR_SIZE, "*%g"SEP_STR, unit->scalar / scale_pref); /* # removed later */
+ len_num = BLI_snprintf(str_tmp, TEMP_STR_SIZE, "*%.9g"SEP_STR, unit->scalar / scale_pref); /* # removed later */
if (len_num > len_max)
len_num = len_max;
@@ -598,7 +598,7 @@ static int unit_find(const char *str, bUnitDef *unit)
* ...will be resolved by python.
*
* values will be split by a comma's
- * 5'2" -> 5'0.0254, 2*0.3048
+ * 5'2" -> 5*0.3048, 2*0.0254
*
* str_prev is optional, when valid it is used to get a base unit when none is set.
*
diff --git a/source/tests/bl_pyapi_units.py b/source/tests/bl_pyapi_units.py
index f472f8d..76b97ef 100644
--- a/source/tests/bl_pyapi_units.py
+++ b/source/tests/bl_pyapi_units.py
@@ -51,12 +51,9 @@ class UnitsTesting(unittest.TestCase):
for usys, utype, ref, inpt, val in self.INPUT_TESTS:
opt_val = units.to_value(usys, utype, inpt, ref)
- # Note: Internal precision of BKE's unit.c is 6 per unit (see unit_scale_str(), using just %g).
- # Since there are multiplications etc., we can't count on better than 5 in result.
- # XXX Maybe we should raise it to 8 or 9 (%.9g)?
- # Also, almostequal is not good here, precision is fixed on decimal digits, not variable with
+ # Note: almostequal is not good here, precision is fixed on decimal digits, not variable with
# magnitude of numbers (i.e. 1609.4416 ~= 1609.4456 fails even at 5 of 'places'...).
- self.assertTrue(similar_values(opt_val, val, 1e-5),
+ self.assertTrue(similar_values(opt_val, val, 1e-7),
msg="%s, %s: \"%s\" (ref: \"%s\") => %f, expected %f"
"" % (usys, utype, inpt, ref, opt_val, val))
More information about the Bf-blender-cvs
mailing list