Skip to content

Commit 6da47a1

Browse files
committed
Merge pull request #1936 from bettio/support-unnormalized-intn
Support unnormalized intn Fix documentation about functions accepting big integers in not-normalized form, and extend intn_to_double to accept them. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 545755d + f03f115 commit 6da47a1

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

src/libAtomVM/intn.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,10 @@ size_t intn_count_digits(const intn_digit_t *num, size_t num_len)
899899
return count;
900900
}
901901

902-
double intn_to_double(const intn_digit_t *num, size_t len, intn_integer_sign_t sign)
902+
double intn_to_double(const intn_digit_t *num, size_t num_len, intn_integer_sign_t sign)
903903
{
904+
size_t len = intn_count_digits(num, num_len);
905+
904906
double acc = 0.0;
905907
double base = ((double) (UINT32_MAX)) + 1;
906908

src/libAtomVM/intn.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,8 @@
7979
* ## Functions Requiring Normalized Input
8080
*
8181
* The following functions MUST receive normalized input for correct operation:
82-
* - \c intn_to_string() - for correct output
83-
* - \c intn_to_double() - for correct conversion
8482
* - \c intn_to_int64() - for correct conversion
8583
* - \c intn_fits_int64() - for accurate check
86-
* - \c intn_to_integer_bytes() - for correct size calculation
87-
* - \c intn_required_unsigned_integer_bytes() - for accurate size
8884
*
8985
* All other functions accept both normalized and non-normalized inputs.
9086
*
@@ -719,9 +715,9 @@ size_t intn_count_digits(const intn_digit_t *num, size_t num_len);
719715
* @return Newly allocated null-terminated string (caller must free)
720716
*
721717
* @pre base >= 2 && base <= 36
722-
* @pre Input must be normalized for correct output
723718
* @post Returned string must be freed by caller
724719
* @note Output format: uppercase letters, no base prefix
720+
* @note Accepts both normalized and non-normalized inputs
725721
*/
726722
char *intn_to_string(const intn_digit_t *num, size_t len, intn_integer_sign_t num_sign, int base,
727723
size_t *string_len);
@@ -763,9 +759,9 @@ int intn_parse(
763759
* @param sign Sign of integer
764760
* @return Double representation
765761
*
766-
* @pre Input must be normalized
767762
* @note Precision loss expected for integers > 53 bits
768763
* @note With current 256-bit limit, result always fits in double range
764+
* @note Accepts both normalized and non-normalized inputs
769765
*/
770766
double intn_to_double(const intn_digit_t *num, size_t len, intn_integer_sign_t sign);
771767

0 commit comments

Comments
 (0)