Skip to content

Commit f098174

Browse files
authored
[test] Add separate testing files for __int__, __float__, __str__ (#34)
This pull request includes several changes to the `DeciMojo` library, focusing on code cleanup, new test additions, and minor optimizations. The most important changes include the addition of a new test case, a fix in the `Decimal` struct, and the removal of unnecessary comment lines. ### New test additions: * [`mojoproject.toml`](diffhunk://#diff-1b0ef62120bccf4c05a76e60e13fe686f33eb92d5897b3f05e0c3f8d737fc5c0R43): Added a new test case `test_to_float` to the test suite. ### Test removal: * [`tests/test_conversions.mojo`](diffhunk://#diff-d43bab99eda0af629ae4f094121805ed00644a6fa4ff98de5d068111ff7e7738L1-L129): Removed the entire file containing tests for Decimal conversion methods and split it into `test_to_string` and `test_to_int`. ### Code optimizations: * [`src/decimojo/decimal.mojo`](diffhunk://#diff-c2178e6e0bbbbb0d3379f1513412224309d1232c444654e77abb5ce36b3a854fL970-R968): Fixed the `to_float` method in the `Decimal` struct to correctly use `Float64(10)` for division, ensuring proper floating-point arithmetic. ### Documentation updates: * [`docs/todo.md`](diffhunk://#diff-d84697334acf64fe5d67dfd34f6370dca462f602aa3a720711eae12bf8dd153dL5-R5): Marked the optimization task for the `exp()` function as completed.
1 parent 53fa909 commit f098174

16 files changed

Lines changed: 326 additions & 151 deletions

docs/todo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
This is a to-do list for Yuhao's personal use.
44

5-
- The `exp()` function performs slower than Python's counterpart in specific cases. Detailed investigation reveals the bottleneck stems from multiplication operations between decimals with significant fractional components. These operations currently rely on UInt256 arithmetic, which introduces performance overhead. Optimization of the `multiply()` function is required to address these performance bottlenecks, particularly for high-precision decimal multiplication with many digits after the decimal point.
5+
- [x] (#31) The `exp()` function performs slower than Python's counterpart in specific cases. Detailed investigation reveals the bottleneck stems from multiplication operations between decimals with significant fractional components. These operations currently rely on UInt256 arithmetic, which introduces performance overhead. Optimization of the `multiply()` function is required to address these performance bottlenecks, particularly for high-precision decimal multiplication with many digits after the decimal point.

mojoproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ test_round = "magic run package && magic run mojo test tests/test_round.mojo &&
4040
test_creation = "magic run package && magic run mojo test tests/test_creation.mojo && magic run delete_package"
4141
test_from_float = "magic run package && magic run mojo test tests/test_from_float.mojo && magic run delete_package"
4242
test_from_string = "magic run package && magic run mojo test tests/test_from_string.mojo && magic run delete_package"
43+
test_to_float = "magic run package && magic run mojo test tests/test_to_float.mojo && magic run delete_package"
4344
test_comparison = "magic run package && magic run mojo test tests/test_comparison.mojo && magic run delete_package"
4445
test_factorial = "magic run package && magic run mojo test tests/test_factorial.mojo && magic run delete_package"
4546
test_exp = "magic run package && magic run mojo test tests/test_exp.mojo && magic run delete_package"

src/decimojo/__init__.mojo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# ===----------------------------------------------------------------------=== #
2-
#
32
# DeciMojo: A fixed-point decimal arithmetic library in Mojo
43
# https://github.com/forFudan/DeciMojo
54
#
@@ -16,7 +15,6 @@
1615
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1716
# See the License for the specific language governing permissions and
1817
# limitations under the License.
19-
#
2018
# ===----------------------------------------------------------------------=== #
2119

2220
"""

src/decimojo/arithmetics.mojo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# ===----------------------------------------------------------------------=== #
2-
#
32
# DeciMojo: A fixed-point decimal arithmetic library in Mojo
43
# https://github.com/forFudan/DeciMojo
54
#
@@ -16,7 +15,6 @@
1615
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1716
# See the License for the specific language governing permissions and
1817
# limitations under the License.
19-
#
2018
# ===----------------------------------------------------------------------=== #
2119
#
2220
# Implements basic arithmetic functions for the Decimal type

src/decimojo/comparison.mojo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# ===----------------------------------------------------------------------=== #
2-
#
32
# DeciMojo: A fixed-point decimal arithmetic library in Mojo
43
# https://github.com/forFudan/DeciMojo
54
#
@@ -16,7 +15,6 @@
1615
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1716
# See the License for the specific language governing permissions and
1817
# limitations under the License.
19-
#
2018
# ===----------------------------------------------------------------------=== #
2119
#
2220
# Implements comparison operations for the Decimal type

src/decimojo/decimal.mojo

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# ===----------------------------------------------------------------------=== #
2-
#
32
# DeciMojo: A fixed-point decimal arithmetic library in Mojo
43
# https://github.com/forFudan/DeciMojo
54
#
@@ -16,7 +15,6 @@
1615
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1716
# See the License for the specific language governing permissions and
1817
# limitations under the License.
19-
#
2018
# ===----------------------------------------------------------------------=== #
2119
#
2220
# Implements basic object methods for the Decimal type
@@ -967,7 +965,7 @@ struct Decimal(
967965
The floating-point representation of this Decimal.
968966
"""
969967

970-
var result = Float64(self.coefficient()) / (10 ** self.scale())
968+
var result = Float64(self.coefficient()) / (Float64(10) ** self.scale())
971969
result = -result if self.is_negative() else result
972970

973971
return result

src/decimojo/exponential.mojo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# ===----------------------------------------------------------------------=== #
2-
#
32
# DeciMojo: A fixed-point decimal arithmetic library in Mojo
43
# https://github.com/forFudan/DeciMojo
54
#
@@ -16,7 +15,6 @@
1615
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1716
# See the License for the specific language governing permissions and
1817
# limitations under the License.
19-
#
2018
# ===----------------------------------------------------------------------=== #
2119
#
2220
# Implements exponential functions for the Decimal type

src/decimojo/prelude.mojo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# ===----------------------------------------------------------------------=== #
2-
#
32
# DeciMojo: A fixed-point decimal arithmetic library in Mojo
43
# https://github.com/forFudan/DeciMojo
54
#
@@ -16,7 +15,6 @@
1615
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1716
# See the License for the specific language governing permissions and
1817
# limitations under the License.
19-
#
2018
# ===----------------------------------------------------------------------=== #
2119

2220
"""

src/decimojo/rounding.mojo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# ===----------------------------------------------------------------------=== #
2-
#
32
# DeciMojo: A fixed-point decimal arithmetic library in Mojo
43
# https://github.com/forFudan/DeciMojo
54
#
@@ -16,7 +15,6 @@
1615
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1716
# See the License for the specific language governing permissions and
1817
# limitations under the License.
19-
#
2018
# ===----------------------------------------------------------------------=== #
2119
#
2220
# Implements basic object methods for the Decimal type

src/decimojo/rounding_mode.mojo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# ===----------------------------------------------------------------------=== #
2-
#
32
# DeciMojo: A fixed-point decimal arithmetic library in Mojo
43
# https://github.com/forFudan/DeciMojo
54
#
@@ -16,7 +15,6 @@
1615
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1716
# See the License for the specific language governing permissions and
1817
# limitations under the License.
19-
#
2018
# ===----------------------------------------------------------------------=== #
2119

2220

0 commit comments

Comments
 (0)