Math.SQRT1_2
Math.SQRT1_2 is a static property of the Math object that represents the square root of 1/2, approximately 0.7071067811865476. It is a read-only constant; you cannot change its value.
Mathematically, this equals 1 / Math.sqrt(2) or Math.sqrt(2) / 2. It appears frequently in trigonometry (cos 45° = sin 45°), normalization calculations, and probability distributions.
Syntax
Math.SQRT1_2
Value
Math.SQRT1_2; // 0.7071067811865476
Examples
Basic Usage
console.log(Math.SQRT1_2);
// 0.7071067811865476
Verifying the Value
console.log(Math.SQRT1_2 === 1 / Math.sqrt(2));
// true
console.log(Math.SQRT1_2 === Math.sqrt(2) / 2);
// true
Trigonometric Calculations
The value 1/√2 appears in angle calculations:
// cos(45°) = sin(45°) = 1/√2 = √2/2
console.log(Math.cos(Math.PI / 4));
// 0.7071067811865476
console.log(Math.sin(Math.PI / 4));
// 0.7071067811865476
Normalizing a 2D Vector
When normalizing diagonal vectors, you divide by √2:
function normalize2D(x, y) {
const magnitude = Math.sqrt(x * x + y * y);
return {
x: x / magnitude,
y: y / magnitude
};
}
// Unit vector along (1, 1) diagonal
const normalized = normalize2D(1, 1);
console.log(normalized.x);
// 0.7071067811865476
console.log(normalized.x === Math.SQRT1_2);
// true
Calculating Diagonal Length
For a unit square, the diagonal length is √2, but each component is 1/√2:
function diagonalComponents(side) {
return {
x: side * Math.SQRT1_2,
y: side * Math.SQRT1_2
};
}
console.log(diagonalComponents(10));
// { x: 7.071067811865476, y: 7.071067811865476 }
Probability: Normal Distribution
The standard normal distribution uses √2 in its probability density function:
function normalPDF(x) {
return Math.exp(-0.5 * x * x) / Math.sqrt(2 * Math.PI);
}
console.log(normalPDF(0));
// 0.3989422804014327
Photography: F-Stop Calculations
Aperture ratios relate to √2:
// Each f-stop doubles light, related to √2
function apertureArea(fNumber) {
return Math.PI / (fNumber * fNumber);
}
console.log(apertureArea(1.414));
// Approximately 1/2 times the area at f/1
Audio: RMS Calculations
Root mean square calculations use √2 for sine waves:
function sineWaveRMS(amplitude) {
return amplitude / Math.sqrt(2);
}
console.log(sineWaveRMS(1));
// 0.7071067811865476
console.log(sineWaveRMS(1) === Math.SQRT1_2);
// true
Common Patterns
Avoiding Repeated Calculations
// Instead of computing sqrt(1/2) each time
function calculateHalfRoot(value) {
return value * Math.SQRT1_2;
}
Scale Factor for Unit Circle
// Getting the x or y coordinate at 45 degrees
function unitCircle45() {
return Math.SQRT1_2;
}
Why Use Math.SQRT1_2?
Using Math.SQRT1_2 instead of hardcoding 0.7071067811865476 provides several advantages:
- Precision — The constant is defined to full floating-point precision
- Readability — Code clearly expresses the mathematical concept
- Standardization — Matches mathematical notation in formulas
- Performance — The value is pre-computed by the JavaScript engine
- Intent — Using
Math.SQRT1_2signals that 1/√2 is intentionally used
See Also
- Math.sqrt() — returns the square root of a number
- Math.SQRT2 — the square root of 2
- Math.PI — the ratio of circle circumference to diameter