Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions double/README.mbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test "basic operations" {
inspect(Double::trunc(3.7), content="3")

// Pow function
inspect(Double::pow(2.0, 3), content="8")
inspect(@math.pow(2.0, 3), content="8")

// Sign
inspect(Double::signum(-3.14), content="-1")
Expand Down Expand Up @@ -80,13 +80,13 @@ test "binary representation" {
// Convert to big-endian and little-endian bytes
// Different byte orders should produce different results
inspect(
num.to_be_bytes(),
num.to_be_bytes_local(),
content=(
#|b"?\xf0\x00\x00\x00\x00\x00\x00"
),
)
inspect(
num.to_le_bytes(),
num.to_le_bytes_local(),
content=(
#|b"\x00\x00\x00\x00\x00\x00\xf0?"
),
Expand Down
26 changes: 4 additions & 22 deletions double/double.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,8 @@ pub fn Double::is_close(
/// Returns a sequence of 8 bytes representing the double-precision
/// floating-point number in big-endian byte order.
///
/// Example:
///
/// ```moonbit
/// let d = 1.0
/// inspect(
/// d.to_be_bytes(),
/// content=(
/// #|b"?\xf0\x00\x00\x00\x00\x00\x00"
/// ),
/// )
/// ```
#deprecated
#doc(hidden)
pub fn Double::to_be_bytes(self : Double) -> Bytes {
self.reinterpret_as_uint64().to_be_bytes()
}
Expand All @@ -282,17 +273,8 @@ pub fn Double::to_be_bytes(self : Double) -> Bytes {
/// Returns a sequence of 8 bytes representing the double-precision
/// floating-point number in little-endian order.
///
/// Example:
///
/// ```moonbit
/// let d = 1.0
/// inspect(
/// d.to_le_bytes(),
/// content=(
/// #|b"\x00\x00\x00\x00\x00\x00\xf0?"
/// ),
/// )
/// ```
#deprecated
#doc(hidden)
pub fn Double::to_le_bytes(self : Double) -> Bytes {
self.reinterpret_as_uint64().to_le_bytes()
}
20 changes: 18 additions & 2 deletions double/double_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,27 @@ test "panic is_close with invalid relative and absolute tolerances" {
ignore(1.0.is_close(1.0, relative_tolerance=-1.0, absolute_tolerance=-1.0))
}

///|
fn Double::to_le_bytes_local(self : Double) -> Bytes {
// self.reinterpret_as_uint64().to_le_bytes()
let buf = @buffer.new(size_hint=8)
buf.write_uint64_le(self.reinterpret_as_uint64())
return buf.to_bytes()
}

///|
fn Double::to_be_bytes_local(self : Double) -> Bytes {
// self.reinterpret_as_uint64().to_be_bytes()
let buf = @buffer.new(size_hint=8)
buf.write_uint64_be(self.reinterpret_as_uint64())
return buf.to_bytes()
}

///|
test "to_be_bytes with negative number" {
let d = -123.456
inspect(
d.to_be_bytes(),
d.to_be_bytes_local(),
content=(
#|b"\xc0^\xdd/\x1a\x9f\xbew"
),
Expand All @@ -87,7 +103,7 @@ test "to_be_bytes with negative number" {
///|
test "to_le_bytes with infinity" {
inspect(
(1.0 / 0.0).to_le_bytes(),
(1.0 / 0.0).to_le_bytes_local(),
content="b\"\\x00\\x00\\x00\\x00\\x00\\x00\\xf0\\x7f\"",
)
}
Expand Down
46 changes: 0 additions & 46 deletions double/internal/ryu/common.mbt

This file was deleted.

33 changes: 33 additions & 0 deletions double/internal/ryu/ryu.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.

///|
fn pow5bits(e : Int) -> Int {
((e * 1217359).reinterpret_as_uint() >> 19).reinterpret_as_int() + 1
}

///|
fn copy_special_str(sign : Bool, exponent : Bool, mantissa : Bool) -> String {
if mantissa {
return "NaN"
}
let s = if sign { "-" } else { "" }
if exponent {
return s + "Infinity"
}
return s + "0.0"
}

// Returns floor(log_10(5^e)); requires 0 <= e <= 2620.

///|
fn log10Pow5(e : Int) -> Int {
((e * 732923).reinterpret_as_uint() >> 20).reinterpret_as_int()
}

// Returns floor(log_10(2^e)); requires 0 <= e <= 1650.

///|
fn log10Pow2(e : Int) -> Int {
((e * 78913).reinterpret_as_uint() >> 18).reinterpret_as_int()
}

// MoonBit implementation of Ryu, https://github.com/ulfjack/ryu
// This is a fork of the ryu crate adjusted to comply to the ECMAScript number-to-string algorithm,

Expand Down
7 changes: 5 additions & 2 deletions double/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/int64",
"moonbitlang/core/uint64",
"moonbitlang/core/double/internal/ryu"
],
"test-import": ["moonbitlang/core/bytes"],
"test-import": [
"moonbitlang/core/bytes",
"moonbitlang/core/buffer",
"moonbitlang/core/math"
],
"targets": {
"exp_js.mbt": ["js"],
"exp_nonjs.mbt": ["not", "js"],
Expand Down
2 changes: 0 additions & 2 deletions double/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ fn Double::pow(Double, Double) -> Double
#as_free_fn
fn Double::round(Double) -> Double
fn Double::signum(Double) -> Double
fn Double::to_be_bytes(Double) -> Bytes
fn Double::to_le_bytes(Double) -> Bytes
fn Double::to_string(Double) -> String
fn Double::to_uint(Double) -> UInt
#as_free_fn
Expand Down
12 changes: 1 addition & 11 deletions double/pow_nonjs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,8 @@ const POW_ivln2_l = 1.92596299112661746887e-08
///
/// Returns the result of raising `base` to the power of `exponent`.
///
/// Example:
///
/// ```moonbit
/// let x = 2.0
/// inspect(x.pow(3.0), content="8")
/// inspect(x.pow(0.5), content="1.4142135623730951")
/// inspect(x.pow(0.0), content="1")
/// inspect((-1.0).pow(2.0), content="1")
/// inspect(0.0.pow(0.0), content="1")
/// inspect(@double.infinity.pow(-1.0), content="0")
/// ```
#as_free_fn
#deprecated("Use the free function `@math.pow` instead.")
pub fn Double::pow(self : Double, other : Double) -> Double {
fn set_low_word(d : Double, v : UInt) -> Double {
let bits : UInt64 = d.reinterpret_as_uint64()
Expand Down
Loading
Loading