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- }
0 commit comments