Best way to prevent/handle divide by 0 in javascript

后端 未结 8 2008
天命终不由人
天命终不由人 2020-12-01 09:12

What is the best way to prevent divide by 0 in javascript that is accepting user inputs. If there is no particular way to achieve this what would be the best way to handle s

相关标签:
8条回答
  • 2020-12-01 09:48

    Why not just check if the denominator is zero?

    if(x != 0) z = y / x;
    

    You can also check if the result is Infinity:

    3 / 0 == Infinity
    

    Results in true;

    (Only tested in chrome.)

    0 讨论(0)
  • 2020-12-01 09:49

    There is no way to do that with the normal / and /= operators.

    The best way to do what you want is with guards:

    function notZero(n) {
      n = +n;  // Coerce to number.
      if (!n) {  // Matches +0, -0, NaN
        throw new Error('Invalid dividend ' + n);
      }
      return n;
    }
    

    and then do division like

    numerator / notZero(denominator)
    

    Alternatively you can always guard the output

    function dividend(numerator, denominator) {
      var quotient = numerator / denominator;
      if (quotient !== quotient) { throw new Error(numerator + " / " + denominator); }
      return quotient;
    }
    

    but that loses the readability and expressiveness of /=.

    0 讨论(0)
  • 2020-12-01 09:54

    Hope this is useful

    (denominator != 0 ? numerator/denominator : Infinity)
    

    or whatever value you want to put at the end.

    Greetings.

    0 讨论(0)
  • 2020-12-01 10:03

    A bit different than stopping execution, but the ternary operator is a pretty slick way to customize variable assignment.

    var one = 1,
        zero = 0,
        customValue = 1;
    
    
    var quotient = zero===0 ? customValue : one / zero;
    

    This way, by setting the customVariable to the integer of your choice, you can expect a predictable result when division by zero occurs.

    0 讨论(0)
  • 2020-12-01 10:04

    Off the top of my head you could:

    1. Check the user input to see if the denominator is zero (or evaluates to zero, depending on what your script actually does).
    2. Check if the result of the action isFinite() and if not then handle appropriately.
    0 讨论(0)
  • 2020-12-01 10:04

    The best way is contextual. But here's the easiest:

    function myFunction( input ){
        input = 0 ? 0.0001 : input; // same as if( input == 0 ){ input = 0.0001; }
        return 1 / input;
    }
    

    Basically if the input is zero, turn it into a very small number before using as a denominator. Works great for integers, since after your division you can round them back down.

    A couple caveats prevent this from being universal:

    • It could cause false positives if your input accepts really small numbers
    • It won't trigger any error-handling code, if you need to do something special if zero is entered

    So it's best for general-purpose, non-critical cases. For example, if you need to return the result of a complex calculation and don't care if the answer is accurate to N digits (determined by 0.0001 vs. 0.00000001, etc.); you just don't want it to break on a divide-by-zero.

    As another answer suggested, you could also create a reusable global function.

    function divisor( n ){ return ( n = 0 ? 0.0001 : n ); }
    function myFunction( input ){ return 1 / divisor( input ); }
    

    Possible improvements:

    function divisor( n, orError ){
        if( typeof n == 'undefined' || isNaN( n ) || !n ){
            if( orError ){ throw new Error( 'Divide by zero.' ); }
            return 0.000000000000001;
        }else{ return 0 + n; }
    }
    

    This would take any value (null, number, string, object) and if invalid or zero, return the failsafe zero-like value. It would also coerce the output to a number just in case it was a string and you were doing something odd. All this would ensure that your divisor function always worked. Finally, for cases where you wanted to handle such errors yourself, you could set the second parameter to true and use a try/catch.

    0 讨论(0)
提交回复
热议问题