Welcome! Log In Create A New Profile

Advanced

Minor bug in float to string conversion

Posted by 3D-ES 
Minor bug in float to string conversion
January 12, 2014 05:34PM
When I was using the web interface to lower the nozzle,
the reported nozzle height suddenly jumped up and down.

The problem:

A float with a zero on the right of the comma dot is converted incorrectly,
when the Z height is 0.09 it is presented as 0.9 in the web interface.

The problem location:

The "convert float to string" function in RepRapFirmware.cpp at line 293:
// Float to a string.

static long precision[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
char scratchString[STRING_LENGTH];

char* ftoa(char *a, float f, int prec)
{
  if(a == NULL)
    a = scratchString;
  char *ret = a;
  long whole = (long)f;
  if(!whole && f < 0.0)
  {
	  a[0] = '-';
	  a++;
  }
  snprintf(a, STRING_LENGTH, "%d", whole);
  while (*a != '\0') a++;
  *a++ = '.';
  long decimal = abs((long)((f - (float)whole) * precision[prec]));
  snprintf(a, STRING_LENGTH, "%d", decimal);
  return ret;
}
My translation:

The float value is split in two parts, left of the comma dot and right of the comma dot.
When both parts are combined with the desired precision, the %d kills zeroes.

I have tried to fix this and my version of the code looks like this:
// Float to a string.

static const char *formatting[] = {"%d", "%d", "%02d","%03d","%04d","%05d","%06d","%07d","%08d"};
static long precision[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
char scratchString[STRING_LENGTH];

char* ftoa(char *a, float f, int prec)
{
  if(a == NULL)
    a = scratchString;
  char *ret = a;
  long whole = (long)f;
  if(!whole && f < 0.0)
  {
	  a[0] = '-';
	  a++;
  }
  snprintf(a, STRING_LENGTH, "%d", whole);
  while (*a != '\0') a++;
  *a++ = '.';
  long decimal = abs((long)((f - (float)whole) * precision[prec]));
  snprintf(a, STRING_LENGTH, formatting[prec], decimal);
  return ret;
}
The bug is a bit hard to trigger on the printer,
I have attached a source file to test it on a PC.
$ ./ftoa
Old ftoa with input 0.000 and precision 2: 0.0
Old ftoa with input 0.090 and precision 2: 0.9
Old ftoa with input 0.123 and precision 2: 0.12
New ftoa with input 0.000 and precision 2: 0.00
New ftoa with input 0.090 and precision 2: 0.09
New ftoa with input 0.123 and precision 2: 0.12
There are warnings about the %d in combination with 'long int',
the %d should probably be replaced by %ld for long int values.
$ gcc ftoa.c -o ftoa
ftoa.c:23:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat]
ftoa.c:27:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat]
ftoa.c:44:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat]

Edit: Sorry for the confusing use of the word comma instead of dot, that was probably nl_NL locale.

Edited 4 time(s). Last edit at 01/12/2014 06:11PM by 3D-ES.


RS-Online Ormerod #263, Kossel mini with Minitronics, Prusa i3 MK2
Attachments:
open | download - ftoa.c (1.7 KB)
Re: Minor bug in float to string conversion
January 12, 2014 05:50PM
Is this a "localisation" problem or does it hold for UK/us convention of using a dot to separate the decimal?
Re: Minor bug in float to string conversion
January 12, 2014 06:04PM
Re: Minor bug in float to string conversion
January 12, 2014 06:09PM
Quote
bobc
You might try something like :

snprintf(a, STRING_LENGTH, "%0*d", prec, decimal)

Wow, nice!

Thanks smiling smiley


RS-Online Ormerod #263, Kossel mini with Minitronics, Prusa i3 MK2
Re: Minor bug in float to string conversion
January 13, 2014 07:04AM
Hi 3D-ES, thanks for that. I had seen the same thing, but assumed it was caused by the bed compensation doing something strange.

An easy way to reproduce the problem is to send a command such as "G92 Z5.09" via the web interface with bed compensation turned off, then the Z value in the interface shows as 5.9.

Are you going to submit this fix to github, or would you like me to do it?

[EDIT: ignore that last bit, from Ian's comment in another thread it sounds like RRP have already accepted your fix.]

Edited 1 time(s). Last edit at 01/13/2014 07:07AM by dc42.



Large delta printer [miscsolutions.wordpress.com], E3D tool changer, Robotdigg SCARA printer, Crane Quad and Ormerod

Disclosure: I design Duet electronics and work on RepRapFirmware, [duet3d.com].
Re: Minor bug in float to string conversion
January 13, 2014 07:13AM
Yup, updated in 0.56 - [github.com]

[github.com] seems to have 0.55, 0.56 and a random dc42 version in it! Hopefully Adrian will spot this and remove the 'extras'...

Ian
RepRapPro tech support
Sorry, only registered users may post in this forum.

Click here to login