TheAlgorithms/Java · error · RuntimeException
Number must be zero for PI assignment!
Error message
Number must be zero for PI assignment!
What it means
Thrown by Builder.pi() when the builder's internal number field is not 0. The pi() method is an assignment operation that overwrites number with Math.PI; the guard ensures the caller is not accidentally discarding a previously accumulated value. Uses RuntimeException rather than IllegalArgumentException.
Source
Thrown at src/main/java/com/thealgorithms/maths/MathBuilder.java:120
number -= num;
}
return this;
}
// Generates a random number and sets to NUMBER
public Builder rand(long seed) {
if (number != 0) {
throw new RuntimeException("Number must be zero for random assignment!");
}
Random random = new Random();
number = random.nextDouble(seed);
return this;
}
// Takes PI value and sets to NUMBER
public Builder pi() {
if (number != 0) {
throw new RuntimeException("Number must be zero for PI assignment!");
}
number = Math.PI;
return this;
}
// Takes E value and sets to NUMBER
public Builder e() {
if (number != 0) {
throw new RuntimeException("Number must be zero for E assignment!");
}
number = Math.E;
return this;
}
public Builder randomInRange(double min, double max) {
if (number != 0) {
throw new RuntimeException("Number must be zero for random assignment!");
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Call pi() on a fresh Builder (number == 0)
- If you want to use PI in a calculation, use multiply(Math.PI) or add(Math.PI) instead
- Build the current state into a MathBuilder first, then start a new Builder for PI
Example fix
// before MathBuilder.Builder b = new MathBuilder.Builder(5).pi(); // after MathBuilder.Builder b = new MathBuilder.Builder().pi(); // or if you need PI in a calculation: MathBuilder.Builder b2 = new MathBuilder.Builder(5).multiply(Math.PI);
Defensive patterns
Strategy: validation
Validate before calling
// Call pi() only on a fresh builder MathBuilder.Builder b = new MathBuilder.Builder(); // number == 0 b.pi();
Try / catch
try {
builder.pi();
} catch (RuntimeException e) {
builder = new MathBuilder.Builder();
builder.pi();
} Prevention
- pi() overwrites number with Math.PI — it does not multiply or add
- Use multiply(Math.PI) if you need PI in an arithmetic expression with an existing value
- Create a new Builder for each independent assignment operation
When it happens
Trigger: Calling pi() after any operation that set number to non-zero: new MathBuilder.Builder(5).pi(), or after add(), multiply(), or any other mutating operation on a non-zero builder.
Common situations: Fluent chaining where the developer expects pi() to add or multiply by PI, but the API treats it as a raw assignment. For example, new Builder().add(5).pi() throws because add(5) set number to 5.
Related errors
- Number must be zero for random assignment!
- Number must be zero for E assignment!
- Number must be zero to set!
- Cannot convert NaN to long!
- Theta (angle) must be a finite number.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/a8334a40bc35810f.
Report an issue: GitHub.