Skip to content

Commit f350bf0

Browse files
committed
zmij
0 parents  commit f350bf0

File tree

8 files changed

+1142
-0
lines changed

8 files changed

+1142
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/Cargo.lock

Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "zmij"
3+
version = "0.0.1"
4+
authors = ["David Tolnay <dtolnay@gmail.com>"]
5+
categories = ["value-formatting", "no-std", "no-std::no-alloc"]
6+
description = "A double-to-string conversion algorithm based on Schubfach and yy"
7+
documentation = "https://docs.rs/zmij"
8+
edition = "2024"
9+
license = "MIT"
10+
11+
[workspace]
12+
members = ["gen-pow10"]
13+
14+
[package.metadata.docs.rs]
15+
targets = ["x86_64-unknown-linux-gnu"]
16+
rustdoc-args = [
17+
"--generate-link-to-definition",
18+
"--generate-macro-expansion",
19+
"--extern-html-root-url=core=https://doc.rust-lang.org",
20+
]

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Permission is hereby granted, free of charge, to any
2+
person obtaining a copy of this software and associated
3+
documentation files (the "Software"), to deal in the
4+
Software without restriction, including without
5+
limitation the rights to use, copy, modify, merge,
6+
publish, distribute, sublicense, and/or sell copies of
7+
the Software, and to permit persons to whom the Software
8+
is furnished to do so, subject to the following
9+
conditions:
10+
11+
The above copyright notice and this permission notice
12+
shall be included in all copies or substantial portions
13+
of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
18+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
22+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
DEALINGS IN THE SOFTWARE.

gen-pow10/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "gen-pow10"
3+
version = "0.0.0"
4+
edition = "2024"
5+
publish = false
6+
7+
[[bin]]
8+
name = "gen-pow10"
9+
path = "main.rs"
10+
11+
[dependencies]
12+
num-bigint = "0.4"
13+
num-integer = "0.1"

gen-pow10/main.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Power of 10 overestimates generator for the Schubfach algorithm:
2+
// https://fmt.dev/papers/Schubfach4.pdf.
3+
// Copyright (c) 2025 - present, Victor Zverovich
4+
5+
use num_bigint::BigUint as Uint;
6+
use num_integer::Integer as _;
7+
use std::f64::consts::LOG2_10;
8+
9+
fn main() {
10+
// Range of decimal exponents [K_min, K_max] from the paper.
11+
let dec_exp_min = -324_i32;
12+
let dec_exp_max = 292_i32;
13+
14+
let num_bits = 128_i32;
15+
16+
// Negate dec_pow_min and dec_pow_max because we need negative powers 10^-k.
17+
for dec_exp in -dec_exp_max..=-dec_exp_min {
18+
// dec_exp is -k in the paper.
19+
let bin_exp = (f64::from(dec_exp) * LOG2_10).floor() as i32 - (num_bits - 1);
20+
let bin_pow = Uint::from(2_u8).pow(bin_exp.unsigned_abs());
21+
let dec_pow = Uint::from(10_u8).pow(dec_exp.unsigned_abs());
22+
let mut result = if dec_exp < 0 {
23+
bin_pow / dec_pow
24+
} else if bin_exp < 0 {
25+
dec_pow * bin_pow
26+
} else {
27+
dec_pow / bin_pow
28+
};
29+
result.inc();
30+
let hi = &result >> 64;
31+
let lo = result & (Uint::from(2_u8).pow(64) - Uint::from(1_u8));
32+
println!("{{{hi:#x}, {lo:#018x}}}, // {dec_exp:4}");
33+
}
34+
}

0 commit comments

Comments
 (0)