|
3 | 3 | ## The absolute value of a number is the number without its sign.
|
4 | 4 |
|
5 | 5 |
|
6 |
| -struct complext: |
7 |
| - var real: Int |
8 |
| - var imag: Int |
| 6 | +struct complex: |
| 7 | + var real_part: Int |
| 8 | + var imaginary_part: Int |
9 | 9 |
|
10 | 10 |
|
11 |
| - fn __init__(inout self: Self, real: Int, imag: Int): |
12 |
| - self.real = real |
13 |
| - self.imag = imag |
| 11 | + fn __init__(inout self: Self, real_part: Int, imaginary_part: Int): |
| 12 | + self.real_part = real_part |
| 13 | + self.imaginary_part = imaginary_part |
14 | 14 |
|
15 | 15 | struct Math:
|
16 | 16 | @staticmethod
|
17 |
| - fn abs(n: Int) -> Int: |
| 17 | + fn absolute(number: Int) -> Int: |
18 | 18 | ## Returns the absolute value of an integer.
|
19 |
| - ## >>> Math.abs(-5) |
| 19 | + ## >>> Math.absolute(-5) |
20 | 20 | ## 5
|
21 |
| - ## >>> Math.abs(5) |
| 21 | + ## >>> Math.absolute(5) |
22 | 22 | ## 5
|
23 |
| - return n if n >= 0 else -n |
| 23 | + return number if number >= 0 else -number |
24 | 24 |
|
25 | 25 | @staticmethod
|
26 |
| - fn abs(n: FloatLiteral) -> Int: |
| 26 | + fn absolute(number: FloatLiteral) -> FloatLiteral: |
27 | 27 | ## Returns the absolute value of a float.
|
28 |
| - ## >>> Math.abs(-5.5) |
| 28 | + ## >>> Math.absolute(-5.5) |
29 | 29 | ## 5.5
|
30 |
| - ## >>> Math.abs(5.5) |
| 30 | + ## >>> Math.absolute(5.5) |
31 | 31 | ## 5.5
|
32 |
| - return n if n >= 0 else -n |
| 32 | + return number if number >= 0 else -number |
33 | 33 |
|
34 | 34 |
|
35 | 35 | ## For a complex number z = x + yi,
|
36 | 36 | ## we define the absolute value |z| as being the distance from z to 0 in the complex plane C.
|
37 | 37 | ## Reference: https://www2.clarku.edu/faculty/djoyce/complex/abs.html#:~:text=For%20a%20complex%20number%20z,on%20the%20real%20number%20line.
|
38 | 38 | @staticmethod
|
39 |
| - fn abs(n: complext) -> FloatLiteral: |
| 39 | + fn absolute(number: complex) -> FloatLiteral: |
40 | 40 | ## Returns the absolute value of a complex number.
|
41 |
| - ## >>> Math.abs(complext(5, 0)) |
| 41 | + ## >>> Math.absolute(complex(5, 12)) |
| 42 | + ## 13.0 |
| 43 | + ## >>> Math.absolute(complex(3, 4)) |
42 | 44 | ## 5.0
|
43 |
| - ## >>> Math.abs(complext(3, 4)) |
44 |
| - ## 5.0 |
45 |
| - return math.sqrt(n.real ** 2 + n.imag ** 2) |
| 45 | + return math.sqrt(number.real_part ** 2 + number.imaginary_part ** 2) |
0 commit comments