This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pdclib:floatingpoint [2019/10/09 10:36] solar [Fixed Point] |
pdclib:floatingpoint [2019/10/21 10:13] (current) solar [Floating-Point Printout] |
||
---|---|---|---|
Line 157: | Line 157: | ||
Steele & White approach the presentation of their Dragon algorithm as a series of " | Steele & White approach the presentation of their Dragon algorithm as a series of " | ||
- | ==== Fixed Point ==== | + | ==== Fixed Point Fraction Output |
Given: | Given: | ||
Line 165: | Line 165: | ||
Output: | Output: | ||
- | * A value //F// of //N// digits to base //B// (usually 10). | + | * A value //F// of //N// digits to (output) |
Such that: | Such that: | ||
- < | - < | ||
+ | * The difference between representations is less than half the positional value of the //n//th digit of //f//. | ||
- < | - < | ||
- | - < | + | - < |
+ | * The difference between representations is no more than half the positional value of the //N//th digit of //F//. | ||
- Each digit of //F// is output before the next is generated; no "back up" for corrections. | - Each digit of //F// is output before the next is generated; no "back up" for corrections. | ||
+ | |||
+ | Algorithm < | ||
+ | |||
+ | * <m>k = 0, R = f, M = { b ^ { -n } / 2 }</m> | ||
+ | * while ( 1 ) | ||
+ | * k++ | ||
+ | * U = floor( R * B ) | ||
+ | * R = ( R * B ) % 1 | ||
+ | * M *= B | ||
+ | * if ( <m>R >= M AND <= 1 - M</m> ) | ||
+ | * append( F, U ) | ||
+ | * else | ||
+ | * break | ||
+ | * if ( <m>R <= 1/ | ||
+ | * append( F, U ) | ||
+ | * if ( <m>R >= 1/ | ||
+ | * append( F, U + 1 ) | ||
+ | |||
+ | At the end, < | ||
+ | |||
+ | Example: | ||
+ | |||
+ | * Given the base <m>b = 2</m> number <m>f = .10110</ | ||
+ | * The //exact// decimal representation would be < | ||
+ | * The next higher number (< | ||
+ | * The next lower number (< | ||
+ | * The Mask would be <m>M = { b ^ {-n} } / 2 = { 2 ^ { -5 } } / 2 = 0.015625</ | ||
+ | |||
+ | * First (<m>k = 1</ | ||
+ | * multiply <m>R = 0.6835</ | ||
+ | * multiply <m>M = 0.015625</ | ||
+ | * Fractional part < | ||
+ | * Second (<m>k = 2</ | ||
+ | * multiply <m>R = 0.835</ | ||
+ | * multiply <m>M = 0.15625</ | ||
+ | * Fractional part < | ||
+ | * Post-loop | ||
+ | * Fractional part < | ||
+ | * We have <m>N = k = 2</m> fractional digits in our result of < | ||
+ | |||
+ | ==== Floating-Point Printout ==== | ||
+ | |||
+ | Given: | ||
+ | |||
+ | * A base //b// floating-point number <m>v = f * b ^ (e - p)</ | ||
+ | |||
+ | Output: | ||
+ | |||
+ | * An approximate representation to base //B//, using exponential notation if the value is very large or very small. | ||
+ | |||
+ | Algorithm (Dragon2): | ||
+ | |||
+ | * Compute <m>v prime = v * B ^ {-x}</ | ||
+ | * Print the integer part of <m>v prime</ | ||
+ | * Print a decimal point | ||
+ | * Take the fractional part of <m>v prime</ | ||
+ | * Apply algorithm < | ||
+ | * If < |