Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul rational types #169

Merged
merged 49 commits into from
Nov 26, 2024
Merged

Overhaul rational types #169

merged 49 commits into from
Nov 26, 2024

Conversation

inakleinbottle
Copy link
Contributor

This PR introduces the multiprecision family of types to the generic type system and brings this subsystem out of experimental phase into main-line builds. This includes mulitprecision integers, rationals, and floats. (See #168.)

Implemented classes and traits for multiprecision rational number arithmetic, comparisons, and conversions. Added extensive unit tests to validate the functionality and included configuration in CMakeLists.
This commit adds methods for in-place addition, subtraction, multiplication, and division of rational numbers using the GMP library. It ensures the methods check for non-null pointers before proceeding with operations to avoid null pointer dereference.
Implemented functions for comparing rational numbers using GMP library. This includes checks for equality, less than, less than or equal to, greater than, and greater than or equal to. Assertions ensure the pointers are not null before performing comparisons.
Implement the RationalNumber class with functions for abs, pow, real, imaginary, and from_rational operations. Utilize GMP library for precise arithmetic and add necessary assertions for debugging.
Restored the switch statement in `get_builtin_trait` to enable retrieval of built-in traits for comparison, arithmetic, and number operations. Added a fallback with `RPY_UNREACHABLE_RETURN(nullptr)` to handle unexpected cases safely.
This change ensures that null bytes at the end of the string buffer are removed after converting the rational number to a string. This prevents potential issues with string display or further processing where null bytes might interfere.
A redundant newline was removed from the buffer initialization section. This change improves the code readability by keeping the formatting neat and consistent. No functional code was affected by this modification.
Implemented IntegerType and IntegerArithmetic classes along with relevant header and source files. Added unit tests for the IntegerType class and integrated them into the build system. Updated rational_type test for consistency.
Added initialization of `integer_type` with `IntegerType::get()` to ensure it is not left as a null pointer. Also included a call to `ignore
Changed the return values in IntegerType's name and id methods to more accurately reflect the class's purpose. 'integer' was renamed to 'MultiPrecisionInteger' and 'api' was renamed to 'apz' to avoid ambiguity.
Changed the last configuration flag from true to false to correct the object initialization settings. This adjustment ensures the proper handling of edge cases during multiprecision integer operations.
Renamed include guards to prevent naming collisions and align with the ROUGHPY_GENERICS_INTERNAL naming convention. This change increases consistency and reduces the risk of conflicts in larger projects.
Introduce unsafe_add_inplace, unsafe_sub_inplace, unsafe_mul_inplace, and a stub for unsafe_div_inplace using GMP. Ensured non-null safety checks and consistent operation behavior.
Introduce IntegerComparison and IntegerNumber classes to handle integer-specific comparison and numeric operations. Updates also include modifications to CMakeLists.txt to include new files in the build process.
This commit introduces explicit constructors for `IntegerArithmetic`, `IntegerComparison`, and `IntegerNumber` within the `IntegerType` class, improving clarity and initialization logic. It also corrects some formatting issues in the related header files.
Enhanced IntegerType parsing by adding a null check and using mpz_set_str for string conversion. Refactored trait retrieval by returning appropriate built-in traits and adding a default unreachable return statement.
Updated the vcpkg baseline and refined dependency listings. Also added new dependencies mpfr (v4.2.1) and updated gmp (v6.3.0#1) to ensure compatibility and performance improvements. Mpir is removed because it did not fix the intermittent bug on Windows.
Introduce a new CMake module to find and configure the MPFR library. Updated main CMakeLists.txt and multiprecision_types to include and link MPFR accordingly.
This commit introduces a new test file, `test_float_type.cpp`, which includes a series of unit tests for the MultiPrecision float type. The tests cover various properties and functionalities, such as identity, reference counting, basic properties, and a range of arithmetic, comparison, and number-like operations.
This commit removes an extra blank line in the `test_integer_type.cpp` file to improve code readability. No functionality or logic in the code has been impacted by this change.
This commit introduces the MPFloatType class, enabling multiprecision floating-point operations within the generics module. The related header and implementation files were added, and necessary CMakeLists.txt adjustments were made to include the new files. Unit tests for MPFloatType have also been integrated into the test suite.
Changed the conversion of the input string to use a temporary `std::string` object before passing it to `mpz_set_str`. This ensures the function works correctly with different string encodings and formats, improving reliability.
Changed the precision parameter in the `MPFloatType` constructor from `size_t` to `int`. This modification includes an added member variable and ensures that 32 bits of precision is sufficient for the intended calculations.
Modified the `float_type` method to accept an `int` for precision instead of `size_t`. This change ensures consistency across different parts of the codebase and avoids potential issues with type mismatches.
Previously, passing a string directly to `mpq_set_str` could lead to unexpected behavior. This change introduces a temporary string variable to ensure proper handling and conversion.
Revised the constructor, member functions, and internal logic of the `MPFloatType` class. Enhanced memory allocation, deallocation, copy/move operations, and parsing methods for better precision handling and error management. Updated return types and added assertions for robustness.
Changed hash initialization to use the mp_size_t of the integer. This ensures a more unique starting hash value and potentially improves collision resistance in hash computations.
Implemented proper resource cleanup in the `MPFloatType::destroy_range` and display functions to ensure proper memory management. Added hash calculation logic to generate hashes based on the significand and exponent of the floating-point value. This enhances both functionality and robustness of MPFloatType operations.
Introduced `FloatArithmetic` class that extends `ArithmeticTrait` and provides inplace arithmetic operations (addition, subtraction, multiplication, division) using MPFR library. This allows for multiprecision floating-point arithmetic within the generics module.
Introduced the FloatComparison class inheriting from ComparisonTrait to handle various comparison operations on multiprecision floating-point numbers using MPFR. Implemented methods for equality, less than, less than or equal to, greater than, and greater than or equal to comparisons. This facilitates robust and precise floating-point arithmetic comparisons within the generics module.
Included float_arithmetic.cpp, float_arithmetic.h, float_comparison.cpp, and float_comparison.h to the project. This update supports floating-point operations and comparisons, enhancing the multiprecision capabilities of the platform.
Introduced FloatNumber class in float_number.h and float_number.cpp to handle multiprecision floating-point operations. Updated CMakeLists.txt to include the new files. This class supports basic arithmetic functions such as sqrt, pow, exp, and log.
Included the necessary header for float type and modified the function to return a new MPFloatType object based on precision input. Changed the parameter type from `size_t` to `int` for consistency.
Introduce arithmetic, comparison, and number traits to MPFloatType, allowing better trait handling and checks. Add a precision limit check to ensure it does not exceed MPFR_PREC_MAX. Implement name and ID retrieval methods to provide clearer type identification.
Ensure function returns a value by adding RPY_UNREACHABLE_RETURN(false) in the default switch case. This addresses potential undefined behavior and improves code safety.
Adjust test expectations to match the correct reference count and check for the presence of the Division operation. These changes ensure the tests accurately reflect the current functionality and object states.
The generics subdirectory is now always included in the build process, regardless of the ROUGHPY_EXPERIMENTAL flag. This change simplifies the CMake configuration by eliminating unnecessary conditional checks.
Moved the implementation details of the `get_type` function to a separate location. This change enhances the readability and maintainability of the code by separating the function declaration from its implementation.
Refactor number parsing logic to handle floating-point and integer types separately using `std::from_chars`. Added a debug assertion to ensure the entire string is parsed correctly.
Simplified the version macro logic by introducing a generic RPY_COMPILER_VERSION macro. This change ensures consistency across different compiler detections and improves readability.
Added conditional inclusion of <sstream> and a workaround for older Clang and AppleClang lacking `std::from_chars` for floating points. Cleaned up white spaces and simplified conditional checks to improve code readability.
Updated the handling of string conversion using istringstream to ensure compatibility with older versions of Clang and AppleClang. This addresses parsing issues with floating point values when from_chars is not available.
Removed conditional compilation directives and updated the logic to streamline type parsing for integral, float, and double values. This improves code readability and maintains compatibility with compilers lacking `std::from_chars` support for floating-point values.
Add the <cstdlib> header for proper functionality and mark caught exceptions as unused by using the RPY_UNUSED macro to prevent compiler warnings for unused variables.
Update RPY_UNREACHABLE_RETURN to return false instead of void to eliminate unreachable return warning in the ComparisonTraitImpl class. This ensures compliance with modern compiler standards and improves code robustness.
Simplify the CMake configurations by merging RoughPy_Generics_BuiltinType and RoughPy_Generics_Multiprecision targets into RoughPy_Platform. This change reduces redundancy and centralizes target management, improving maintainability.
Add the `ROUGHPY_PLATFORM_EXPORT` macro to the `backup_display` function to ensure it is properly exported for external use. This change allows the function to be accessible outside the compilation unit, facilitating integration with other modules or shared libraries.
Updated the FindGMP.cmake module to use pkg_check_modules with IMPORTED_TARGET, ensuring the creation of the GMP::GMP target when the package is found. This streamlined the handling of GMP library properties and reduced the redundancy in code, making it easier to manage and maintain.
@inakleinbottle inakleinbottle merged commit bf732fa into main Nov 26, 2024
3 checks passed
@inakleinbottle inakleinbottle deleted the overhaul-rational-types branch November 26, 2024 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant