Last active
November 17, 2025 01:40
-
-
Save vitaut/aac4a9b7f662c0ed9b73e18e4a80a5a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # Public domain power of 10 table generator for the Schubfach algorithm: | |
| # https://drive.google.com/file/d/1IEeATSVnEE6TkrHlCYNY2GjaraBjOT4f. | |
| # Author: Victor Zverovich | |
| import math | |
| # Range of decimal exponents [K_min, K_max] from the paper. | |
| dec_exp_min = -324 | |
| dec_exp_max = 292 | |
| # Use all 128 bits intead of 126 as in the paper. | |
| num_bits = 128 | |
| # Negate dec_pow_min and dec_pow_max because we need negative powers 10^-k. | |
| for dec_exp in range(-dec_exp_max, -dec_exp_min + 1, 1): | |
| # dec_exp is -k in the paper. | |
| bin_exp = math.floor(dec_exp * math.log2(10)) - (num_bits - 1) | |
| bin_pow = 2**abs(bin_exp) | |
| dec_pow = 10**abs(dec_exp) | |
| if dec_exp < 0: | |
| result = -(-bin_pow // dec_pow) # Round up. | |
| elif bin_exp < 0: | |
| result = dec_pow * bin_pow | |
| else: | |
| result = -(-dec_pow // bin_pow) # Round up. | |
| print(f"{result >> 64:#x}, {result & (2**64 - 1):#018x}, // {dec_exp}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment