Hatena::Groupmnemonic

きおく屋めがねばし

 | 

2012-09-30

「3.1.3 JavaScript での算術演算」

Arithmetic in JavaScript

JavaScript の基本的な算術演算子には、加算 (+)、減算 (-)、乗算 (*)、除算 (/)、剰余 (%)があります。

JavaScript programs work with numbers using the arithmetic operators that language provides. These include + for addition, - for subtraction, * for multiplication, / for division, and % for modulo (remainder after division).

これらの基本的な算術演算子のほかに、もっと複雑な数学演算を行う関数がたくさんあります。

In addition to these basic arithmetic operators, JavaScript supports more complex mathematical operators through a set of functions and constants as properties of the Math object:

Math.pow(2,53)           // => 9007199254740992: 2 to the power 53
Math.round(.6)           // => 1.0: round to the nearest integer
Math.ceil(.6)            // => 1.0 round up to an integer
Math.floor(.6)           // => 0.0 round down to an integer
Math.abs(-5)             // => 5: absolute value
Math.max(x,y,z)          // Return the largest argument
Math.min(x,y,z)          // Return the smallest argument
Math.random()            // Pseudo-random number x where 0 <= x < 1.0
Math.PI                  // π: circumference of circle / diameter
Math.E                   // e: The base of the natural logarithm
Math.sqrt(3)             // The square root of 3
Math.pow(3, 1/3)         // The cube root of 3
Math.sin(0)              // Trigonometry: also Math.cos, Math.atan, etc.
Math.log(10)             // Natural logarithm of 10
Math.log(100)/Math.LN10  // Base 10 logarithm of 100
Math.log(512)/Math.LN2   // Base 2 logarithm of 512
Math.exp(3)              // Math.E cubed

JavaScript では、算術演算中にオーバーフローやアンダーフローが発生した場合や、0 除算を行った場合もエラーが発生しません。

Infinity                   // A read/write variable initialized to Infinity.
Number.POSITIVE_INFINITY   // same value. read-only.
1/0  			   // This is also the same value.
Number.MAX_VALUE + 1       //This also evaluates to Infinity.
Number.NEGATIVE_INFINITY   // These expression are negative infinity.
-Infinity
-1/0
-Number.MAX_VALUE - 1

NaN  			  // A read/write variable initialized to NaN.
Number.NaN  		  // A read-only property holding the same value.
0/0  		       	  // Evaluates to NaN.

Number.MIN_VALUE/2  	  // Underflow: evaluates to 0
-Number.MIN_VALUE/2       // Negative zero
-1/Infinity  	          // Also negative 0
-0

トラックバック - http://mnemonic.g.hatena.ne.jp/paragramma/20120930
 |