Created
July 25, 2015 23:59
-
-
Save wware/aa3fb30b34b06a2314b6 to your computer and use it in GitHub Desktop.
An Arduino function to print a double-precision floating point number via Serial. Not exhaustively tested, use at your own risk.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void print_double(double val) | |
{ | |
char buf[10]; | |
int neg = (val < 0); | |
int exponent = 0; | |
if (val == 0.0) { | |
Serial.print("0.0"); | |
return; | |
} | |
if (neg) | |
val = -val; | |
while (val >= 10.0) { | |
val *= 0.1; | |
exponent++; | |
} | |
while (val < 1.0) { | |
val *= 10.0; | |
exponent--; | |
} | |
if (neg) | |
Serial.print("-"); | |
Serial.print(int(val)); | |
Serial.print("."); | |
sprintf(buf, "%06u", | |
(unsigned int) ((val - int(val)) * 1000000)); | |
Serial.print(buf); | |
if (exponent != 0) { | |
Serial.print('e'); | |
Serial.print(exponent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment