Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a^b in Math node and improved output type #2282

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions backend/src/packages/chaiNNer_standard/utility/math/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,18 @@ class MathOperation(Enum):
let a = Input0;
let b = Input2;

def nonZero(x: number): number {
match x {
0 => never,
_ as x => x,
}
}

match Input1 {
MathOperation::Add => a + b,
MathOperation::Subtract => a - b,
MathOperation::Multiply => a * b,
MathOperation::Divide => a / b,
MathOperation::Divide => a / nonZero(b),
MathOperation::Power => number::pow(a, b),
MathOperation::Log => number::log(a) / number::log(b),
MathOperation::Maximum => max(a, b),
Expand All @@ -85,6 +92,8 @@ class MathOperation(Enum):
MathOperation::Percent => a * b / 100,
}
""",
).with_never_reason(
"The mathematical operation is not defined. This is most likely a divide by zero error."
)
],
)
Expand All @@ -98,7 +107,14 @@ def math_node(a: float, op: MathOperation, b: float) -> Union[int, float]:
elif op == MathOperation.DIVIDE:
return a / b
elif op == MathOperation.POWER:
return a**b
try:
result = pow(a, b)
except Exception as e:
raise ValueError(f"{a}^{b} is not defined for real numbers.") from e

if isinstance(result, (int, float)):
return result
raise ValueError(f"{a}^{b} is not defined for real numbers.")
elif op == MathOperation.LOG:
return math.log(b, a)
elif op == MathOperation.MAXIMUM:
Expand Down