Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Math
- [Abs](math/abs.jule)
- [Cube Root](math/cube_root.jule)
- [Fact](math/fact.jule)
- [Fib](math/fib.jule)
- [Max](math/max.jule)
Expand Down
19 changes: 19 additions & 0 deletions math/cube_root.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Import the standard math library
use std::math::pow

/**
* Calculates the cube root of a given f64 number.
*
* Uses the standard library's pow function: cbrt(x) = x^(1/3).
*
* @param x The number (f64) to find the cube root of.
* @return The cube root of x as an f64.
*/
fn cbrt(x: f64): f64 {
// Handle negative numbers correctly for cube roots
if x < 0.0 {
ret -pow(-x, 1.0 / 3.0)
}
ret pow(x, 1.0 / 3.0)
}

42 changes: 42 additions & 0 deletions math/cube_root_test.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#build test // Tells Jule this file is for testing

// Import necessary libraries with correct syntax
use std::testing::{T} // Correct import for testing type T
use std::math::{abs} // Import abs function for tolerance check
use super::{cbrt} // Import the cbrt function from cube_root.jule

/**
* Test function for the cbrt implementation.
* Jule's test functions are prefixed with #test.
*/
#test
fn test_cbrt(t: &T) {
// Define a small tolerance for floating-point comparisons
const TOLERANCE: f64 = 0.000001

// Test cases: Define the type explicitly as an array of (f64, f64) tuples
let test_cases: [(f64, f64)] = [
(27.0, 3.0),
(-64.0, -4.0),
(125.0, 5.0),
(0.0, 0.0),
(15.625, 2.5),
(1.0, 1.0),
(-1.0, -1.0),
(8.0, 2.0),
]

// Iterate through the test cases
for tc in test_cases {
let input: f64 = tc[0]
let expected: f64 = tc[1]
let result: f64 = cbrt(input) // Call the imported function

// Check if the absolute difference is within the tolerance
// Use the imported 'abs' function
if abs(result - expected) > TOLERANCE {
// Use t.Errorf to report a test failure with details
t.Errorf("cbrt({}) = {}; expected {}", input, result, expected)
}
}
}
Loading