A MoonBit implementation of the Ryu algorithm for fast floating-point to string conversion, compliant with ECMAScript number-to-string specification.
Note: This file is copied from https://github.com/moonbitlang/core/tree/main/double/internal/ryu
test "to_string examples" {
inspect(to_string(3.1415926), content="3.1415926")
inspect(to_string(0.0), content="0")
inspect(to_string(1.0 / 0.0), content="Infinity")
inspect(to_string(-1.0 / 0.0), content="-Infinity")
inspect(to_string(0.0 / 0.0), content="NaN")
}test "output_to_logger example" {
let logger = StringBuilder::new(size_hint=60)
output_to_logger(42.0, logger)
inspect(logger.to_string(), content="42")
}test "write_to_arrayview_byte example" {
let buffer : Array[Byte] = Array::make(32, b'\xff')
let bytes_written = write_to_arrayview_byte(123.456, buffer[:])
// buffer now contains the UTF-8 bytes of "123.456"
inspect(bytes_written, content="7")
let result_str = StringBuilder::new()
for i in 0..<bytes_written {
result_str.write_char(buffer[i].to_char())
}
inspect(result_str.to_string(), content="123.456")
}test "basic usage examples" {
// Simple number conversion
inspect(to_string(42.0), content="42")
inspect(to_string(3.14159), content="3.14159")
inspect(to_string(1.23e-10), content="1.23e-10")
// Special values
inspect(to_string(0.0 / 0.0), content="NaN")
inspect(to_string(1.0 / 0.0), content="Infinity")
inspect(to_string(-1.0 / 0.0), content="-Infinity")
}test "scientific notation examples" {
inspect(to_string(1.0e-15), content="1e-15")
inspect(to_string(1.0e+15), content="1000000000000000")
inspect(to_string(1.23456789e+20), content="123456789000000000000")
}test "edge cases" {
// Very small numbers
inspect(to_string(2.2250738585072014e-308), content="2.2250738585072014e-308")
// Very large numbers
inspect(to_string(1.7976931348623157e+308), content="1.7976931348623157e+308")
// Minimum positive value
inspect(to_string(5.0e-324), content="5e-324")
}Install
Download zipA MoonBit implementation of the Ryu algorithm for fast floating-point to string conversion, compliant with ECMAScript number-to-string specification.