Related Articles

Related Articles

JavaScript | Math.imul( ) Function
  • Last Updated : 13 Aug, 2019

The Math.imul() function in JavaScript is used to calculate the result of the 32-bit multiplication of the two integers passed as parameters to it. Math.imul() allows for 32-bit integer multiplication with C-like semantics. If the Math.imul() function is used with normal floating type variables in JavaScript then there will be a degrade in performance because of the conversion of floats to ints before multiplication. The overhead of conversion results in a performance degrades if the Math.imul() function is used with normal floating-point variables allowed in JavaScript.

Syntax:

Math.imul(Value1, Value2);

Parameters: This function accepts two parameters Value1 and Value2 which represents two numbers to be multiplied.

Return Value: The Math.imul() function returns the result of the C-like 32-bit multiplication of the given arguments.

Examples:



Input  : Math.imul(3, 4)
Output : 12
     
Input  : Math.imul(-3, -4)
Output : 12

Input  : Math.imul(0, 4)
Output : 0

Below programs illustrate the Math.imul() function in JavaScript:

  • Example 1: When two positive numbers are passed as parameters.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.imul(3, 4));
    </script>

    chevron_right

    
    

    Output:

    12
  • Example 2: When both the numbers(of opposite sign) are passed as parameters.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.imul(0xfffffffe, 4));
    </script>

    chevron_right

    
    

    Output:

    -8
  • Example 3: When two negative numbers are passed as parameters.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.imul(-3, -4));
    </script>

    chevron_right

    
    

    Output:

    12
  • Example 4: When one of the parameter passed is a zero.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.imul(0, 4));
    </script>

    chevron_right

    
    

    Output:

    0

Supported Browsers: The browsers supported by JavaScript Math.imul( ) function are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :