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
10 changes: 8 additions & 2 deletions src/decimo/bigdecimal/arithmetics.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ def true_divide(
The quotient of x and y, with precision up to `precision`
significant digits.

Raises:
ZeroDivisionError: If the divisor is zero.

Notes:

- If the coefficients can be divided exactly, the number of digits after
Expand Down Expand Up @@ -692,6 +695,9 @@ def true_divide_inexact(

Returns:
The quotient of x1 and x2.

Raises:
ZeroDivisionError: If the divisor is zero.
"""

# Check for division by zero
Expand Down Expand Up @@ -904,7 +910,7 @@ def truncate_divide(x1: BigDecimal, x2: BigDecimal) raises -> BigDecimal:
The quotient of x1 and x2, truncated to zeros.

Raises:
Error: If division by zero is attempted.
ZeroDivisionError: If division by zero is attempted.

Notes:
This function performs integer division that truncates toward zero.
Expand Down Expand Up @@ -951,7 +957,7 @@ def truncate_modulo(
The truncated modulo of x1 and x2.

Raises:
Error: If division by zero is attempted.
ZeroDivisionError: If division by zero is attempted.
"""
# Check for division by zero
if x2.coefficient.is_zero():
Expand Down
6 changes: 5 additions & 1 deletion src/decimo/bigdecimal/bigdecimal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ struct BigDecimal(
Returns:
The BigDecimal representation of the Scalar value.

Raises:
ValueError: If the value is NaN.
ConversionError: If the conversion from scalar to BigDecimal fails.

Notes:

If the value is a floating-point number, it is converted to a string
Expand Down Expand Up @@ -430,7 +434,7 @@ struct BigDecimal(
The BigDecimal representation of the Python Decimal.

Raises:
Error: If the conversion from Python Decimal fails, or if
ConversionError: If the conversion from Python Decimal fails, or if
the as_tuple() method returns invalid data.

Examples:
Expand Down
3 changes: 3 additions & 0 deletions src/decimo/bigdecimal/constants.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def pi(precision: Int) raises -> BigDecimal:

Returns:
The value of π to the specified precision.

Raises:
ValueError: If the precision is negative.
"""

if precision < 0:
Expand Down
41 changes: 21 additions & 20 deletions src/decimo/bigdecimal/exponential.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ def power(
The result of base^exponent.

Raises:
Error: If base is negative and exponent is not an integer.
Error: If base is zero and exponent is negative or zero.
ValueError: If base is negative and exponent is not an integer.
ValueError: If base is zero and exponent is zero.
ZeroDivisionError: If base is zero and exponent is negative.

Notes:

Expand Down Expand Up @@ -425,8 +426,8 @@ def root(x: BigDecimal, n: BigDecimal, precision: Int) raises -> BigDecimal:
The nth root of x with the specified precision.

Raises:
Error: If x is negative and n is not an odd integer.
Error: If n is zero.
ValueError: If x is negative and n is not an odd integer.
ValueError: If n is zero.

Notes:
Uses the identity x^(1/n) = exp(ln(|x|)/n) for calculation.
Expand Down Expand Up @@ -552,9 +553,9 @@ def integer_root(
The nth root of x with the specified precision.

Raises:
Error: If x is negative and n is even.
Error: If n is not a positive integer.
Error: If n is zero.
ValueError: If x is negative and n is even.
ValueError: If n is not a positive integer.
ValueError: If n is zero.
"""
comptime BUFFER_DIGITS = 9
var working_precision = precision + BUFFER_DIGITS
Expand Down Expand Up @@ -1016,7 +1017,7 @@ def sqrt(x: BigDecimal, precision: Int) raises -> BigDecimal:
The square root of x with the specified precision.

Raises:
Error: If x is negative.
ValueError: If x is negative.
"""
return sqrt_exact(x, precision)

Expand Down Expand Up @@ -1199,7 +1200,7 @@ def sqrt_exact(x: BigDecimal, precision: Int) raises -> BigDecimal:
The square root of x with the specified precision.

Raises:
Error: If x is negative.
ValueError: If x is negative.
"""

# Handle special cases
Expand Down Expand Up @@ -1333,7 +1334,7 @@ def sqrt_reciprocal(x: BigDecimal, precision: Int) raises -> BigDecimal:
The square root of x with the specified precision.

Raises:
Error: If x is negative.
ValueError: If x is negative.
"""

# Handle special cases
Expand Down Expand Up @@ -1496,7 +1497,7 @@ def sqrt_newton(x: BigDecimal, precision: Int) raises -> BigDecimal:
The square root of x with the specified precision.

Raises:
Error: If x is negative.
ValueError: If x is negative.

Notes:

Expand Down Expand Up @@ -1593,7 +1594,7 @@ def sqrt_decimal_approach(x: BigDecimal, precision: Int) raises -> BigDecimal:
The square root of x with the specified precision.

Raises:
Error: If x is negative.
ValueError: If x is negative.

Notes:

Expand Down Expand Up @@ -1746,9 +1747,6 @@ def cbrt(x: BigDecimal, precision: Int) raises -> BigDecimal:

Returns:
The cube root of x with the specified precision.

Raises:
Error: If x is negative.
"""

result = integer_root(
Expand All @@ -1774,6 +1772,9 @@ def exp(x: BigDecimal, precision: Int) raises -> BigDecimal:
Returns:
The natural exponential of x (e^x) to the specified precision.

Raises:
OverflowError: If the result is too large to represent.

Notes:
Uses aggressive range reduction for optimal performance:
1. Divide x by 2^M where M ≈ √(3.322·precision) to make x tiny
Expand Down Expand Up @@ -1970,7 +1971,7 @@ def ln(x: BigDecimal, precision: Int) raises -> BigDecimal:
The natural logarithm of x to the specified precision.

Raises:
Error: If x is negative or zero.
ValueError: If x is negative or zero.
"""
var cache = MathCache()
return ln(x, precision, cache)
Expand All @@ -1993,7 +1994,7 @@ def ln(
The natural logarithm of x to the specified precision.

Raises:
Error: If x is negative or zero.
ValueError: If x is negative or zero.
"""
comptime BUFFER_DIGITS = 9 # word-length, easy to append and trim
var working_precision = precision + BUFFER_DIGITS
Expand Down Expand Up @@ -2088,8 +2089,8 @@ def log(x: BigDecimal, base: BigDecimal, precision: Int) raises -> BigDecimal:
The logarithm of x with respect to base.

Raises:
Error: If x is negative or zero.
Error: If base is negative, zero, or one.
ValueError: If x is negative or zero.
ValueError: If base is negative, zero, or one.
"""
comptime BUFFER_DIGITS = 9 # word-length, easy to append and trim
var working_precision = precision + BUFFER_DIGITS
Expand Down Expand Up @@ -2157,7 +2158,7 @@ def log10(x: BigDecimal, precision: Int) raises -> BigDecimal:
The base-10 logarithm of x.

Raises:
Error: If x is negative or zero.
ValueError: If x is negative or zero.
"""
comptime BUFFER_DIGITS = 9 # word-length, easy to append and trim
var working_precision = precision + BUFFER_DIGITS
Expand Down
6 changes: 6 additions & 0 deletions src/decimo/bigdecimal/trigonometric.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ def tan_cot(x: BigDecimal, precision: Int, is_tan: Bool) raises -> BigDecimal:
Returns:
The cotangent of x with the specified precision.

Raises:
ValueError: If computing cot(nπ) which is undefined.

Notes:

This function calculates tan(x) = cos(x) / sin(x) or
Expand Down Expand Up @@ -425,6 +428,9 @@ def csc(x: BigDecimal, precision: Int) raises -> BigDecimal:
Returns:
The cosecant of x with the specified precision.

Raises:
ValueError: If x is zero (csc(nπ) is undefined).

Notes:

This function calculates csc(x) = 1 / sin(x).
Expand Down
Loading
Loading