How to resolve the algorithm Detect division by zero step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

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

Source code in the v programming language

fn main() {
	divide(0, 0)	
	divide(15, 0)
	divide(15, 3)
}

fn divide(x f64, y f64) {
	result := x/y
	if result.str().contains_any_substr(["inf","nan"]) == true {
		println("Can\'t divide by zero!") 
		return
	}
	println(result)
}


fn main() {
	divide(0, 0)	
	divide(15, 0)
	divide(15, 3)
}

pub fn divide(x f64, y f64) {
	succeed := divide_error_handler(x, y) or {
		println(err)
		return
	}
	println(succeed)
}

fn divide_error_handler(x f64, y f64) !f64 {
	result := x/y
	if result.str().contains_any_substr(["inf","nan"]) == true {
		return error("Can\'t divide by zero!")
	}
	return result
}


  

You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the Clojure programming language
You may also check:How to resolve the algorithm Add a variable to a class instance at runtime step by step in the Python programming language
You may also check:How to resolve the algorithm Find if a point is within a triangle step by step in the Factor programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm N-smooth numbers step by step in the Mathematica / Wolfram Language programming language