cubecl_std/trigonometry.rs
1//! Trigonometric functions and utilities for CubeCL.
2//!
3//! This module provides basic trigonometric operations and angle conversion utilities
4//! that can be used in all GPU kernels.
5
6use core::f32;
7use cubecl::prelude::*;
8use cubecl_core as cubecl;
9
10/// Converts an angle from radians to degrees.
11///
12/// # Example
13///
14/// ```rust,ignore
15/// let radians = F::new(std::f32::consts::PI);
16/// let degrees = to_degrees(radians);
17/// assert!((degrees - F::new(180.0)).abs() < F::new(1e-6));
18/// ```
19#[cube]
20pub fn to_degrees<F: Float>(val: F) -> F {
21 val * F::new(180.0 / f32::consts::PI)
22}
23
24/// Converts an angle from degrees to radians.
25///
26/// # Example
27///
28/// ```rust,ignore
29/// let degrees = F::new(180.0);
30/// let radians = to_radians(degrees);
31/// assert!((radians - F::new(std::f32::consts::PI)).abs() < F::new(1e-6));
32/// ```
33#[cube]
34pub fn to_radians<F: Float>(val: F) -> F {
35 val * F::new(f32::consts::PI / 180.0)
36}
37
38/// Computes the hypotenuse of a right triangle given the lengths of the other two sides.
39///
40/// This function computes `sqrt(x² + y²)` in a numerically stable way that avoids
41/// overflow and underflow issues.
42///
43/// # Arguments
44///
45/// * `x` - Length of one side
46/// * `y` - Length of the other side
47///
48/// # Returns
49///
50/// The length of the hypotenuse
51///
52/// # Example
53///
54/// ```rust,ignore
55/// let hyp = hypot(F::new(3.0), F::new(4.0));
56/// assert!((hyp - F::new(5.0)).abs() < F::new(1e-6));
57/// ```
58#[cube]
59pub fn hypot<F: Float>(x: F, y: F) -> F {
60 F::sqrt(x * x + y * y)
61}