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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn main() raises:
print(a * b) # 152415787654.32099750190521
print(a.true_divide(b, precision=80)) # 100000.0001

# === Mathematical Functions === #
# === Exponential Functions === #
print(a.sqrt(precision=80))
# 11111.111066111110969430554981749302328338130654689094538188579359566416821203641
print(a.cbrt(precision=80))
Expand All @@ -102,6 +102,24 @@ fn main() raises:
print(a.ln(precision=80))
# 18.631401767168018032693933348296537542797015174553735308351756611901741276655161

# === Trigonometric Functions === #
print(a.sin(precision=200))
# 0.99985093087193092464780008002600992896256609588456
# 91036188395766389946401881352599352354527727927177
# 79589259132243649550891532070326452232864052771477
# 31418817041042336608522984511928095747763538486886
print(b.cos(precision=1000))
# -0.9969577603867772005841841569997528013669868536239849713029893885930748434064450375775817720425329394
# 9756020177557431933434791661179643984869397089102223199519409695771607230176923201147218218258755323
# 7563476302904118661729889931783126826250691820526961290122532541861737355873869924820906724540889765
# 5940445990824482174517106016800118438405307801022739336016834311018727787337447844118359555063575166
# 5092352912854884589824773945355279792977596081915868398143592738704592059567683083454055626123436523
# 6998108941189617922049864138929932713499431655377552668020889456390832876383147018828166124313166286
# 6004871998201597316078894718748251490628361253685772937806895692619597915005978762245497623003811386
# 0913693867838452088431084666963414694032898497700907783878500297536425463212578556546527017688874265
# 0785862902484462361413598747384083001036443681873292719322642381945064144026145428927304407689433744
# 5821277763016669042385158254006302666602333649775547203560187716156055524418512492782302125286330865

