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


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.