Multiplication Operator (*)

Summary

Multiplies two numbers.

Syntax

expression1 * expression2

Parameters

expression1
An expression that evaluates to a number.
expression2
An expression that evaluates to a number.

Description

For numeric data, the * operator multiplies two numbers. For example, 5 * 3 will evaluate to the result 15.

Differences from JavaScript

  • In JS++, arithmetic cannot be performed on Booleans. In JavaScript, for these operations, false is treated as 0 and true is treated as 1.

  • In JS++, arithmetic operations cannot be performed on null. In JavaScript, null is treated as 0.

  • In JS++, undefined cannot be used in arithmetic operations. In addition, NaN can never occur from integer arithmetic. In JavaScript, an arithmetic expression that evaluates to undefined or NaN results in NaN for the result.

Overflow Behavior

Due to its JavaScript history, multiplication of int types may result in different overflow behavior from Java and C#. For example:

1
2
3
int x = 0x7fffffff, y = 0x7fffffff;
 
x * y; // 0

In Java/C#, the above overflow produces the value 1 (one). The reason this happens is because multiplication, as guaranteed by the ECMAScript 3 specification, produces an IEEE-754 double result before it is converted to int. Since precision for double is lost beyond 253, any multiplication with a result higher than 253 may have inaccurate overflow. However, the decision was made not to de-optimize compiled code for this corner case, and if your multiplication results in integer overflow, it is advised to use a larger numeric type, such as long or unsigned long.

Examples

Basic Usage
1
2
3
4
import System;
 
Console.log(2 * 2); // 4
Console.log(5 * 3); // 15

See Also

Share

HTML | BBCode | Direct Link