÷

Divide

Math
Run the live example Free · no sign-up · runs in your browser

The Divide block performs algebraic division of two incoming signals: the numerator signal connected to the upper port (num) and the denominator signal connected to the lower port (den).

To guarantee numerical stability and prevent simulation crashes from mathematical singularities (such as floating-point ±∞\pm\infty or NaN values), the block features built-in division-by-zero protection implemented directly in Rust WebAssembly. When the denominator magnitude falls below machine precision, the block smoothly outputs a configurable fallback value and optionally alerts the developer.

Mathematical Model

The algebraic division with singular protection is evaluated as:

y(t)={num(t)den(t),∣den(t)∣>εvzero,∣den(t)∣≤εy(t) = \begin{cases} \dfrac{\text{num}(t)}{\text{den}(t)}, & |\text{den}(t)| > \varepsilon \\[10pt] v_{\text{zero}}, & |\text{den}(t)| \le \varepsilon \end{cases}

where:

  • num(t)\text{num}(t) is the numerator signal entering the upper port.
  • den(t)\text{den}(t) is the denominator signal entering the lower port.
  • ε=2.2204×10−16\varepsilon = 2.2204 \times 10^{-16} is the double-precision floating-point machine epsilon (f64::EPSILON).
  • vzerov_{\text{zero}} is the user-configured fallback value (divByZeroValue).

Inputs & Outputs

Direction ID Label Type Status
→ In num Num number Required
→ In den Den number Required
← Out out Result number Output

Parameters

Parameters Label Type Default Description
divByZeroValue Zero-Div Value number 0 Value to return when denominator is zero
reportWarning Report Warning boolean true Send alert to console on division by zero

Usage Examples

Example 1: Signal Normalization and Unit Scaling

Normalizing a fluctuating process sensor measurement u(t)∈[0,umax⁡]u(t) \in [0, u_{\max}] to a unit modulation index [0,1][0, 1]. Connecting the measurement signal to num and the setpoint span umax⁡u_{\max} to den yields a scaled modulation signal for downstream control logic.

Example 2: Dynamic Resistance Calculation (Ohm's Law)

Computing dynamic electrical resistance R(t)=V(t)I(t)R(t) = \frac{V(t)}{I(t)} from measured voltage and current signals. Setting divByZeroValue = 1e6 ensures that when current stops flowing (I=0I = 0), the calculated impedance gracefully represents an open circuit without corrupting ODE solver integration.

Remarks & Best Practices

  • Machine Precision Threshold: The zero-check evaluates ∣den(t)∣≤2.22×10−16|\text{den}(t)| \le 2.22 \times 10^{-16}, preventing floating-point overflow and IEEE 754 infinity propagation.
  • Console Debouncing: Warning messages are debounced to prevent console spamming when a signal lingers at zero over many simulation timesteps.
  • WASM Performance: The protection logic is implemented natively in Rust for maximum execution speed with zero runtime overhead during standard non-zero division.
  • Port Identification: The upper input port is dedicated to the numerator (num), while the lower input port accepts the denominator (den).

Related Components

  • Product: Performs algebraic multiplication of multiple input signals.
  • Gain: Scales a signal by a constant multiplication factor.
  • MathFunction: Evaluates elementary mathematical functions (ex,ln⁡x,x2e^x, \ln x, x^2).
  • NumericDisplay: Real-time numerical readout of instantaneous scalar values.