# === Internal representation of the number === #
(
BDec(
Expand Down
20 changes: 19 additions & 1 deletion docs/examples_on_bdec.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() raises:
print(a * b) # 152415787654.32099750190521
print(a.true_divide(b, precision=80)) # 100000.0001

# === Mathematical Functions === #
# === Exponential Functions === #
print(a.sqrt(precision=80))
# 11111.111066111110969430554981749302328338130654689094538188579359566416821203641
print(a.cbrt(precision=80))
Expand All @@ -27,6 +27,24 @@ fn main() raises:
print(a.ln(precision=80))
# 18.631401767168018032693933348296537542797015174553735308351756611901741276655161

# === Trigonometric Functions === #
print(a.sin(precision=200))
# 0.99985093087193092464780008002600992896256609588456
# 91036188395766389946401881352599352354527727927177
# 79589259132243649550891532070326452232864052771477
# 31418817041042336608522984511928095747763538486886
print(b.cos(precision=1000))
# -0.9969577603867772005841841569997528013669868536239849713029893885930748434064450375775817720425329394
# 9756020177557431933434791661179643984869397089102223199519409695771607230176923201147218218258755323
# 7563476302904118661729889931783126826250691820526961290122532541861737355873869924820906724540889765
# 5940445990824482174517106016800118438405307801022739336016834311018727787337447844118359555063575166
# 5092352912854884589824773945355279792977596081915868398143592738704592059567683083454055626123436523
# 6998108941189617922049864138929932713499431655377552668020889456390832876383147018828166124313166286
# 6004871998201597316078894718748251490628361253685772937806895692619597915005978762245497623003811386
# 0913693867838452088431084666963414694032898497700907783878500297536425463212578556546527017688874265
# 0785862902484462361413598747384083001036443681873292719322642381945064144026145428927304407689433744
# 5821277763016669042385158254006302666602333649775547203560187716156055524418512492782302125286330865

# === Internal representation of the number === #
(
BDec(
Expand Down
13 changes: 12 additions & 1 deletion docs/internal_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

## Values and results

- For power functionality: `BigDecimal.power()`, Python's decimal, WolframAlpha give the same result, but `mpmath` gives a different result. Eample: `0.123456789 ** 1000`, `1234523894766789 ** 1098.1209848`.
- For power functionality: `BigDecimal.power()`, Python's decimal, WolframAlpha give the same result, but `mpmath` gives a different result. Eamples:
- `0.123456789 ** 1000`
- `1234523894766789 ** 1098.1209848`
- For sin functionality: `BigDecimal.sin()` and WolframAlpha give the same results, but `mpmath` gives a different result. This occurs mainly for pi-related values. Examples:
- `sin(3.1415926535897932384626433833)`, precision 50:
- Decimojo: -2.0497115802830600624894179025055407692183593713791E-29
- WolframAlpha: -2.0497115802830600624894179025055407692183593713791 x 10-29
- mpmath: -2.049711580283060062489453928920860542175349360102e-29
- `sin(6.2831853071795864769252867666)`, precision 50:
- Decimojo: 4.4.0994231605661201249788358050110815384367187427582E-29
- WolframAlpha: 4.4.0994231605661201249788358050110815384367187427582 x 10-29
- mpmath: 4.0994231605661201249789078578417210843506987202039e-29

## Time complexity

Expand Down
26 changes: 26 additions & 0 deletions src/decimojo/bigdecimal/bigdecimal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,15 @@ struct BigDecimal(
"""
return decimojo.bigdecimal.arithmetics.truncate_divide(self, other)

@always_inline
fn __mod__(self, other: Self) raises -> Self:
"""Returns the result of modulo operation.
See `arithmetics.truncate_modulo()` for more information.
"""
return decimojo.bigdecimal.arithmetics.truncate_modulo(
self, other, precision=28
)

@always_inline
fn __pow__(self, exponent: Self) raises -> Self:
"""Returns the result of exponentiation."""
Expand Down Expand Up @@ -753,6 +762,16 @@ struct BigDecimal(
return decimojo.bigdecimal.exponential.power(self, exponent, precision)

# === Trigonometric operations === #
@always_inline
fn sin(self, precision: Int = 28) raises -> Self:
"""Returns the sine of the BigDecimal number."""
return decimojo.bigdecimal.trigonometric.sin(self, precision)

@always_inline
fn cos(self, precision: Int = 28) raises -> Self:
"""Returns the cosine of the BigDecimal number."""
return decimojo.bigdecimal.trigonometric.cos(self, precision)

@always_inline
fn arctan(self, precision: Int = 28) raises -> Self:
"""Returns the arctangent of the BigDecimal number."""
Expand Down Expand Up @@ -826,6 +845,13 @@ struct BigDecimal(
fill_zeros_to_precision: Bool,
) raises:
"""Rounds the number to the specified precision in-place.

Notes:

Note that precision is the number of significant digits,
not the number of decimal places. If you want to round to a
specific number of decimal places, use `round()` instead.

See `rounding.round_to_precision()` for more information.
"""
decimojo.bigdecimal.rounding.round_to_precision(
Expand Down
25 changes: 22 additions & 3 deletions src/decimojo/bigdecimal/constants.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ fn pi(precision: Int) raises -> BigDecimal:

# Use precomputed value for precision ≤ 1024
if precision <= 1024:
return PI_1024.round(precision, RoundingMode.ROUND_HALF_EVEN)
var result = PI_1024
result.round_to_precision(
precision,
RoundingMode.ROUND_HALF_EVEN,
remove_extra_digit_due_to_rounding=True,
fill_zeros_to_precision=False,
)
return result^

# Use Chudnovsky with binary splitting for maximum speed
return pi_chudnovsky_binary_split(precision)
Expand Down Expand Up @@ -212,7 +219,13 @@ fn pi_chudnovsky_binary_split(precision: Int) raises -> BigDecimal:
# Final formula: π = 426880 * √10005 / sum_series
var result = bdec_426880 * bdec_10005.sqrt(working_precision) * sum_series

return result.round(precision, RoundingMode.ROUND_HALF_EVEN)
result.round_to_precision(
precision,
RoundingMode.ROUND_HALF_EVEN,
remove_extra_digit_due_to_rounding=True,
fill_zeros_to_precision=False,
)
return result^


fn chudnovsky_split(a: Int, b: Int, precision: Int) raises -> Rational:
Expand Down Expand Up @@ -309,4 +322,10 @@ fn pi_machin(precision: Int) raises -> BigDecimal:
var pi_over_4 = term1 - term2
var result = bdec_4 * pi_over_4

return result.round(precision, RoundingMode.ROUND_HALF_EVEN)
result.round_to_precision(
precision,
RoundingMode.ROUND_HALF_EVEN,
remove_extra_digit_due_to_rounding=True,
fill_zeros_to_precision=False,
)
return result^
6 changes: 3 additions & 3 deletions src/decimojo/bigdecimal/rounding.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ fn round_to_precision(
RoundingMode.ROUND_UP: Round up.
RoundingMode.ROUND_HALF_UP: Round half up.
RoundingMode.ROUND_HALF_EVEN: Round half even.
remove_extra_digit_due_to_rounding: If True, remove an trailing.
digit if the rounding mode result in an extra digit.
fill_zeros_to_precision: If True, fill zeros to the precision.
remove_extra_digit_due_to_rounding: If True, remove a trailing digit if
the rounding mode result in an extra leading digit.
fill_zeros_to_precision: If True, fill trailing zeros to the precision.
"""

var ndigits_coefficient = number.coefficient.number_of_digits()
Expand Down
Loading