Helo guys, I have a big project, but one function in particular to calculate a value (y) in a linear equation. Let me explain:
A linear function is a function that represents a straight line on the coordinate plane. For example, y = 3x - 2 represents a straight line on a coordinate plane and hence it represents a linear function.
I have a table with coordinates with two points like: X(minX, maxX) and Y(minY, ymaxY) that to form this coordinate plane. I need find Y for a given X.
I'm doing this:
function calculateCoordinateY(
int256 x,
int256 minX,
int256 maxX,
int256 minY,
int256 maxY
) internal pure returns (int256 _y) {
/**
* https://medium.com/not-zero/linear-equation-50a847f58665
* minY = (a * minX) + b
* maxY = (a * maxX) + b
* a = (minY - maxY) / (minX - maxX);
* b = ((minY * maxX) - (minX * maxY)) / (maxX - minX);
* y = (a * x) + b;
*/
_y =
(((minY - maxY) * x) / (minX - maxX)) +
(((minY * maxX) - (minX * maxY)) / (maxX - minX));
}
But doesn't work.. For values of small intervals it works very well, but when the values increase, not much, more than 10 already gives an error.
// Point test # 1 = works fine
(-10 * 10**18, 0.02 * 10**18)
// Point test # 2 = works fine
(-9.63 * 10**18, 0.0257454545454546 * 10**18)
// Point test # 3 = works fine
(0, 0.0621454545454546 * 10**18));
// Point test # 4 = works fine
(0.556 * 10**18, 0.0635454545454546 * 10**18)
// Point test # 5 = no match math
(14.44 * 10**18, 0.0985454545454545 * 10**18)
// Point test # 6 = no match math
(15.00 * 10**18, 0.1 * 10**18)
Does anyone know where I went wrong?
If my question is not clear, please tell me, I try to clarify better