Skip to content

Commit d6968f7

Browse files
feat: Add cube root algorithm and tests
1 parent eefd14c commit d6968f7

File tree

2 files changed

+43
-57
lines changed

2 files changed

+43
-57
lines changed

math/cube_root.jule

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Import the standard math library
2-
use std::math { pow, abs }
3-
// Import the standard testing library
4-
use std::testing { T }
2+
use std::math::pow
53

64
/**
75
* Calculates the cube root of a given f64 number.
@@ -19,57 +17,3 @@ fn cbrt(x: f64): f64 {
1917
ret pow(x, 1.0 / 3.0)
2018
}
2119

22-
/**
23-
* Entry point for example usage.
24-
*/
25-
fn main() {
26-
let num1: f64 = 27.0
27-
let root1: f64 = cbrt(num1)
28-
println("Cube root of " + f64_to_str(num1) + " is " + f64_to_str(root1)) // Expected: 3.0
29-
30-
let num2: f64 = -64.0
31-
let root2: f64 = cbrt(num2)
32-
println("Cube root of " + f64_to_str(num2) + " is " + f64_to_str(root2)) // Expected: -4.0
33-
34-
let num3: f64 = 15.625
35-
let root3: f64 = cbrt(num3)
36-
println("Cube root of " + f64_to_str(num3) + " is " + f64_to_str(root3)) // Expected: 2.5
37-
38-
let num4: f64 = 0.0
39-
let root4: f64 = cbrt(num4)
40-
println("Cube root of " + f64_to_str(num4) + " is " + f64_to_str(root4)) // Expected: 0.0
41-
}
42-
43-
/**
44-
* Test function for the cbrt implementation.
45-
* Jule's test functions are prefixed with #test.
46-
*/
47-
#test
48-
fn test_cbrt(t: &T) {
49-
// Define a small tolerance for floating-point comparisons
50-
const TOLERANCE: f64 = 0.000001
51-
52-
// Test cases
53-
let test_cases = [
54-
(27.0, 3.0),
55-
(-64.0, -4.0),
56-
(125.0, 5.0),
57-
(0.0, 0.0),
58-
(15.625, 2.5),
59-
(1.0, 1.0),
60-
(-1.0, -1.0),
61-
(8.0, 2.0),
62-
]
63-
64-
for tc in test_cases {
65-
let input: f64 = tc[0]
66-
let expected: f64 = tc[1]
67-
let result: f64 = cbrt(input)
68-
69-
// Check if the result is within the tolerance of the expected value
70-
if math::abs(result - expected) > TOLERANCE {
71-
// Use t.Errorf to report a test failure
72-
t.Errorf("cbrt({}) = {}; expected {}", input, result, expected)
73-
}
74-
}
75-
}

math/cube_root_test.jule

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#build test // Tells Jule this file is for testing
2+
3+
// Import necessary libraries with correct syntax
4+
use std::testing::{T} // Correct import for testing type T
5+
use std::math::{abs} // Import abs function for tolerance check
6+
use super::{cbrt} // Import the cbrt function from cube_root.jule
7+
8+
/**
9+
* Test function for the cbrt implementation.
10+
* Jule's test functions are prefixed with #test.
11+
*/
12+
#test
13+
fn test_cbrt(t: &T) {
14+
// Define a small tolerance for floating-point comparisons
15+
const TOLERANCE: f64 = 0.000001
16+
17+
// Test cases: Define the type explicitly as an array of (f64, f64) tuples
18+
let test_cases: [(f64, f64)] = [
19+
(27.0, 3.0),
20+
(-64.0, -4.0),
21+
(125.0, 5.0),
22+
(0.0, 0.0),
23+
(15.625, 2.5),
24+
(1.0, 1.0),
25+
(-1.0, -1.0),
26+
(8.0, 2.0),
27+
]
28+
29+
// Iterate through the test cases
30+
for tc in test_cases {
31+
let input: f64 = tc[0]
32+
let expected: f64 = tc[1]
33+
let result: f64 = cbrt(input) // Call the imported function
34+
35+
// Check if the absolute difference is within the tolerance
36+
// Use the imported 'abs' function
37+
if abs(result - expected) > TOLERANCE {
38+
// Use t.Errorf to report a test failure with details
39+
t.Errorf("cbrt({}) = {}; expected {}", input, result, expected)
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)