160 likes | 260 Views
Engineering Output. Engineering Notation: Similar to scientific notation. Exponent is evenly divisible by 3 1 <= mantissa < 1000 The x10 n portion is replaced by a “units prefix” Examples: 3495V -> 3.495kV 0.00008763A -> 87.63uA. Engineering Output Function.
E N D
Engineering Output • Engineering Notation: • Similar to scientific notation. • Exponent is evenly divisible by 3 • 1 <= mantissa < 1000 • The x10nportion is replaced by a “units prefix” • Examples: • 3495V -> 3.495kV • 0.00008763A -> 87.63uA
Engineering Output Function int PutV_Eng(double v, int sigfigs, char *units) v : Value to be output sigfigs : number of sig figs to display units : string containing the units name to append. RETURNS: Number of characters printed.
Significant Figures • Measure of the accuracy and/or precision of a value. • Values are assumed known to +/- 0.5 sig fig. • Determines how many digits are reported. • By convention, a leading 1 is not considered significant. • By convention, trailing zeroes not considered significant. • Examples: • 3.495kV has 4 sig figs. • 17.63uA has 3 sig figs. • 400m has 1 sig fig. (unless told otherwise).
Rounding • Round to nearest least significant digit. • Visually, we look to see if the next digit is 5 or greater. • Our output algorithm automatically truncates (ignores) any digits to the right of the last digit output. • Algorithmically, we can simply add 0.5 sig fig and truncate. • Examples: • 4.53243 rounds to 4.532 with 4 sig. figs. • 4.53243 + 0.0005 = 4.532 | 93 (only output 4 digits) • 2.38954 rounds to 2.39 with 3 sig figs. • 2.38954 + 0.005 = 2.39 | 454 (only output 3 digits)
Rounding Normalized Values In scientific notation, the value v can be expressed using a mantissa (m) and an exponent (e) as follows: v = m x 10e This value is normalized if m has exactly one non-zero digit to the left of the decimal point, i.e, if: 1.0 <= m < 10.0 Ignoring significant of leading 1 (for now), 0.5 sig fig is then: hsf = 5 x 10-N (hsf - “hemi-sig fig) If m has a leading 1, then hsf is another factor of ten smaller: hsf = 0.5 x 10-N
Top Level Decomposition 1) TASK: Output negative sign and take absolute value. 2) TASK: Determine the mantissa and the exponent. 3) TASK: Add 0.5 Sig Fig to Mantissa 4) TASK: Scale mantissa so that exponent is divisible by 3. 5) TASK: Output mantissa to N sig figs. 6) TASK: Output prefix based on exponent. 7) TASK: Output units. 8) TASK: Return number of characters printed.
Hand Example v = -0.00001846774 to 4 sig figs 1) TASK: Output negative sign and take absolute value. OUTPUT: ‘-’ v = 0.00001846774 2) TASK: Determine the mantissa and the exponent. m = 1.846774 e = -5 3) TASK: Add 0.5 Sig Fig to Mantissa hsf = 5 x 10-4 = 0.0005 hsf = hsf/10 = 0.00005 (since m < 2) m = m + hsf = 1.846774 + 0.00005 = 1.846824
Hand Example (cont’d) 4) TASK: Scale mantissa so that exponent is divisible by 3. e = -4 not divisible by 3. Multiply mantissa by 10 and decrement exponent until it is. m = 18.46824 e = -3 5) TASK: Output mantissa to N sig figs. Output m to (N+1 digits since leading digit was a 1) OUTPUT: 18.468 6) TASK: Output prefix based on exponent. OUTPUT: m 7) TASK: Output units.
PutV_Eng() Task 1 int PutV_Eng(double v, int sigfigs, char *units) { int i, c, e; double m, hsf; /* TASK 1 - Handle negative values */ if (n < 0.0) { PutC(‘-’); return 1 + PutV_Eng(-v, sigfigs, units); }
PutV_Eng() Task 2 /* TASK 2 - Determine mantissa and exponent */ m = 0.0; e = 0; if (v != 0.0) { while (v >= 10.0) { v /= 10.0; e++; } while (v < 1.0) { v *= 10.0; e--; } }
PutV_Eng() Task 3 /* TASK 3 - Add 0.5 sig fig */ hsf = (m < 2.0)? 0.5 : 5.0; for (i = 0; i < sigfigs; i++) hsf /= 10.0;
PutV_Eng() Task 4 /* TASK 4 - Make exponent divisible by 3 */ while (e%3) { m *= 10.0; e--; }
PutV_Eng() Task 5 /* TASK 5 - Output Mantissa */ c = PutV_lfN(m, sigfigs + ((m<2.0)? 1 : 0) ); /* The PutV_lfN() function is basically the PutV_lf() function except that it only puts out N digits (followed by trailing zeros if necessary). */
PutV_Eng() Task 6 /* TASK 6 - Output Prefix */ switch(e) { case -24: PutC(‘y’); c++; break; case -21: PutC(‘z’); c++; break; /* ... */ case -3: PutC(‘m’); c++; break; case 0: break; case 3: PutC(‘k’); c++; break; /* ... */ case 21: PutC(‘Z’); c++; break; case 24: PutC(‘Y’); c++; break; default: PutC(‘e’); c += 1 + PutV_i(e); }
PutV_Eng() Task 7 / 8 /* TASK 7 - Output Units */ c += PutS(units); /* TASK 8 - Return number of characters */ return c; }