Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions benches/bench.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from bench_divide import main as bench_divide
from bench_sqrt import main as bench_sqrt
from bench_from_float import main as bench_from_float
from bench_from_string import main as bench_from_string
from bench_round import main as bench_round
from bench_comparison import main as bench_comparison
from bench_exp import main as bench_exp

Expand All @@ -17,5 +18,6 @@ fn main() raises:
bench_sqrt()
bench_from_float()
bench_from_string()
bench_round()
bench_comparison()
bench_exp()
1 change: 1 addition & 0 deletions mojoproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ debug_sqrt = "magic run package && magic run mojo tests/test_sqrt.mojo && magic
test = "magic run package && magic run mojo test tests && magic run delete_package"
t = "clear && magic run test"
test_arith = "magic run package && magic run mojo test tests/test_arithmetics.mojo && magic run delete_package"
test_multiply = "magic run package && magic run mojo test tests/test_multiply.mojo && magic run delete_package"
test_divide = "magic run package && magic run mojo test tests/test_division.mojo && magic run delete_package"
test_sqrt = "magic run package && magic run mojo test tests/test_sqrt.mojo && magic run delete_package"
test_round = "magic run package && magic run mojo test tests/test_round.mojo && magic run delete_package"
Expand Down
11 changes: 6 additions & 5 deletions src/decimojo/arithmetics.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,13 @@ fn multiply(x1: Decimal, x2: Decimal) raises -> Decimal:
# SPECIAL CASE: zero
# Return zero while preserving the scale
if x1_coef == 0 or x2_coef == 0:
var result = Decimal.ZERO()
var result_scale = min(combined_scale, Decimal.MAX_SCALE)
result.flags = UInt32(
(result_scale << Decimal.SCALE_SHIFT) & Decimal.SCALE_MASK
return Decimal(
0,
0,
0,
scale=min(combined_scale, Decimal.MAX_SCALE),
sign=is_negative,
)
return result

# SPECIAL CASE: Both operands have coefficient of 1
if x1_coef == 1 and x2_coef == 1:
Expand Down
172 changes: 0 additions & 172 deletions tests/test_arithmetics.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -466,175 +466,6 @@ fn test_subtract() raises:
print("Decimal subtraction tests passed!")


fn test_multiplication() raises:
print("------------------------------------------------------")
print("Testing decimal multiplication...")

# Test case 1: Simple multiplication with same scale
var a1 = Decimal("12.34")
var b1 = Decimal("5.67")
var result1 = a1 * b1
testing.assert_equal(
String(result1), "69.9678", "Simple multiplication with same scale"
)

# Test case 2: Multiplication with different scales
var a2 = Decimal("12.3")
var b2 = Decimal("5.67")
var result2 = a2 * b2
testing.assert_equal(
String(result2), "69.741", "Multiplication with different scales"
)

# Test case 3: Multiplication with negative numbers
var a3 = Decimal("12.34")
var b3 = Decimal("-5.67")
var result3 = a3 * b3
testing.assert_equal(
String(result3), "-69.9678", "Multiplication with negative number"
)

# Test case 4: Multiplication with both negative numbers
var a4 = Decimal("-12.34")
var b4 = Decimal("-5.67")
var result4 = a4 * b4
testing.assert_equal(
String(result4), "69.9678", "Multiplication with both negative numbers"
)

# Test case 5: Multiplication by zero
var a5 = Decimal("12.34")
var b5 = Decimal("0.00")
var result5 = a5 * b5
testing.assert_equal(String(result5), "0.0000", "Multiplication by zero")

# Test case 6: Multiplication by one
var a6 = Decimal("12.34")
var b6 = Decimal("1.00")
var result6 = a6 * b6
testing.assert_equal(String(result6), "12.3400", "Multiplication by one")

# Test case 7: Multiplication with large scales
var a7 = Decimal("0.0001")
var b7 = Decimal("0.0002")
var result7 = a7 * b7
testing.assert_equal(
String(result7), "0.00000002", "Multiplication with large scales"
)

# Test case 8: Multiplication resulting in scale truncation
var a8 = Decimal("0.123456789")
var b8 = Decimal("0.987654321")
var result8 = a8 * b8
testing.assert_equal(
String(result8),
"0.121932631112635269",
"Multiplication with scale truncation",
)

# Test case 9: Multiplication of large numbers
var a9 = Decimal("1234567.89")
var b9 = Decimal("9876543.21")
var result9 = a9 * b9
testing.assert_equal(
String(result9),
"12193263111263.5269",
"Multiplication of large numbers",
)

# Test case 10: Verify that a * b = b * a (commutative property)
var a10 = Decimal("123.456")
var b10 = Decimal("789.012")
var result10a = a10 * b10
var result10b = b10 * a10
testing.assert_equal(
String(result10a),
String(result10b),
"Multiplication should be commutative",
)

# Test case 11: Multiplication near precision limits
var a11 = Decimal("0." + "1" * 10)
var b11 = Decimal("0." + "1" * 18)
var result11 = a11 * b11
testing.assert_equal(
String(result11),
"0.0123456790111111110987654321",
"Multiplication near precision limits",
)

# Test case 12: Multiplication with integers
var a12 = Decimal("123")
var b12 = Decimal("456")
var result12 = a12 * b12
testing.assert_equal(
String(result12), "56088", "Multiplication with integers"
)

# Test case 13: Multiplication with powers of 10
var a13 = Decimal("12.34")
var b13 = Decimal("10")
var result13 = a13 * b13
testing.assert_equal(
String(result13), "123.40", "Multiplication by power of 10"
)

# Test case 14: Multiplication with very small numbers
var a14 = Decimal("0." + "0" * 25 + "1")
var b14 = Decimal("0." + "0" * 25 + "1")
var result14 = a14 * b14
testing.assert_equal(
String(result14),
"0." + "0" * 28,
"Multiplication of very small numbers",
)

# Test case 15: Verify that a * 0 = 0 for various values - individual test cases
var zero = Decimal("0")

# Test 15a: Multiplication of zero by zero
var value15a = Decimal("0")
testing.assert_equal(
String(value15a * zero),
"0",
"Multiplication by zero should yield zero (zero case)",
)

# Test 15b: Multiplication of positive number by zero
var value15b = Decimal("123.45")
testing.assert_equal(
String(value15b * zero),
"0.00",
"Multiplication by zero should yield zero (positive number case)",
)

# Test 15c: Multiplication of negative number by zero
var value15c = Decimal("-987.654")
testing.assert_equal(
String(value15c * zero),
"0.000",
"Multiplication by zero should yield zero (negative number case)",
)

# Test 15d: Multiplication of small number by zero
var value15d = Decimal("0.0001")
testing.assert_equal(
String(value15d * zero),
"0.0000",
"Multiplication by zero should yield zero (small number case)",
)

# Test 15e: Multiplication of large negative number by zero
var value15e = Decimal("-99999.99999")
testing.assert_equal(
String(value15e * zero),
"0.00000",
"Multiplication by zero should yield zero (large negative number case)",
)

print("Decimal multiplication tests passed!")


fn test_power_integer_exponents() raises:
print("------------------------------------------------------")
print("Testing power with integer exponents...")
Expand Down Expand Up @@ -894,9 +725,6 @@ fn main() raises:
# Run subtraction tests
test_subtract()

# Run multiplication tests
test_multiplication()

# Run power tests with integer exponents
test_power_integer_exponents()

Expand Down
Loading
Loading