Skip to content

Commit 6b9d43c

Browse files
authored
[docstring] Ensure that all functions, fields, and constants have docstrings + Eliminate mojo doc warnings (#194)
This PR focuses on improving generated documentation by adding/expanding docstrings across the TOML tokenizer/parser and the numeric types (BigInt/BigUInt/BigDecimal/Decimal128/etc.), with the goal of eliminating `mojo doc` warnings. **Changes:** - Added docstrings for structs, fields, constants, and many methods across the codebase. - Standardized docstring sections (Args/Returns/Parameters) in multiple modules. - Removed an unused error-message header constant from `errors.mojo`.
1 parent b989278 commit 6b9d43c

22 files changed

+3807
-345
lines changed

src/decimo/bigdecimal/bigdecimal.mojo

Lines changed: 603 additions & 46 deletions
Large diffs are not rendered by default.

src/decimo/bigdecimal/comparison.mojo

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,44 +111,108 @@ def compare(x1: BigDecimal, x2: BigDecimal) -> Int8:
111111

112112

113113
def equal(x1: BigDecimal, x2: BigDecimal) -> Bool:
114-
"""Returns whether x1 equals x2."""
114+
"""Returns whether x1 equals x2.
115+
116+
Args:
117+
x1: The first operand.
118+
x2: The second operand.
119+
120+
Returns:
121+
True if x1 equals x2, False otherwise.
122+
"""
115123
return compare(x1, x2) == 0
116124

117125

118126
def not_equal(x1: BigDecimal, x2: BigDecimal) -> Bool:
119-
"""Returns whether x1 does not equal x2."""
127+
"""Returns whether x1 does not equal x2.
128+
129+
Args:
130+
x1: The first operand.
131+
x2: The second operand.
132+
133+
Returns:
134+
True if x1 does not equal x2, False otherwise.
135+
"""
120136
return compare(x1, x2) != 0
121137

122138

123139
def less(x1: BigDecimal, x2: BigDecimal) -> Bool:
124-
"""Returns whether x1 is less than x2."""
140+
"""Returns whether x1 is less than x2.
141+
142+
Args:
143+
x1: The first operand.
144+
x2: The second operand.
145+
146+
Returns:
147+
True if x1 is less than x2, False otherwise.
148+
"""
125149
return compare(x1, x2) < 0
126150

127151

128152
def less_equal(x1: BigDecimal, x2: BigDecimal) -> Bool:
129-
"""Returns whether x1 is less than or equal to x2."""
153+
"""Returns whether x1 is less than or equal to x2.
154+
155+
Args:
156+
x1: The first operand.
157+
x2: The second operand.
158+
159+
Returns:
160+
True if x1 is less than or equal to x2, False otherwise.
161+
"""
130162
return compare(x1, x2) <= 0
131163

132164

133165
def greater(x1: BigDecimal, x2: BigDecimal) -> Bool:
134-
"""Returns whether x1 is greater than x2."""
166+
"""Returns whether x1 is greater than x2.
167+
168+
Args:
169+
x1: The first operand.
170+
x2: The second operand.
171+
172+
Returns:
173+
True if x1 is greater than x2, False otherwise.
174+
"""
135175
return compare(x1, x2) > 0
136176

137177

138178
def greater_equal(x1: BigDecimal, x2: BigDecimal) -> Bool:
139-
"""Returns whether x1 is greater than or equal to x2."""
179+
"""Returns whether x1 is greater than or equal to x2.
180+
181+
Args:
182+
x1: The first operand.
183+
x2: The second operand.
184+
185+
Returns:
186+
True if x1 is greater than or equal to x2, False otherwise.
187+
"""
140188
return compare(x1, x2) >= 0
141189

142190

143191
def max(x1: BigDecimal, x2: BigDecimal) -> BigDecimal:
144-
"""Returns the maximum of x1 and x2."""
192+
"""Returns the maximum of x1 and x2.
193+
194+
Args:
195+
x1: The first operand.
196+
x2: The second operand.
197+
198+
Returns:
199+
The larger of the two values.
200+
"""
145201
if compare(x1, x2) >= 0:
146202
return x1.copy()
147203
return x2.copy()
148204

149205

150206
def min(x1: BigDecimal, x2: BigDecimal) -> BigDecimal:
151-
"""Returns the minimum of x1 and x2."""
207+
"""Returns the minimum of x1 and x2.
208+
209+
Args:
210+
x1: The first operand.
211+
x2: The second operand.
212+
213+
Returns:
214+
The smaller of the two values.
215+
"""
152216
if compare(x1, x2) <= 0:
153217
return x1.copy()
154218
return x2.copy()

src/decimo/bigdecimal/constants.mojo

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,14 @@ comptime PI_1024 = BigDecimal(
154154
# we check whether the precision is higher than the current precision.
155155
# If yes, then we save it into the global scope as cached value.
156156
def pi(precision: Int) raises -> BigDecimal:
157-
"""Calculates π using the fastest available algorithm."""
157+
"""Calculates π using the fastest available algorithm.
158+
159+
Args:
160+
precision: The number of significant digits to compute.
161+
162+
Returns:
163+
The value of π to the specified precision.
164+
"""
158165

159166
if precision < 0:
160167
raise Error("Precision must be non-negative")
@@ -179,9 +186,17 @@ struct Rational:
179186
"""Represents a rational number p/q for exact arithmetic."""
180187

181188
var p: BigInt10 # numerator
189+
"""The numerator of the rational number."""
182190
var q: BigInt10 # denominator
191+
"""The denominator of the rational number."""
183192

184193
def __init__(out self, p: BigInt10, q: BigInt10):
194+
"""Initializes a rational number from a numerator and denominator.
195+
196+
Args:
197+
p: The numerator.
198+
q: The denominator.
199+
"""
185200
self.p = p.copy()
186201
self.q = q.copy()
187202

@@ -197,6 +212,12 @@ def pi_chudnovsky_binary_split(precision: Int) raises -> BigDecimal:
197212
(1) M(k) = (6k)! / ((3k)! * (k!)³)
198213
(2) L(k) = 545140134*k + 13591409
199214
(3) X(k) = (-262537412640768000)^k
215+
216+
Args:
217+
precision: The number of significant digits to compute.
218+
219+
Returns:
220+
The value of π to the specified precision.
200221
"""
201222

202223
var working_precision = precision + 9 # 1 words
@@ -228,7 +249,16 @@ def pi_chudnovsky_binary_split(precision: Int) raises -> BigDecimal:
228249

229250

230251
def chudnovsky_split(a: Int, b: Int, precision: Int) raises -> Rational:
231-
"""Conducts binary splitting for Chudnovsky series from term a to b-1."""
252+
"""Conducts binary splitting for Chudnovsky series from term a to b-1.
253+
254+
Args:
255+
a: The start index of the splitting range (inclusive).
256+
b: The end index of the splitting range (exclusive).
257+
precision: The working precision for intermediate calculations.
258+
259+
Returns:
260+
A `Rational` representing the partial sum of the Chudnovsky series.
261+
"""
232262

233263
var bint_1 = BigInt10(1)
234264
var bint_13591409 = BigInt10(13591409)
@@ -273,7 +303,14 @@ def chudnovsky_split(a: Int, b: Int, precision: Int) raises -> Rational:
273303

274304

275305
def compute_m_k_rational(k: Int) raises -> Rational:
276-
"""Computes M(k) = (6k)! / ((3k)! * (k!)³) as exact rational."""
306+
"""Computes M(k) = (6k)! / ((3k)! * (k!)³) as exact rational.
307+
308+
Args:
309+
k: The term index in the Chudnovsky series.
310+
311+
Returns:
312+
A `Rational` with numerator (6k)!/(3k)! and denominator (k!)³.
313+
"""
277314

278315
var bint_1 = BigInt10(1)
279316

@@ -296,7 +333,14 @@ def compute_m_k_rational(k: Int) raises -> Rational:
296333

297334

298335
def pi_machin(precision: Int) raises -> BigDecimal:
299-
"""Fallback π calculation using Machin's formula."""
336+
"""Fallback π calculation using Machin's formula.
337+
338+
Args:
339+
precision: The number of significant digits to compute.
340+
341+
Returns:
342+
The value of π to the specified precision.
343+
"""
300344

301345
var working_precision = precision + 9
302346

src/decimo/bigdecimal/rounding.mojo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def round(
5454
round(123.456, -2) -> 1E+2
5555
round(123.456, -3) -> 0E+3
5656
round(678.890, -3) -> 1E+3
57+
58+
Returns:
59+
A new `BigDecimal` rounded to the specified number of decimal places.
5760
"""
5861

5962
var ndigits_to_remove = number.scale - ndigits

src/decimo/bigdecimal/trigonometric.mojo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,13 @@ def arctan(x: BigDecimal, precision: Int) raises -> BigDecimal:
477477
y = arctan(x),
478478
where x can be all real numbers,
479479
and y is in the range (-π/2, π/2).
480+
481+
Args:
482+
x: The input number to compute the arctangent of.
483+
precision: The number of significant digits for the result.
484+
485+
Returns:
486+
The arctangent of x in radians, in the range (-π/2, π/2).
480487
"""
481488

482489
comptime BUFFER_DIGITS = 9 # word-length, easy to append and trim

0 commit comments

Comments
 (0)