Jump to content

JavaScript convert to Rational Number Z19702
Type converter from code

From Wikifunctions
changed to 2-key-representation for JS
deal with the case of a possible negative denominator

Contents

converter / codeconverter / code
function Z19702( Z19702K1 ) {
let signzid = 'Z16660';
if ( Z19702K1.K1 < 0n ) {
signzid = 'Z16662';
} else if ( Z19702K1.K1 == 0n ) {
signzid = 'Z16661';
}
const gcd = (a, b) => (b ? gcd(b, a % b) : a);
const abs = (Z19702K1.K1 > 0) ? Z19702K1.K1 : ( Z19702K1.K1 * -1n );
const divisor = gcd( abs, Z19702K1.K2 );
const numerator = abs / divisor;
const denominator = Z19702K1.K2 / 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()
}
}
};
}
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()
}
}
};
}

Revision as of 08:54, 14 November 2024