Skip to content

Commit 9a4cdee

Browse files
authored
[decimal] Rename floor_divide to truncate_divide to avoid ambiguity (#52)
This pull request involves renaming the `floor_divide` function and all related references to `truncate_divide` across multiple files. The changes ensure consistency in the naming convention and update the logic accordingly. ### Renaming and Updating Functions: * `benches/decimal/bench.mojo`: Renamed `bench_floor_divide` to `bench_truncate_divide` in the import statements and function calls. * `benches/decimal/bench_truncate_divide.mojo`: Renamed from `bench_floor_divide.mojo` and updated all instances of `run_benchmark_floor_divide` to `run_benchmark_truncate_divide`. * `src/decimojo/__init__.mojo`: Updated import statement from `floor_divide` to `truncate_divide`. * `src/decimojo/decimal/arithmetics.mojo`: Renamed `floor_divide` function to `truncate_divide` and updated the corresponding logic in the `modulo` function. ### Updating `Decimal` Struct: * `src/decimojo/decimal/decimal.mojo`: Updated all instances of `floor_divide` to `truncate_divide` in the `__floordiv__`, `__mod__`, `__rfloordiv__`, `__rmod__`, `__ifloordiv__`, and `__imod__` methods. Added docstrings to reflect the changes.
1 parent daa1a12 commit 9a4cdee

File tree

6 files changed

+52
-33
lines changed

6 files changed

+52
-33
lines changed

benches/decimal/bench.mojo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from bench_add import main as bench_add
22
from bench_subtract import main as bench_subtract
33
from bench_multiply import main as bench_multiply
44
from bench_divide import main as bench_divide
5-
from bench_floor_divide import main as bench_floor_divide
5+
from bench_truncate_divide import main as bench_truncate_divide
66
from bench_modulo import main as bench_modulo
77
from bench_sqrt import main as bench_sqrt
88
from bench_from_float import main as bench_from_float
@@ -23,7 +23,7 @@ fn main() raises:
2323
bench_subtract()
2424
bench_multiply()
2525
bench_divide()
26-
bench_floor_divide()
26+
bench_truncate_divide()
2727
bench_modulo()
2828
bench_sqrt()
2929
bench_from_float()

benches/decimal/bench_floor_divide.mojo renamed to benches/decimal/bench_truncate_divide.mojo

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn open_log_file() raises -> PythonObject:
2828

2929
# Generate a timestamp for the filename
3030
var timestamp = String(datetime.datetime.now().isoformat())
31-
var log_filename = log_dir + "/benchmark_floor_divide_" + timestamp + ".log"
31+
var log_filename = log_dir + "/benchmark_truncate_divide_" + timestamp + ".log"
3232

3333
print("Saving benchmark results to:", log_filename)
3434
return python.open(log_filename, "w")
@@ -47,7 +47,7 @@ fn log_print(msg: String, log_file: PythonObject) raises:
4747
log_file.flush() # Ensure the message is written immediately
4848

4949

50-
fn run_benchmark_floor_divide(
50+
fn run_benchmark_truncate_divide(
5151
name: String,
5252
dividend: String,
5353
divisor: String,
@@ -168,7 +168,7 @@ fn main() raises:
168168
)
169169

170170
# Case 1: Basic integer floor division with no remainder
171-
run_benchmark_floor_divide(
171+
run_benchmark_truncate_divide(
172172
"Integer division, no remainder",
173173
"10",
174174
"2",
@@ -178,7 +178,7 @@ fn main() raises:
178178
)
179179

180180
# Case 2: Basic integer floor division with remainder
181-
run_benchmark_floor_divide(
181+
run_benchmark_truncate_divide(
182182
"Integer division, with remainder",
183183
"10",
184184
"3",
@@ -188,7 +188,7 @@ fn main() raises:
188188
)
189189

190190
# Case 3: Division with decimal values
191-
run_benchmark_floor_divide(
191+
run_benchmark_truncate_divide(
192192
"Decimal division",
193193
"10.5",
194194
"2.5",
@@ -198,7 +198,7 @@ fn main() raises:
198198
)
199199

200200
# Case 4: Division resulting in a decimal value
201-
run_benchmark_floor_divide(
201+
run_benchmark_truncate_divide(
202202
"Division resulting in integer",
203203
"5",
204204
"2",
@@ -208,7 +208,7 @@ fn main() raises:
208208
)
209209

210210
# Case 5: Division with different decimal places
211-
run_benchmark_floor_divide(
211+
run_benchmark_truncate_divide(
212212
"Different decimal places",
213213
"10.75",
214214
"1.5",
@@ -218,7 +218,7 @@ fn main() raises:
218218
)
219219

220220
# Case 6: Negative dividend, positive divisor
221-
run_benchmark_floor_divide(
221+
run_benchmark_truncate_divide(
222222
"Negative dividend, positive divisor",
223223
"-10",
224224
"3",
@@ -228,7 +228,7 @@ fn main() raises:
228228
)
229229

230230
# Case 7: Positive dividend, negative divisor
231-
run_benchmark_floor_divide(
231+
run_benchmark_truncate_divide(
232232
"Positive dividend, negative divisor",
233233
"10",
234234
"-3",
@@ -238,7 +238,7 @@ fn main() raises:
238238
)
239239

240240
# Case 8: Negative dividend, negative divisor
241-
run_benchmark_floor_divide(
241+
run_benchmark_truncate_divide(
242242
"Negative dividend, negative divisor",
243243
"-10",
244244
"-3",
@@ -248,7 +248,7 @@ fn main() raises:
248248
)
249249

250250
# Case 9: Decimal values, negative dividend, positive divisor
251-
run_benchmark_floor_divide(
251+
run_benchmark_truncate_divide(
252252
"Decimal, negative dividend, positive divisor",
253253
"-10.5",
254254
"3.5",
@@ -258,7 +258,7 @@ fn main() raises:
258258
)
259259

260260
# Case 10: Division by 1
261-
run_benchmark_floor_divide(
261+
run_benchmark_truncate_divide(
262262
"Division by 1",
263263
"10",
264264
"1",
@@ -268,7 +268,7 @@ fn main() raises:
268268
)
269269

270270
# Case 11: Zero dividend
271-
run_benchmark_floor_divide(
271+
run_benchmark_truncate_divide(
272272
"Zero dividend",
273273
"0",
274274
"5",
@@ -278,7 +278,7 @@ fn main() raises:
278278
)
279279

280280
# Case 12: Division by a decimal < 1
281-
run_benchmark_floor_divide(
281+
run_benchmark_truncate_divide(
282282
"Division by decimal < 1",
283283
"10",
284284
"0.5",
@@ -288,7 +288,7 @@ fn main() raises:
288288
)
289289

290290
# Case 13: Division resulting in a negative zero
291-
run_benchmark_floor_divide(
291+
run_benchmark_truncate_divide(
292292
"Division resulting in zero",
293293
"0",
294294
"-5",
@@ -298,7 +298,7 @@ fn main() raises:
298298
)
299299

300300
# Case 14: Large number division
301-
run_benchmark_floor_divide(
301+
run_benchmark_truncate_divide(
302302
"Large number division",
303303
"1000000000",
304304
"7",
@@ -308,7 +308,7 @@ fn main() raises:
308308
)
309309

310310
# Case 15: Small number division
311-
run_benchmark_floor_divide(
311+
run_benchmark_truncate_divide(
312312
"Small number division",
313313
"0.0000001",
314314
"0.0000002",
@@ -318,7 +318,7 @@ fn main() raises:
318318
)
319319

320320
# Case 16: Very large dividend and divisor
321-
run_benchmark_floor_divide(
321+
run_benchmark_truncate_divide(
322322
"Large dividend and divisor",
323323
"123456789012345",
324324
"987654321",
@@ -328,7 +328,7 @@ fn main() raises:
328328
)
329329

330330
# Case 17: High precision dividend
331-
run_benchmark_floor_divide(
331+
run_benchmark_truncate_divide(
332332
"High precision dividend",
333333
"3.14159265358979323846",
334334
"1.5",
@@ -338,7 +338,7 @@ fn main() raises:
338338
)
339339

340340
# Case 18: Very close values
341-
run_benchmark_floor_divide(
341+
run_benchmark_truncate_divide(
342342
"Very close values",
343343
"1.0000001",
344344
"1",
@@ -348,7 +348,7 @@ fn main() raises:
348348
)
349349

350350
# Case 19: Power of 10 values
351-
run_benchmark_floor_divide(
351+
run_benchmark_truncate_divide(
352352
"Power of 10 values",
353353
"10000",
354354
"100",
@@ -358,7 +358,7 @@ fn main() raises:
358358
)
359359

360360
# Case 20: Edge values not quite reaching the next integer
361-
run_benchmark_floor_divide(
361+
run_benchmark_truncate_divide(
362362
"Edge values",
363363
"9.9999999",
364364
"2",

src/decimojo/__init__.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ from .decimal.arithmetics import (
3838
negative,
3939
multiply,
4040
divide,
41-
floor_divide,
41+
truncate_divide,
4242
modulo,
4343
)
4444

src/decimojo/decimal/arithmetics.mojo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,8 @@ fn divide(x1: Decimal, x2: Decimal) raises -> Decimal:
11381138
return Decimal(low, mid, high, scale_of_truncated_quot, is_negative)
11391139

11401140

1141-
fn floor_divide(x1: Decimal, x2: Decimal) raises -> Decimal:
1142-
"""Returns the integral part of the true quotient (truncating towards zero).
1141+
fn truncate_divide(x1: Decimal, x2: Decimal) raises -> Decimal:
1142+
"""Returns the integral part of the quotient (truncating towards zero).
11431143
The following identity always holds: x_1 == (x_1 // x_2) * x_2 + x_1 % x_2.
11441144
11451145
Args:
@@ -1167,6 +1167,6 @@ fn modulo(x1: Decimal, x2: Decimal) raises -> Decimal:
11671167
A new Decimal containing the remainder of x1 / x2.
11681168
"""
11691169
try:
1170-
return x1 - (floor_divide(x1, x2) * x2)
1170+
return x1 - (truncate_divide(x1, x2) * x2)
11711171
except e:
11721172
raise Error("Error in `modulo()`: ", e)

src/decimojo/decimal/decimal.mojo

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,18 +1190,24 @@ struct Decimal(
11901190

11911191
@always_inline
11921192
fn __floordiv__(self, other: Self) raises -> Self:
1193-
return decimojo.decimal.arithmetics.floor_divide(self, other)
1193+
"""Performs truncate division with // operator."""
1194+
return decimojo.decimal.arithmetics.truncate_divide(self, other)
11941195

11951196
@always_inline
11961197
fn __floordiv__(self, other: Int) raises -> Self:
1197-
return decimojo.decimal.arithmetics.floor_divide(self, Decimal(other))
1198+
"""Performs truncate division with // operator."""
1199+
return decimojo.decimal.arithmetics.truncate_divide(
1200+
self, Decimal(other)
1201+
)
11981202

11991203
@always_inline
12001204
fn __mod__(self, other: Self) raises -> Self:
1205+
"""Performs truncate modulo."""
12011206
return decimojo.decimal.arithmetics.modulo(self, other)
12021207

12031208
@always_inline
12041209
fn __mod__(self, other: Int) raises -> Self:
1210+
"""Performs truncate modulo."""
12051211
return decimojo.decimal.arithmetics.modulo(self, Decimal(other))
12061212

12071213
@always_inline
@@ -1237,10 +1243,14 @@ struct Decimal(
12371243

12381244
@always_inline
12391245
fn __rfloordiv__(self, other: Int) raises -> Self:
1240-
return decimojo.decimal.arithmetics.floor_divide(Decimal(other), self)
1246+
"""Performs truncate division with // operator."""
1247+
return decimojo.decimal.arithmetics.truncate_divide(
1248+
Decimal(other), self
1249+
)
12411250

12421251
@always_inline
12431252
fn __rmod__(self, other: Int) raises -> Self:
1253+
"""Performs truncate modulo."""
12441254
return decimojo.decimal.arithmetics.modulo(Decimal(other), self)
12451255

12461256
# ===------------------------------------------------------------------=== #
@@ -1284,11 +1294,20 @@ struct Decimal(
12841294

12851295
@always_inline
12861296
fn __ifloordiv__(mut self, other: Self) raises:
1287-
self = decimojo.decimal.arithmetics.floor_divide(self, other)
1297+
"""Performs truncate division with // operator."""
1298+
self = decimojo.decimal.arithmetics.truncate_divide(self, other)
12881299

12891300
@always_inline
12901301
fn __ifloordiv__(mut self, other: Int) raises:
1291-
self = decimojo.decimal.arithmetics.floor_divide(self, Decimal(other))
1302+
"""Performs truncate division with // operator."""
1303+
self = decimojo.decimal.arithmetics.truncate_divide(
1304+
self, Decimal(other)
1305+
)
1306+
1307+
@always_inline
1308+
fn __imod__(mut self, other: Self) raises:
1309+
"""Performs truncate modulo."""
1310+
self = decimojo.decimal.arithmetics.modulo(self, other)
12921311

12931312
# ===------------------------------------------------------------------=== #
12941313
# Basic binary comparison operation dunders

0 commit comments

Comments
 (0)