forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearEquation.java
More file actions
27 lines (24 loc) · 768 Bytes
/
LinearEquation.java
File metadata and controls
27 lines (24 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.thealgorithms.maths;
/**
* Solves linear equations of the form ax + b = 0.
*
* @see <a href="https://en.wikipedia.org/wiki/Linear_equation">Linear equation (Wikipedia)</a>
*/
public final class LinearEquation {
private LinearEquation() {
}
/**
* Solves the equation ax + b = 0 and returns the value of x.
*
* @param a the coefficient of x, must not be zero
* @param b the constant term
* @return the value of x that satisfies the equation
* @throws IllegalArgumentException if a is zero
*/
public static double solve(final double a, final double b) {
if (a == 0) {
throw new IllegalArgumentException("Coefficient 'a' must not be zero");
}
return -b / a;
}
}