JavaScript convert to Rational Number Z19702 Type converter from code
Appearance
changed to 2-key-representation for JS |
deal with the case of a possible negative denominator |
||
Contents | |||
| converter / code | converter / code | ||
function Z19702( Z19702K1 ) { let signzid = 'Z16660'; signzid = 'Z16662'; } else if ( signzid = 'Z16661'; } const gcd = (a, b) => (b ? gcd(b, a % b) : a); const const divisor = gcd( const numerator = const denominator = return { "Z1K1": { "Z1K1": "Z9", "Z9K1": "Z19677" }, "Z19677K1": { "Z1K1": { "Z1K1": "Z9", "Z9K1": "Z16659" }, "Z16659K1": { "Z1K1": "Z9", "Z9K1": signzid } }, "Z19677K2": { "Z1K1": { "Z1K1": "Z9", "Z9K1": "Z13518" }, "Z13518K1": { "Z1K1": "Z6", "Z6K1": numerator.toString() } }, "Z19677K3": { "Z1K1": { "Z1K1": "Z9", "Z9K1": "Z13518" }, "Z13518K1": { "Z1K1": "Z6", "Z6K1": denominator.toString() } } }; } | function Z19702( Z19702K1 ) { let signzid = 'Z16660'; const product = Z19702K1.K1 * Z19702K1.K2 if ( product < 0n ) { signzid = 'Z16662'; } else if ( product == 0n ) { signzid = 'Z16661'; } const gcd = (a, b) => (b ? gcd(b, a % b) : a); const absolute_numerator = (Z19702K1.K1 > 0) ? Z19702K1.K1 : ( Z19702K1.K1 * -1n ); const absolute_denominator = (Z19702K1.K2 > 0) ? Z19702K1.K2 : ( Z19702K1.K2 * -1n ); const divisor = gcd( absolute_numerator, absolute_denominator ); const numerator = absolute_numerator / divisor; const denominator = absolute_denominator / divisor; return { "Z1K1": { "Z1K1": "Z9", "Z9K1": "Z19677" }, "Z19677K1": { "Z1K1": { "Z1K1": "Z9", "Z9K1": "Z16659" }, "Z16659K1": { "Z1K1": "Z9", "Z9K1": signzid } }, "Z19677K2": { "Z1K1": { "Z1K1": "Z9", "Z9K1": "Z13518" }, "Z13518K1": { "Z1K1": "Z6", "Z6K1": numerator.toString() } }, "Z19677K3": { "Z1K1": { "Z1K1": "Z9", "Z9K1": "Z13518" }, "Z13518K1": { "Z1K1": "Z6", "Z6K1": denominator.toString() } } }; } | ||