{"id":"decimal-to-fraction","version":"1.0.0","description":"Convert a decimal or repeating decimal like 0.41(6) to a reduced fraction, or the closest fraction under a cap.","supported_operations":["convert decimal to fraction","repeating decimal to fraction","0.333 as a reduced fraction","closest fraction with denominator at most 100","best rational approximation of pi","simplify a decimal into lowest terms","approximate a decimal with a small denominator","exact fraction for 0.(142857)"],"unsupported_operations":["converting fractions into decimal expansions","irrational numbers, exponent notation or symbolic constants","percent or mixed number formatting","continued fraction expansion listing"],"semantics":["value is either a plain decimal ^-?(0|[1-9][0-9]*)(\\.[0-9]+)?$ or a repeating decimal ^-?(0|[1-9][0-9]*)\\.([0-9]*)\\(([0-9]+)\\)$: an integer part, a point, an optional non-repeating fraction part (may be empty), and exactly one non-empty parenthesised repeating block that runs to the end of the string. \"0.(142857)\", \"0.1(6)\", \"0.41(6)\" and \"12.(3)\" are valid; \"0.(3)(4)\", \"0.3(\", \"0.()\", \"(3)\", \".5\", \"1.\", \"1e3\", \"0.(3)x\" and \"0,5\" are invalid_input.","Length limit: a value longer than 64 UTF-16 code units -> limit_exceeded, checked before the grammar. A value of up to 64 characters that violates the grammar or the digit limits -> invalid_input. The digit limits are: at most 60 digits in total (integer part, non-repeating part and block together) and at most 40 fraction digits (non-repeating part plus repeating block). A non-string value is invalid_input.","The repeating block may start with zeros and may be \"0\" or \"9\": \"0.(0)\" is 0, \"1.(0)\" is 1, \"0.(9)\" is exactly 1 (numerator \"1\", denominator \"1\", exact true), \"0.(003)\" is 1/333, \"0.0(3)\" is 1/30. A negative sign applies to the whole value: \"-0.1(6)\" is -1/6.","Exact conversion of a repeating decimal: value = integer + non_repeating / 10^a + block / (10^a * (10^b - 1)), with a and b the lengths of the two parts, then reduced to lowest terms. Verify: 0.41(6) = 41/100 + 6/900 = 5/12.","Without max_denominator the result is the exact fraction in lowest terms and exact is true. The denominator is always positive; the sign is carried by the numerator. Zero is \"0\" over \"1\" with no sign (\"-0\", \"-0.00\" and \"0.(0)\" all give 0/1).","max_denominator is a decimal integer string matching ^[1-9][0-9]*$, value 1 to 10^18 (1000000000000000000). Anything else (\"0\", \"01\", \"-3\", \"7.0\", \"1e3\", a JSON number, 10^18+1, empty) is invalid_input. If the exact reduced denominator is <= N the exact fraction is returned; otherwise the result is the fraction closest to the value among all fractions with denominator <= N.","Closest fraction: minimise the absolute error |value - p/q| over all q <= N. On an exact tie between two candidates choose the one with the smaller denominator, and if the denominators are equal choose the one closer to zero (smaller magnitude). Examples: 0.41(6) = 5/12 with N=3 ties between 1/3 and 1/2 (both 1/12 away) and gives 1/2 (denominator 2 < 3, error \"-1/12\"); 0.5 with N=1 ties between 0/1 and 1/1 and gives 0/1; -1.5 with N=1 gives -1/1; 1.5 with N=1 gives 1/1. The result is in lowest terms; a zero numerator has denominator 1. Worked references (edge vectors, computed): Pi to 40 places with N=7 gives 22/7, N=100 gives 311/99, N=106 gives 333/106, N=113 gives 355/113 and N=33102 gives 103993/33102 (OEIS A002485/A002486 convergents; N=100 is the best of the semiconvergents, not a convergent).","error is the exact signed difference value - returned fraction, reduced, written \"n/d\" with the sign on n (\"-1/3000\" when 0.333 is approximated by 1/3; \"22/700\" would be written \"11/350\"), or \"0\" when exact. It is never a rounded decimal.","Validation order: input not an object; unknown field; missing value; field types; value length limit_exceeded; value grammar and digit limits; max_denominator range. There are no not_computable cases."],"limits":{"max_value_chars":64,"max_total_digits":60,"max_fraction_digits":40,"max_denominator_cap":"10^18"},"pricing":{"status":"unpriced","charge_usd":null},"input_schema":{"type":"object","additionalProperties":false,"required":["value"],"properties":{"value":{"type":"string","minLength":1,"maxLength":64,"pattern":"^-?(0|[1-9][0-9]*)(\\.[0-9]*\\([0-9]+\\)|\\.[0-9]+)?$","description":"A decimal string, or a repeating decimal such as \"0.41(6)\" or \"0.(142857)\", with an optional leading minus sign."},"max_denominator":{"type":"string","minLength":1,"maxLength":19,"pattern":"^[1-9][0-9]*$","description":"Optional cap N as a decimal integer string, 1 to 1000000000000000000 (10^18). When given, the closest fraction with denominator <= N is returned."}}},"output_schema":{"type":"object","additionalProperties":false,"required":["numerator","denominator","exact","error"],"properties":{"numerator":{"type":"string","minLength":1,"maxLength":110,"description":"Numerator as an integer string; carries the sign; \"0\" for zero."},"denominator":{"type":"string","minLength":1,"maxLength":41,"description":"Positive denominator as an integer string, in lowest terms."},"exact":{"type":"boolean","description":"true when the returned fraction equals the input exactly."},"error":{"type":"string","minLength":1,"maxLength":260,"description":"Exact signed value input minus fraction as a reduced \"n/d\" string (sign on n), or \"0\" when exact."}}},"examples":[{"input":{"value":"0.41(6)"},"output":{"numerator":"5","denominator":"12","exact":true,"error":"0"}},{"input":{"value":"3.14159265358979","max_denominator":"100"},"output":{"numerator":"311","denominator":"99","exact":false,"error":"1767270538921/9900000000000000"}},{"input":{"value":"-0.(142857)"},"output":{"numerator":"-1","denominator":"7","exact":true,"error":"0"}}],"execute_url":"/v1/tools/decimal-to-fraction/versions/1.0.0/execute"}