Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the the output is to give a human reader a fast impression of the order of the result, it makes no sense return something like "113/211", so the output should limit itself to using one-digit numbers (and maybe 1/10 and 9/10). If so, you can observe that there are only 27 <em>different</em> fractions.</p> <p>Since the underlying math for generating the output will never change, a solution could be to simply hard-code a binary search tree, so that the function would perform at most log(27) ~= 4 3/4 comparisons. Here is a tested C version of the code</p> <pre><code>char *userTextForDouble(double d, char *rval) { if (d == 0.0) return "0"; // TODO: negative numbers:if (d &lt; 0.0)... if (d &gt;= 1.0) sprintf(rval, "%.0f ", floor(d)); d = d-floor(d); // now only the fractional part is left if (d == 0.0) return rval; if( d &lt; 0.47 ) { if( d &lt; 0.25 ) { if( d &lt; 0.16 ) { if( d &lt; 0.12 ) // Note: fixed from .13 { if( d &lt; 0.11 ) strcat(rval, "1/10"); // .1 else strcat(rval, "1/9"); // .1111.... } else // d &gt;= .12 { if( d &lt; 0.14 ) strcat(rval, "1/8"); // .125 else strcat(rval, "1/7"); // .1428... } } else // d &gt;= .16 { if( d &lt; 0.19 ) { strcat(rval, "1/6"); // .1666... } else // d &gt; .19 { if( d &lt; 0.22 ) strcat(rval, "1/5"); // .2 else strcat(rval, "2/9"); // .2222... } } } else // d &gt;= .25 { if( d &lt; 0.37 ) // Note: fixed from .38 { if( d &lt; 0.28 ) // Note: fixed from .29 { strcat(rval, "1/4"); // .25 } else // d &gt;=.28 { if( d &lt; 0.31 ) strcat(rval, "2/7"); // .2857... else strcat(rval, "1/3"); // .3333... } } else // d &gt;= .37 { if( d &lt; 0.42 ) // Note: fixed from .43 { if( d &lt; 0.40 ) strcat(rval, "3/8"); // .375 else strcat(rval, "2/5"); // .4 } else // d &gt;= .42 { if( d &lt; 0.44 ) strcat(rval, "3/7"); // .4285... else strcat(rval, "4/9"); // .4444... } } } } else { if( d &lt; 0.71 ) { if( d &lt; 0.60 ) { if( d &lt; 0.55 ) // Note: fixed from .56 { strcat(rval, "1/2"); // .5 } else // d &gt;= .55 { if( d &lt; 0.57 ) strcat(rval, "5/9"); // .5555... else strcat(rval, "4/7"); // .5714 } } else // d &gt;= .6 { if( d &lt; 0.62 ) // Note: Fixed from .63 { strcat(rval, "3/5"); // .6 } else // d &gt;= .62 { if( d &lt; 0.66 ) strcat(rval, "5/8"); // .625 else strcat(rval, "2/3"); // .6666... } } } else { if( d &lt; 0.80 ) { if( d &lt; 0.74 ) { strcat(rval, "5/7"); // .7142... } else // d &gt;= .74 { if(d &lt; 0.77 ) // Note: fixed from .78 strcat(rval, "3/4"); // .75 else strcat(rval, "7/9"); // .7777... } } else // d &gt;= .8 { if( d &lt; 0.85 ) // Note: fixed from .86 { if( d &lt; 0.83 ) strcat(rval, "4/5"); // .8 else strcat(rval, "5/6"); // .8333... } else // d &gt;= .85 { if( d &lt; 0.87 ) // Note: fixed from .88 { strcat(rval, "6/7"); // .8571 } else // d &gt;= .87 { if( d &lt; 0.88 ) // Note: fixed from .89 { strcat(rval, "7/8"); // .875 } else // d &gt;= .88 { if( d &lt; 0.90 ) strcat(rval, "8/9"); // .8888... else strcat(rval, "9/10"); // .9 } } } } } } return rval; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload