How to resolve the algorithm Detect division by zero step by step in the CLU programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Detect division by zero step by step in the CLU programming language

Table of Contents

Problem Statement

Write a function to detect a   divide by zero error   without checking if the denominator is zero.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Detect division by zero step by step in the CLU programming language

Source code in the clu programming language

% This will catch a divide-by-zero exception and 
% return a oneof instead, with either the result or div_by_zero.
% Overflow and underflow are resignaled.
check_div = proc [T: type] (a, b: T) returns (otype)
            signals (overflow, underflow)
            where T has div: proctype (T,T) returns (T) 
                             signals (zero_divide, overflow, underflow)
    otype = oneof[div_by_zero: null, result: T]
    
    return(otype$make_result(a/b))
    except when zero_divide: 
        return(otype$make_div_by_zero(nil))
    end resignal overflow, underflow
end check_div

% Try it
start_up = proc ()
    pair = struct[n, d: int]
    pairs: sequence[pair] := sequence[pair]$[
        pair${n: 10, d: 2},   % OK
        pair${n: 10, d: 0},   % divide by zero
        pair${n: 20, d: 2}    % another OK one to show the program doesn't stop
    ]
    
    po: stream := stream$primary_output()
    for p: pair in sequence[pair]$elements(pairs) do
        stream$puts(po, int$unparse(p.n) || "/" || int$unparse(p.d) || " = ")
        tagcase check_div[int](p.n, p.d)
            tag div_by_zero: stream$putl(po, "divide by zero")
            tag result (r: int): stream$putl(po, int$unparse(r))
        end
    end
end start_up

  

You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the VBScript programming language
You may also check:How to resolve the algorithm Universal Turing machine step by step in the Java programming language
You may also check:How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the J programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the MAXScript programming language