From 5a9a134ae46ec886c8cc02d00976769c269f7b53 Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Tue, 6 Mar 2012 12:08:43 +0100 Subject: [PATCH] gcode_parse.c: review decfloat_to_int() yet again. This time, raise range at the cost of some precision for the newly explored areas. --- gcode_parse.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gcode_parse.c b/gcode_parse.c index 7e7c4fd..65fb295 100644 --- a/gcode_parse.c +++ b/gcode_parse.c @@ -57,9 +57,9 @@ extern const uint32_t powers[]; // defined in sermsg.c /// convert a floating point input value into an integer with appropriate scaling. /// \param *df pointer to floating point structure that holds fp value to convert /// \param multiplicand multiply by this amount during conversion to integer -/// \param divide_by_1000 divide by 1000 during conversion to integer /// -/// lots of work has been done in exploring this function's limitations in terms of overflow and rounding +/// Tested for up to 42'000 mm (accurate), 420'000 mm (precision 10 um) and +/// 4'200'000 mm (precision 100 um). static int32_t decfloat_to_int(decfloat *df, uint32_t multiplicand) { uint32_t r = df->mantissa; uint8_t e = df->exponent; @@ -68,6 +68,13 @@ static int32_t decfloat_to_int(decfloat *df, uint32_t multiplicand) { if (e) e--; + // This raises range for mm by factor 1000 and for inches by factor 100. + // It's a bit expensive, but we should have the time while parsing. + while (e && multiplicand % 10 == 0) { + multiplicand /= 10; + e--; + } + r *= multiplicand; if (e) r = (r + powers[e] / 2) / powers[e];