Detect division by zero

The numerical system of Sidef evaluates x/0 to +/-Inf.

func div_check(a, b){
    var result = a/b
    result.abs == Inf ? nil : result
}

say div_check(10, 2)  # 5
say div_check(1, 0)   # nil (detected)

Alternatively, we can do:

func div_check(a, b){
    Perl.eval("#{a} / #{b}")
}

say div_check(10, 2)  # 5
say div_check(1, 0)   # nil (detected)

Last updated