--- title: Rational Approximation pagination_prev: constellation/ssf pagination_next: constellation/crc32 ---
The SheetJS `frac` library computes rational approximations to floating point numbers with bounded denominator. It is a core component in number formatting, powering "Fraction with up to 1 digit" and related number formats. The library is also available for standalone use on the SheetJS CDN[^1]. Source code and project documentation are hosted on the SheetJS Git server at https://git.sheetjs.com/sheetjs/frac ## Live Demo Formatted texts are calculated from the value and maximum denominators. Please [report an issue](https://git.sheetjs.com/sheetjs/frac/issues) if a particular result does not align with expectations. ```jsx live function SheetJSFrac() { const [val, setVal] = React.useState(0.699450515); const [text, setText] = React.useState(""); if(typeof frac == "undefined") return ( ERROR: Reload this page ); const fmt = arr => `${(""+arr[1]).padStart(5)} / ${(""+arr[2]).padEnd(5)}`; React.useEffect(() => { if(typeof frac == "undefined") return setText("ERROR: Reload this page!"); let v = +val; if(!isFinite(v)) return setText(`ERROR: ${val} is not a valid number!`); try { fmt(frac(val, 9)); setText(""); } catch(e) { setText("ERROR: " + (e && e.message || e)); } }, [val]); const n = { textAlign:"right" }; const g = { backgroundColor:"#C6EFCE", color:"#006100", whiteSpace:"pre-wrap" }; const b = { backgroundColor:"#FFC7CE", color:"#9C0006" }; return (Number Value | setVal(e.target.value)}/> | |||
Max Denom | Mediant | Continued Frac | ||
---|---|---|---|---|
Up to {d} Digit{d == 1 ? "" : "s"} | {10**d - 1} |
{text||fmt(frac(val,10**d-1))} |
{text||fmt(frac.cont(val,10**d-1))} |