0

A bug in the "dtostrf" function

Hello,

While playing with Tinkercad Circuits, I found a little bug in the function "dtostrf".

Here is the code :

// Put just an Arduino UNO as a circuit.

void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.print("Hello\n");

char acStr[20];
int nADC = 225;
// Bug : la conversion est incorrecte pour nADC = 225,
// 1.0997 est converti en 10.999
dtostrf( nADC * 5.0 / 1023.0, 8, 3, acStr); // Conversion en volts
Serial.print(acStr);
Serial.print("\n10.999 is displayed instead of 1.099\n");

Serial.print("\nA way to get the correct result : ");
float f = nADC * 5.0 / 1023.0;
sprintf(acStr, "%d.%03d \n", (int)f, (int)(f*1000)%1000);
Serial.print(acStr);
} // setup

void loop() {
digitalWrite(13, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(13, LOW);
delay(1000); // Wait for 1000 millisecond(s)
} // loop

I hope this will help to improve even more your excellent simulation of circuits.  B. Gisin.

 

 

2 comments

Please sign in to leave a comment.