CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-locutus

JavaScript library that ports standard library functions from other programming languages (PHP, C, Go, Python, Ruby) to JavaScript

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

ruby.mddocs/

Ruby Functions

Ruby standard library functions ported to JavaScript. This module contains 1 mathematical function from Ruby's Math module.

Capabilities

Math Functions (1 function)

Mathematical function from Ruby's Math standard library module.

/**
 * Ruby Math module function
 */
ruby.Math.acos(x)  // Arc cosine (inverse cosine) function

Usage 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 degrees

Function Details

acos(x)

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

  • (number): The arc cosine of x in radians, ranging from 0 to π

Domain and Range:

  • Domain: -1 ≤ x ≤ 1
  • Range: 0 ≤ acos(x) ≤ π

Special Values:

  • acos(1) returns 0
  • acos(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);      // NaN

Use Cases

The Ruby acos function is useful for:

  • Trigonometric Calculations: Computing angles from cosine values
  • Geometric Operations: Finding angles in triangles and other shapes
  • Physics Simulations: Calculating angles in vector operations
  • Computer Graphics: Angle calculations for rotations and transformations
  • Engineering Applications: Converting between different angle representations
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°

Mathematical Notes

The arc cosine function is the inverse of the cosine function:

  • If cos(θ) = x, then acos(x) = θ
  • The function is defined only for values between -1 and 1
  • The output is always between 0 and π radians (0° to 180°)
  • The function is decreasing: as x increases from -1 to 1, acos(x) decreases from π to 0

Import Patterns

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

docs

c.md

golang.md

index.md

php.md

python.md

ruby.md

tile.json