Tutorialspoint.dev

JavaScript | Math.imul( ) Function

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 degrade 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:

  1. When two positive numbers are passed as parameters:
    <script type="text/javascript">
       document.write(Math.imul(3, 4));
    </script>

    /div>

    Output:

    12
  2. When both the numbers(of opposite sign) are passed as parameters:
    <script type="text/javascript">
       document.write(Math.imul(0xfffffffe, 4));
    </script>

    Output:

    -8
  3. When two negative numbers are passed as parameters:
    <script type="text/javascript">
       document.write(Math.imul(-3, -4));
    </script>

    Output:

    12
  4. When one of the parameter passed is a zero:
    <script type="text/javascript">
       document.write(Math.imul(0, 4));
    </script>

    Output:

    0


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter