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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Detect division by zero step by step in the Clojure 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 Clojure programming language

Source code in the clojure programming language

(defn safe-/ [x y]
  (try (/ x y)
    (catch ArithmeticException _
      (println "Division by zero caught!")
      (cond (> x 0)   Double/POSITIVE_INFINITY
            (zero? x) Double/NaN
            :else     Double/NEGATIVE_INFINITY) )))


  

You may also check:How to resolve the algorithm Strip block comments step by step in the Ruby programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Pinstripe/Display step by step in the J programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Factor programming language
You may also check:How to resolve the algorithm Decorate-sort-undecorate idiom step by step in the jq programming language