| 198 | /// This is equivalent to raising the number to the power of `1/n`. |
| 199 | #[node_macro::node(category("Math: Arithmetic"))] |
| 200 | fn root<T: num_traits::float::Float>( |
| 201 | _: impl Ctx, |
| 202 | /// The number inside the radical for which the `n`th root is calculated. |
| 203 | #[default(2.)] |
| 204 | #[implementations(f64, f32)] |
| 205 | radicand: T, |
| 206 | /// The degree of the root to be calculated. Square root is 2, cube root is 3, and so on. |
| 207 | /// Degrees 0 or less are invalid and will produce an output of 0. |
| 208 | #[default(2.)] |
| 209 | #[implementations(f64, f32)] |
| 210 | degree: T, |
| 211 | ) -> T { |
| 212 | if degree == T::from(2.).unwrap() { |
| 213 | radicand.sqrt() |
| 214 | } else if degree == T::from(3.).unwrap() { |
| 215 | radicand.cbrt() |
| 216 | } else if degree <= T::from(0.).unwrap() { |
| 217 | T::from(0.).unwrap() |
| 218 | } else { |
| 219 | radicand.powf(T::from(1.).unwrap() / degree) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /// The logarithmic function (`log`) calculates the logarithm of a number with a specified base. If the natural logarithm function (`ln`) is desired, set the base to "e". |
| 224 | #[node_macro::node(category("Math: Arithmetic"))] |