

The "Arduino Way" is designed to make things "easy to use" for people who don't know much about programming. Here's my observation based on about six months reading this forum: I'll show my take on a possible implementation of the function in my next post.


With proper implementation of rounding, the output will look something like this: Serial.print("tan(") Serial.print(HALF_PI, 6) Serial.print(") = ") Note that tan(pi/2) may not result in INF due Serial.print("INFINITY*0 = ") Serial.println(float2s(f, 5)) Serial.println() Serial.print("sqrt(-1) = ") Serial.println(float2s(f, 3)) Serial.print(i) Serial.print(": ") Serial.println(float2s(f, i)) Serial.print(f,4) Serial.print(" = ") Serial.println(float2s(f, 4)) Serial.println() įor loops 1 and 2, note that floating point Rounding with leading zeros in the fractional part Serial.print("0.0 = ") Serial.println(float2s(f)) Serial.println() Test of function that builds a C-style "string" for a floating point
#Arduino println long how to
I may wont to (or have to) refer back to comments in the code to tell me what the numbers mean (and how to see whether there are any errors). I also like to make test program outputs verbose enough to let me see where the heck the numbers came from. Additional comments to help others understand anything is often considered lagniappe (unless it's for public release, or it's a commercial environment and the code must be supported for future debugging or enhancement). If I am going to use a program even one day after I write it, I put a few comments to remind my tired old brain what the heck I did (and, maybe, even why the heck I did it). Rounding them to three decimal places gives 2.000 E+20 and 1.999 E+20, respectively. Rounding 1.999496 E+20 do four decimal places gives the same answer. "Corner" tests like rounding 1.999518 E+20 to four decimal places (five significant digits) gives 1.9995 E+20. At least we can present correctly rounded values. I mean, we have enough trouble getting through to inexperienced people that floating point is about significant digits, not decimal places. I think that rounding should be done to make it consistent with standard C (and C++) output functions. And, maybe, actually to learn some fundamentals about C programming and arrays using array names in function calls. Having the user pass the name of an array and the length of the array (like strncpy, for example) would be easy to implement, but would make the user actually think about what is going on. Having to dedicate 16 bytes of RAM for this function might be an issue with a resource-limited processor. There is no recursion, and "barefoot" Arduino code doesn't have threads or anything else that requires re-entrance. Well, the issue to me is not that it is overwritten by subsequent calls. Your "TODO" remove the static char s as multiple calls will overwrite it. Īll remarks and additional tests are welcome. Serial.println(float2s(f, 6)) // "fails" as PI is not precise enough. Sprintf(&s, format, whole, part, exponent) Īnd a program to test various values float f Long part = long((g-whole)*multiplier) // # digits Long multiplier = pow(10, digits) // fix int => long

However the standard print() (~arduino-0021\hardware\arduino\cores\arduino\print.cpp) does only print a 'valid' value if abs(f) 6) digits = 6 According to - a float in an Arduino sketch has a range from approx 10E-38 to 10E+38.
