Off Topic Because I Say So

As stated previously on this thread, before @J-M-L hijacked the comment to another thread.
dtostrf() is prone to buffer overflow, because the 'width' arg is the MINIMUM width not the maximum
A simple replacement is the print( ) function from SafeString library which protects against buffer overflows

char output[12];
double d = 33.55;
cSFA(sfOutput,output);  // wrap in a safestring
sfOutput.print(d, 5);  // print with 5 decs

If the result would overflow, an empy SafeString results and an error flag and detailed error message is available.
The direct use of dtostrf( ) will just crash your sketch in that case.

My SafeString library also has a modified print( ), the outputs a FIXED limited width string and reduces the number of decimal points when the number would exceed the FIXED width

print (double d, int decs, int width, bool forceSign=false)
Prints a double (or long/int) to this SafeString padded with spaces (left or right) and limited to the specified width.

This method can also be used for ints and longs by passing 0 for the decs.

d- the double to convert to text
decs- the preferred number of decimial places to output(limited to <7). This will be reduced automatically to fit the fixed width.
width- fixed width the output is to padded/limited to
forceSign- optional, defaults to false, if true the + sign is added for +ve numbers

dtostrf is used inside this print, but with the necessary checks on the size of the number and the necessary restrictions on number of decimal points printed and with the correct char[ ] size so that it does not overflow.

OP question was

Hi , how to use printf on arduino ?

It’s a stdout discussion

Our discussion was unrelated

My post was commenting on the dangers of the suggested alternative dtostrf( )
And my post was independent of this thread.

Not sure where. Might have been another thread? Let’s keep the focus on the question

Then restore my post to the original thread

I'm not a moderator. I suggest you bring that up to moderation.

I'm unsure how our discussion was related to the question in this thread which was "how to use printf on arduino".

The original poster made no mention of floating-point nor did they mention dtostrf.

Stay on topic. The topic is defined by the original poster.

Well at least our discussion on the other bar sport thread got you to think. if you can find the correct char[] size so that it does not overflow, I'm sure other can do too.

This post on the thread is also discussing dtostrf

and how to print to a char[ ] and floating poiint.

So my comment, particularly my last revised post, does not seem off topic compared to that comment.

P.s. my apologies @J-M-L for falsely accusing you of moving my post, seems I have @Coding_Badly to blame for that.

keep the blame if this is how you want to call it to where it has to be.

I'm responsible for the discussion being moved to the bar. (it's here: s/String discussion (again) - #59 by drmpf) because our ping pong and arguing was just bringing clutter to that discussion and that was not the main topic for OP's question .

In which case you also owe me an "apology".

I was under the impressing you moved the post.
Was I wrong?
If so who did make the decision?

I did for the original one. You owe him an apology

Seems @Coding_Badly agreed with me this was getting off topic too since he moved your post again.

I suggest we keep our long discussions about this topic in the bar., they tend to turn threads into long rants

See here How to split a string with space and store the items in array?
11 messages on key question and then it goes sideways…

Is this the s/Singing thread !

I don’t like Pavarotti :open_mouth:, but do like Taylor Swift :woozy_face:

:slight_smile: !!!

Doubles? Arduino double is Arduino float, a source of inaccuracy with every slow operation but fast and right with powers of 10.

i don't need decimal places if my work units are smaller than required by magnitudes.
If I measure micro-meters, I have millimeters plus 3 places that will get rounded off at the end, hold to mm precision. Treat numbers like you wrote them down yourself.

Arduino long counts +/- over 2 billion; safe work space 0 to 999,999,999 units.
Arduino long long counts +/- 9.22 e+18, 18 digits safe work space accuracy.

Be sure to print a decimal place in the human readable output.

AVR double is Arduino float.

(A Due is an Arduino...)

A Due is a Frankenduino, that's wot.

The newest Teensy runs at 600MHz and has an FPU!

1 Like

I know - it's brilliant for running a MLX90640

1 Like

Image processing on the fly? Multi-sensors processing?

Run the chicken coop door or a dollhouse elevator?