Eduard Sjoukes
/
FloatFormatSpecifier
Use the format specifiers with floatingpoint numbers
main.cpp@0:727af1f2cbe4, 2010-12-21 (annotated)
- Committer:
- Eduard
- Date:
- Tue Dec 21 16:55:20 2010 +0000
- Revision:
- 0:727af1f2cbe4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Eduard | 0:727af1f2cbe4 | 1 | /************************************************************************* |
Eduard | 0:727af1f2cbe4 | 2 | * Test of "format specifiers" and display those on a 20x4 Optrex LCD |
Eduard | 0:727af1f2cbe4 | 3 | * This LCD needs a different timing than a normal LCD, see the |
Eduard | 0:727af1f2cbe4 | 4 | * OptrexLCD program for more information. |
Eduard | 0:727af1f2cbe4 | 5 | * Probally this program will work also with a "normal" 20x4 LCD |
Eduard | 0:727af1f2cbe4 | 6 | * if not replace the OptrexLCD library with the original TextLCD library. |
Eduard | 0:727af1f2cbe4 | 7 | ************************************************************************** |
Eduard | 0:727af1f2cbe4 | 8 | */ |
Eduard | 0:727af1f2cbe4 | 9 | #include "mbed.h" |
Eduard | 0:727af1f2cbe4 | 10 | #include "OptrexLCD.h" |
Eduard | 0:727af1f2cbe4 | 11 | |
Eduard | 0:727af1f2cbe4 | 12 | DigitalOut BlinkLed(LED4); |
Eduard | 0:727af1f2cbe4 | 13 | |
Eduard | 0:727af1f2cbe4 | 14 | TextLCD lcd(p10, p12, p15, p16, p17, p18, TextLCD::LCD20x4 ); // rs, e, d0-d3 |
Eduard | 0:727af1f2cbe4 | 15 | |
Eduard | 0:727af1f2cbe4 | 16 | void WaitRefres(int a) // procedure for showing the result for 'a' seconds before getting the next datatypes |
Eduard | 0:727af1f2cbe4 | 17 | { |
Eduard | 0:727af1f2cbe4 | 18 | wait(a); |
Eduard | 0:727af1f2cbe4 | 19 | lcd.cls(); |
Eduard | 0:727af1f2cbe4 | 20 | BlinkLed = !BlinkLed; |
Eduard | 0:727af1f2cbe4 | 21 | } |
Eduard | 0:727af1f2cbe4 | 22 | |
Eduard | 0:727af1f2cbe4 | 23 | int main() |
Eduard | 0:727af1f2cbe4 | 24 | { |
Eduard | 0:727af1f2cbe4 | 25 | lcd.cls(); |
Eduard | 0:727af1f2cbe4 | 26 | BlinkLed = 1; // show the program is running |
Eduard | 0:727af1f2cbe4 | 27 | while(1) |
Eduard | 0:727af1f2cbe4 | 28 | { |
Eduard | 0:727af1f2cbe4 | 29 | lcd.printf(" The use of\n"); |
Eduard | 0:727af1f2cbe4 | 30 | lcd.printf(" Floatingpoint\n"); |
Eduard | 0:727af1f2cbe4 | 31 | lcd.printf(" Format Specifiers\n"); |
Eduard | 0:727af1f2cbe4 | 32 | lcd.printf(" @e.sjoukes"); |
Eduard | 0:727af1f2cbe4 | 33 | WaitRefres(3); |
Eduard | 0:727af1f2cbe4 | 34 | |
Eduard | 0:727af1f2cbe4 | 35 | float f = 0; |
Eduard | 0:727af1f2cbe4 | 36 | lcd.printf("Default notations 1\n"); |
Eduard | 0:727af1f2cbe4 | 37 | lcd.printf("for f= 0 \n"); |
Eduard | 0:727af1f2cbe4 | 38 | lcd.printf(" %%f= %f\n",f); //float notation takes 6 nummers after the point in this case 6 zero's. |
Eduard | 0:727af1f2cbe4 | 39 | WaitRefres(5); |
Eduard | 0:727af1f2cbe4 | 40 | |
Eduard | 0:727af1f2cbe4 | 41 | f = 12.345678; |
Eduard | 0:727af1f2cbe4 | 42 | lcd.printf("Default notations 2\n"); |
Eduard | 0:727af1f2cbe4 | 43 | lcd.printf("for f= 12.345678\n"); |
Eduard | 0:727af1f2cbe4 | 44 | lcd.printf(" %%f= %f\n",f); |
Eduard | 0:727af1f2cbe4 | 45 | WaitRefres(5); |
Eduard | 0:727af1f2cbe4 | 46 | |
Eduard | 0:727af1f2cbe4 | 47 | f = 12345.678; |
Eduard | 0:727af1f2cbe4 | 48 | lcd.printf("BUT \n"); |
Eduard | 0:727af1f2cbe4 | 49 | lcd.printf("for f= 12345.678\n"); |
Eduard | 0:727af1f2cbe4 | 50 | lcd.printf(" %%f= %f\n",f); //again notation takes 6 nummers after the point but now there is a fault |
Eduard | 0:727af1f2cbe4 | 51 | lcd.printf("the fault= 0.000266"); |
Eduard | 0:727af1f2cbe4 | 52 | WaitRefres(7.5); |
Eduard | 0:727af1f2cbe4 | 53 | |
Eduard | 0:727af1f2cbe4 | 54 | f =1234.12345; |
Eduard | 0:727af1f2cbe4 | 55 | lcd.printf("for f= 1234.12345\n"); |
Eduard | 0:727af1f2cbe4 | 56 | lcd.printf(" %%9.2f= %9.2f\n", f); |
Eduard | 0:727af1f2cbe4 | 57 | lcd.printf(" %%09.2f= %09.2f\n", f); |
Eduard | 0:727af1f2cbe4 | 58 | lcd.printf("%%+09.2f= %+09.2f", f); |
Eduard | 0:727af1f2cbe4 | 59 | WaitRefres(7.5); |
Eduard | 0:727af1f2cbe4 | 60 | |
Eduard | 0:727af1f2cbe4 | 61 | |
Eduard | 0:727af1f2cbe4 | 62 | lcd.printf("A very big number\n"); |
Eduard | 0:727af1f2cbe4 | 63 | float f1 = 10000000000000000000; |
Eduard | 0:727af1f2cbe4 | 64 | float f2 = 10000000000000000000; |
Eduard | 0:727af1f2cbe4 | 65 | f1 = f1*f2; //seems that it's not possible to write 100,000,000,000,000,000,000,000,000,000,000,000,000 |
Eduard | 0:727af1f2cbe4 | 66 | lcd.printf("for f= 1e38 %%f= \n%38.0f",f1); //at once to the program. |
Eduard | 0:727af1f2cbe4 | 67 | WaitRefres(5); |
Eduard | 0:727af1f2cbe4 | 68 | |
Eduard | 0:727af1f2cbe4 | 69 | lcd.printf("A too big number\n"); |
Eduard | 0:727af1f2cbe4 | 70 | lcd.printf("%%f= %f\n",f1*10); |
Eduard | 0:727af1f2cbe4 | 71 | lcd.printf("Uppercast with %%F\n"); |
Eduard | 0:727af1f2cbe4 | 72 | lcd.printf("%%F= %F\n",f1*10); |
Eduard | 0:727af1f2cbe4 | 73 | WaitRefres(5); |
Eduard | 0:727af1f2cbe4 | 74 | |
Eduard | 0:727af1f2cbe4 | 75 | lcd.printf("A very small number\n"); |
Eduard | 0:727af1f2cbe4 | 76 | f = 0.0000000000000000000000000000000000001; |
Eduard | 0:727af1f2cbe4 | 77 | lcd.printf("for f= 1e-38 %%f= \n%39.37f\n", f); |
Eduard | 0:727af1f2cbe4 | 78 | WaitRefres(5); |
Eduard | 0:727af1f2cbe4 | 79 | } |
Eduard | 0:727af1f2cbe4 | 80 | } |