Skip to content

Commit 3fd4a46

Browse files
committed
feat: implement MoonBit Ryu number-to-string algorithm
1 parent 0d47965 commit 3fd4a46

File tree

2 files changed

+33
-46
lines changed

2 files changed

+33
-46
lines changed

double/internal/ryu/common.mbt

Lines changed: 0 additions & 46 deletions
This file was deleted.

double/internal/ryu/ryu.mbt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
16+
17+
///|
18+
fn pow5bits(e : Int) -> Int {
19+
((e * 1217359).reinterpret_as_uint() >> 19).reinterpret_as_int() + 1
20+
}
21+
22+
///|
23+
fn copy_special_str(sign : Bool, exponent : Bool, mantissa : Bool) -> String {
24+
if mantissa {
25+
return "NaN"
26+
}
27+
let s = if sign { "-" } else { "" }
28+
if exponent {
29+
return s + "Infinity"
30+
}
31+
return s + "0.0"
32+
}
33+
34+
// Returns floor(log_10(5^e)); requires 0 <= e <= 2620.
35+
36+
///|
37+
fn log10Pow5(e : Int) -> Int {
38+
((e * 732923).reinterpret_as_uint() >> 20).reinterpret_as_int()
39+
}
40+
41+
// Returns floor(log_10(2^e)); requires 0 <= e <= 1650.
42+
43+
///|
44+
fn log10Pow2(e : Int) -> Int {
45+
((e * 78913).reinterpret_as_uint() >> 18).reinterpret_as_int()
46+
}
47+
1548
// MoonBit implementation of Ryu, https://github.com/ulfjack/ryu
1649
// This is a fork of the ryu crate adjusted to comply to the ECMAScript number-to-string algorithm,
1750

0 commit comments

Comments
 (0)