JavaScript library that ports standard library functions from other programming languages (PHP, C, Go, Python, Ruby) to JavaScript
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Ruby standard library functions ported to JavaScript. This module contains 1 mathematical function from Ruby's Math module.
Mathematical function from Ruby's Math standard library module.
/**
* Ruby Math module function
*/
ruby.Math.acos(x) // Arc cosine (inverse cosine) functionUsage Examples:
const locutus = require('locutus');
// Calculate arc cosine
const result1 = locutus.ruby.Math.acos(1); // 0 (radians)
const result2 = locutus.ruby.Math.acos(0); // 1.5707963267948966 (π/2 radians)
const result3 = locutus.ruby.Math.acos(-1); // 3.141592653589793 (π radians)
const result4 = locutus.ruby.Math.acos(0.5); // 1.0471975511965979 (π/3 radians)
// Convert result to degrees
const degrees = locutus.ruby.Math.acos(0.5) * (180 / Math.PI); // 60 degreesReturns the arc cosine (inverse cosine) of x in radians.
/**
* Return the arc cosine of x in radians
* @param {number} x - A number between -1 and 1 (inclusive)
* @returns {number} The arc cosine of x in radians (0 to π)
*/
ruby.Math.acos(x)Parameters:
x (number): A numeric value between -1 and 1 (inclusive)Returns:
x in radians, ranging from 0 to πDomain and Range:
Special Values:
acos(1) returns 0acos(0) returns π/2 (approximately 1.5707963267948966)acos(-1) returns π (approximately 3.141592653589793)Examples:
const locutus = require('locutus');
// Standard values
locutus.ruby.Math.acos(1); // 0
locutus.ruby.Math.acos(0); // 1.5707963267948966 (π/2)
locutus.ruby.Math.acos(-1); // 3.141592653589793 (π)
// Common trigonometric values
locutus.ruby.Math.acos(0.5); // 1.0471975511965979 (π/3, or 60°)
locutus.ruby.Math.acos(Math.sqrt(2)/2); // 0.7853981633974483 (π/4, or 45°)
locutus.ruby.Math.acos(Math.sqrt(3)/2); // 0.5235987755982988 (π/6, or 30°)
// Edge cases
locutus.ruby.Math.acos(0.9999); // Very close to 0
locutus.ruby.Math.acos(-0.9999); // Very close to π
// Invalid input (outside domain) returns NaN
locutus.ruby.Math.acos(2); // NaN
locutus.ruby.Math.acos(-2); // NaNThe Ruby acos function is useful for:
const locutus = require('locutus');
// Calculate angle between two vectors
function angleBetweenVectors(v1, v2) {
// Calculate dot product
const dotProduct = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
// Calculate magnitudes
const mag1 = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
const mag2 = Math.sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
// Calculate cosine of angle
const cosTheta = dotProduct / (mag1 * mag2);
// Return angle in radians
return locutus.ruby.Math.acos(cosTheta);
}
// Convert radians to degrees
function radiansToDegrees(radians) {
return radians * (180 / Math.PI);
}
// Calculate triangle angle from sides (law of cosines)
function triangleAngle(a, b, c) {
// Calculate angle opposite to side 'a'
const cosA = (b * b + c * c - a * a) / (2 * b * c);
return locutus.ruby.Math.acos(cosA);
}
// Examples
const vector1 = { x: 1, y: 0, z: 0 };
const vector2 = { x: 0, y: 1, z: 0 };
const angle = angleBetweenVectors(vector1, vector2);
console.log(`Angle: ${radiansToDegrees(angle)}°`); // 90°
const triangleAngleA = triangleAngle(3, 4, 5); // Right triangle
console.log(`Triangle angle: ${radiansToDegrees(triangleAngleA)}°`); // ~36.87°The arc cosine function is the inverse of the cosine function:
cos(θ) = x, then acos(x) = θ// Full module access
const locutus = require('locutus');
locutus.ruby.Math.acos(0.5);
// Individual function import
const acos = require('locutus/ruby/Math/acos');
acos(0.5); // 1.0471975511965979
// Note: Ruby uses Math (capital M) unlike JavaScript's math (lowercase)
// This follows Ruby's naming convention where Math is a module