Skip to content

Commit

Permalink
Fixed a^b in Math node and improved output type (#2282)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Oct 24, 2023
1 parent 3b3666c commit 232e417
Showing 1 changed file with 18 additions and 2 deletions.
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

0 comments on commit 232e417

Please sign in to comment.