# How do I make variables m and b fractions?

//(y=m*x+b)

let x1 = 5

let y1 = -1

let x2 = -5

let y2 = 11

let m = (y2-y1)/(x2-x1)

let B = m*x1

let b = y1 - B

console.log(‘y=’ )

console.log(m)

console.log(‘x+’)

console.log(b)

In code land, with a considerable amount of effort. To have rationals we need an integer name space. We are writing quotients that have integral components; i. e., numerator and denominator. Our namespace can never allow irrationals else we would never be confined to repeating decimals in the Real Number domain. All repeating decimals are reducible to a rational, as in, fraction.

Can we do this, in Python? Yes, but with help from a library that does the heavy lifting. Do I know what this library is called? Not as yet, but I’m sure it exists. Do, please, report back when you’ve located it. We can explore further…

I don’t understand the `(-m) + '/' + (Math.abs(m))` part to represent the slope.

``````console.log(`y = \${m}x + \${b}`);
``````

or

``````console.log("y = " + m + "x + " + b);
``````
fractions

Dealing with fractions is messy without using objects; you have to keep track of both the numerator and the denominator.
Here’s some code that doesn’t objects or classes (except `Math` and functions).

``````// function to find the greatest common divisor of 2 integers using Euclidean Algorithm
function gcd(a, b) {  // a, b are integers
if (a == 0) { return b; }
else if (b == 0) { return a; }
else if (Math.abs(a) <= 1 || Math.abs(b) <= 1) {
return 1;
}
// setup for gcd(n, d) = gcd(d, n % d)
let n = Math.max( Math.abs(a), Math.abs(b) );
let d = Math.min( Math.abs(a), Math.abs(b) );
if (n % d == 0) {
return d;
}
return gcd(d, n % d); // recursive version
}

// distinct points (x1, y1) and (x2, y2) where x1, y1, x2, y2 are integers
let x1 = 5;
let y1 = -1;
let x2 = -5;
let y2 = 11;
let dy = y2 - y1;
let dx = x2 - x1;
if (dx == 0) {
console.log(`x = \${x1}`);
//throw new Error("division by 0");
}

// fix the negative signs
if (dx < 0) {
dy = -dy;
dx = -dx;
}
// reduce the fraction dy/dx to lowest terms
let gcf = gcd(dy, dx);
dy = dy / gcf;
dx = dx / gcf;

// m is an integer or a string (for a fraction), same for b
let m; //m = dy/dx; // slope
let b; //b = y1 - (m * x1); // y-intercept
if (dx == 1) {
m = dy;
b = y1 - (m * x1);
}
else {
m = dy + "/" + dx;
let b_num = (y1 * dx) - (dy * x1); // numerator of b
let b_den = dx; // denominator of b
// reduce the fraction
let gcf = gcd(b_num, b_den);
b_num = b_num / gcf;
b_den = b_den / gcf;
if (b_den == 1) {
b = b_num;
}
else {
b = b_num + "/" + b_den;  // b = b_num / b_den;
}
}
console.log(`y = \${m}x + \${b}`);
``````