Main Content

Customize Output Value of Real Divide HDL Optimized Block When Denominator Is Zero

This example shows how to customize the output value of the Real Divide HDL Optimized block when the denominator is zero.

Default Behavior for Fixed-Point Division by Zero

The default behavior for division by zero with fixed-point inputs is saturation:

  • If the numerator is greater than or equal to zero, the output at the y port is equal to upperbound(OutputType).

  • If the numerator is smaller than zero, the output at the y port is equal to lowerbound(OutputType).

This behavior is consistent with the divide function. A warning is displayed when division by zero occurs.

posDivByZero = divide(numerictype(1,18,10),fi(1,1,18,10),fi(0,1,18,10))
Warning: Division by zero occurred. Quotient was saturated.
posDivByZero = 
  127.9990

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 18
        FractionLength: 10
zeroDivByZero = divide(numerictype(1,18,10),fi(0,1,18,10),fi(0,1,18,10))
Warning: Division by zero occurred. Quotient was saturated.
zeroDivByZero = 
  127.9990

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 18
        FractionLength: 10
negDivByZero = divide(numerictype(1,18,10),fi(-1,1,18,10),fi(0,1,18,10))
Warning: Division by zero occurred. Quotient was saturated.
negDivByZero = 
  -128

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 18
        FractionLength: 10

Customize Output Value for Division by Zero

In applications where a different behavior is preferred for division by zero, you can use the Show divide by zero parameter to enable the divideByZero port, combined with a Switch block, to override the default output values. This example shows how to configure the Real Divide HDL Optimized block to output 0 when division by zero occurs.

Open the realDivideDBZExample model.

mdl = 'realDivideDBZExample';
open_system(mdl);

The model contains a Real Divide HDL Optimized block connected to a data source, which takes in arrays of inputs (numerators and denominators) and passes an input value from each array to the Real Divide HDL Optimized block on consecutive cycles. The output computed for each value is stored in a workspace variable. The simulation terminates when all inputs have been processed.

The divideByZero output port carries a Boolean flag that indicates if the corresponding denominator is zero. In this model, the divideByZero output port is connected to a Switch block. When the value at the divideByZero port is true, the model overrides the output value by the constant 0. When the value at the divideByZero port is false, the output value is the output of Real Divide HDL Reciprocal block.

Define Input Data

Define the custom value of the output for divide by zero to be 0.

dbzOutput = 0;

Define arrays of inputs, realDivideNumerators and realDivideDenominators.

rng('default');
inOutDataType = numerictype(1,18,10);
realDivideNumerators = fi(randn(10,1)-0.5,inOutDataType);
realDivideDenominators = fi(randn(10,1)-0.5,inOutDataType);

Specify every other denominators to be 0.

realDivideDenominators(1:2:numel(realDivideDenominators)) = 0;
dbzIndices = find(realDivideDenominators==0);

Define the output data type to be used in the model.

OutputType = inOutDataType;

Simulate Model and Verify Output Values

Simulate the model.

sim(mdl);

To examine the error of the calculation, compare the output of the Real Divide HDL Optimized block to the output of the divide function.

Verify Output Values with Default Divide by Zero Output Value

Compare the output of the Real Divide HDL Optimized block to the output of the divide function when the default divide by zero behavior is used.

expectedOutputDefault = divide(OutputType,realDivideNumerators,realDivideDenominators);
Warning: Division by zero occurred. Quotient was saturated.

Compute the maximum error between the expected values and the actual values.

maxErrorDefaultDbz = max(abs(expectedOutputDefault - realDivideOutputsDefaultDbz))
maxErrorDefaultDbz = 
   9.7656e-04

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 19
        FractionLength: 10

Show the default divide by zero output values. At division by zero, output values are saturated to the upper or lower bound of the output data type.

realDivideOutputsDefaultDbz(dbzIndices)
ans = 
  127.9990
 -128.0000
 -128.0000
 -128.0000
  127.9990

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 18
        FractionLength: 10

Verify Output Values with Custom Divide by Zero Output Value

Compare the output of the Real Divide HDL Optimized block to the output of the divide function when a custom divide by zero behavior is used.

expectedOutputCustomizedDbz = expectedOutputDefault;

Replace the default division by zero output values with custom values. Compute the maximum error between the expected values and the actual values.

expectedOutputCustomizedDbz(dbzIndices) = dbzOutput;
maxErrorCustomizedDbz = max(abs(expectedOutputCustomizedDbz - realDivideOutputsCustomizedDBZ))
maxErrorCustomizedDbz = 
   9.7656e-04

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 19
        FractionLength: 10

Show the customized divide by zero output values. At division by zero, the output values are set to the user-specified value of 0.

realDivideOutputsCustomizedDBZ(dbzIndices)
ans = 
     0
     0
     0
     0
     0

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 18
        FractionLength: 10

See Also

Functions

Blocks

Related Topics