///|
/// HCL Number internal representation.
///
/// WARNING: This is an internal implementation detail. Use the Number type
/// and its public API instead of matching on N directly.
///
/// ```mbt check
/// test {
/// assert_true(Number::from_int(5).is_pos_int())
/// assert_true(Number::from_int(-5).is_neg_int())
/// assert_true(Number::from_double(1.5).is_float())
/// }
/// ```
pub(all) enum N {
PosInt(UInt64)
/// Always less than zero
NegInt(Int64)
/// Always finite
Float(Double)
}
///|
/// ```mbt check
/// test {
/// let n = Number::from_int(42)
/// assert_eq(n.as_int().unwrap(), 42)
/// }
/// ```
pub struct Number {
n : N
}
///|
/// Coerce two numbers to a common type for binary operations
fn coerce(a : N, b : N) -> (Double, Double) {
match (a, b) {
(PosInt(a), PosInt(b)) => (a.to_double(), b.to_double())
(NegInt(a), NegInt(b)) => (a.to_double(), b.to_double())
(Float(a), Float(b)) => (a, b)
(a, b) => (N::to_f64(a), N::to_f64(b))
}
}
///|
/// Convert N to f64
fn N::to_f64(self : N) -> Double {
match self {
PosInt(n) => n.to_double()
NegInt(n) => n.to_double()
Float(n) => n
}
}
///|
/// Try to convert N to i64
fn N::to_i64(self : N) -> Int64? {
match self {
PosInt(n) =>
if n <= 9223372036854775807UL {
Some(n.reinterpret_as_int64())
} else {
None
}
NegInt(n) => Some(n)
Float(_) => None
}
}
///|
/// Try to convert N to UInt64
fn N::to_u64(self : N) -> UInt64? {
match self {
PosInt(n) => Some(n)
NegInt(_) => None
Float(_) => None
}
}
///|
/// Check if N is a float
fn N::is_f64(self : N) -> Bool {
match self {
Float(_) => true
PosInt(_) | NegInt(_) => false
}
}
///|
/// Check if N is a negative integer (NegInt variant)
fn N::is_neg_int(self : N) -> Bool {
match self {
NegInt(_) => true
PosInt(_) | Float(_) => false
}
}
///|
/// Check if N is a positive integer (PosInt variant)
fn N::is_pos_int(self : N) -> Bool {
match self {
PosInt(_) => true
NegInt(_) | Float(_) => false
}
}
///|
/// Create N from finite f64, converting to int if no fractional part.
/// If value is NaN or Infinity, stores as Float as-is (internal callers
/// like div-by-zero may produce NaN intentionally).
fn N::from_finite_f64(value : Double) -> N {
if value != value || value == 1.0 / 0.0 || value == -(1.0 / 0.0) {
Float(value)
} else {
let int_part = value.floor()
if int_part == value {
if value < 0.0 {
if int_part >= -9223372036854775808.0 {
NegInt(int_part.to_int64())
} else {
Float(value)
}
} else if int_part <= 9223372036854775807.0 {
PosInt(int_part.to_uint64())
} else {
Float(value)
}
} else {
Float(value)
}
}
}
///|
/// Create N from i64
fn N::from_i64(i : Int64) -> N {
if i < 0L {
NegInt(i)
} else {
PosInt(i.reinterpret_as_uint64())
}
}
// Eq for N
///|
impl Eq for N with fn equal(self, rhs) {
match (self, rhs) {
(PosInt(a), PosInt(b)) => a == b
(NegInt(a), NegInt(b)) => a == b
(Float(a), Float(b)) => a == b
(a, b) => {
let (fa, fb) = coerce(a, b)
fa == fb
}
}
}
// Compare for N
///|
impl Compare for N with fn compare(self, rhs) {
match (self, rhs) {
(PosInt(a), PosInt(b)) => a.compare(b)
(NegInt(a), NegInt(b)) => a.compare(b)
(Float(a), Float(b)) => a.compare(b)
(a, b) => {
let (fa, fb) = coerce(a, b)
fa.compare(fb)
}
}
}
///|
extend N with Compare::{compare}
// Neg for N
///|
impl Neg for N with fn neg(self) {
match self {
PosInt(value) =>
if value == 0UL {
PosInt(0UL)
} else if value <= 9223372036854775807UL {
NegInt(-value.reinterpret_as_int64())
} else {
Float(-value.to_double())
}
NegInt(value) =>
if value == -9223372036854775808L {
Float(9223372036854775808.0)
} else {
N::from_i64(-value)
}
Float(value) => Float(-value)
}
}
// Add for N
///|
impl Add for N with fn add(self, rhs) {
match (self, rhs) {
(PosInt(a), PosInt(b)) => PosInt(a + b)
(NegInt(a), NegInt(b)) => NegInt(a + b)
(a, b) => {
let (fa, fb) = coerce(a, b)
N::from_finite_f64(fa + fb)
}
}
}
// Sub for N
///|
impl Sub for N with fn sub(self, rhs) {
match (self, rhs) {
(PosInt(a), PosInt(b)) =>
if b > a {
let mag = b - a
if mag <= 9223372036854775807UL {
NegInt(-mag.reinterpret_as_int64())
} else {
Float(-mag.to_double())
}
} else {
PosInt(a - b)
}
(NegInt(a), NegInt(b)) => N::from_i64(a - b)
(a, b) => {
let (fa, fb) = coerce(a, b)
N::from_finite_f64(fa - fb)
}
}
}
// Mul for N
///|
impl Mul for N with fn mul(self, rhs) {
match (self, rhs) {
(PosInt(a), PosInt(b)) => PosInt(a * b)
(NegInt(a), NegInt(b)) => N::from_i64(a * b)
(a, b) => {
let (fa, fb) = coerce(a, b)
N::from_finite_f64(fa * fb)
}
}
}
// Div for N
///|
impl Div for N with fn div(self, rhs) {
match (self, rhs) {
(PosInt(_a), PosInt(b)) =>
if b == 0UL {
Float(0.0 / 0.0)
} else {
let (fa, fb) = coerce(self, rhs)
N::from_finite_f64(fa / fb)
}
_ => {
let (fa, fb) = coerce(self, rhs)
N::from_finite_f64(fa / fb)
}
}
}
// Mod for N
///|
impl Mod for N with fn mod(self, rhs) {
match (self, rhs) {
(PosInt(a), PosInt(b)) =>
if b == 0UL {
Float(0.0 / 0.0)
} else {
PosInt(a % b)
}
(NegInt(a), NegInt(b)) =>
if b == 0L {
Float(0.0 / 0.0)
} else {
NegInt(a % b)
}
(a, b) => {
let (fa, fb) = coerce(a, b)
N::from_finite_f64(fa % fb)
}
}
}
// Show for N
///|
fn N::to_string(self : N) -> String {
match self {
PosInt(v) => v.to_string()
NegInt(v) => v.to_string()
Float(v) => v.to_string()
}
}
// --- Number public API ---
///|
/// Create a Number from a finite f64. Returns None if f is NaN or infinite.
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_f64(1.5).unwrap().as_float(), 1.5)
/// let nan = 0.0 / 0.0
/// match Number::from_f64(nan) {
/// None => ()
/// Some(_) => panic()
/// }
/// }
/// ```
pub fn Number::from_f64(f : Double) -> Number? {
if f != f || f == 1.0 / 0.0 || f == -(1.0 / 0.0) {
None
} else {
Some({ n: N::from_finite_f64(f) })
}
}
///|
/// Create a Number from f64, assumes finite
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_finite_f64(2.0).as_int().unwrap(), 2)
/// }
/// ```
pub fn Number::from_finite_f64(f : Double) -> Number {
{ n: N::from_finite_f64(f) }
}
///|
/// If Number is an integer, return as Int64
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int64(7L).as_i64().unwrap(), 7L)
/// assert_eq(Number::from_double(1.5).as_i64(), None)
/// }
/// ```
pub fn Number::as_i64(self : Number) -> Int64? {
self.n.to_i64()
}
///|
/// If Number is a positive integer, return as UInt64
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_uint64(7UL).as_u64().unwrap(), 7UL)
/// assert_eq(Number::from_int(-1).as_u64(), None)
/// }
/// ```
pub fn Number::as_u64(self : Number) -> UInt64? {
self.n.to_u64()
}
///|
/// Get Number value as f64 (always succeeds)
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int(3).as_f64(), 3.0)
/// }
/// ```
pub fn Number::as_f64(self : Number) -> Double {
N::to_f64(self.n)
}
///|
/// Check if Number is a float
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_double(1.5).is_f64(), true)
/// assert_eq(Number::from_int(1).is_f64(), false)
/// }
/// ```
pub fn Number::is_f64(self : Number) -> Bool {
N::is_f64(self.n)
}
///|
/// Check if Number is a negative integer
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int(-5).is_neg_int(), true)
/// assert_eq(Number::from_int(5).is_neg_int(), false)
/// }
/// ```
pub fn Number::is_neg_int(self : Number) -> Bool {
N::is_neg_int(self.n)
}
///|
/// Check if Number is a positive integer
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int(5).is_pos_int(), true)
/// assert_eq(Number::from_int(-5).is_pos_int(), false)
/// }
/// ```
pub fn Number::is_pos_int(self : Number) -> Bool {
N::is_pos_int(self.n)
}
///|
/// Check if Number is any integer (PosInt or NegInt)
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int(5).is_int(), true)
/// assert_eq(Number::from_double(1.5).is_int(), false)
/// }
/// ```
pub fn Number::is_int(self : Number) -> Bool {
match self.n {
PosInt(_) | NegInt(_) => true
Float(_) => false
}
}
///|
/// Check if Number is a negative integer (alias for is_neg_int)
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int64(-5L).is_int64(), true)
/// assert_eq(Number::from_int(5).is_int64(), false)
/// }
/// ```
pub fn Number::is_int64(self : Number) -> Bool {
N::is_neg_int(self.n)
}
///|
/// Check if Number is a float (alias for is_f64)
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_double(1.5).is_float(), true)
/// }
/// ```
pub fn Number::is_float(self : Number) -> Bool {
N::is_f64(self.n)
}
///|
/// If Number fits in Int (32-bit), return as Int
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int(42).as_int().unwrap(), 42)
/// assert_eq(Number::from_double(1.5).as_int(), None)
/// }
/// ```
pub fn Number::as_int(self : Number) -> Int? {
match self.n {
PosInt(n) => if n <= 2147483647UL { Some(n.to_int()) } else { None }
NegInt(n) => {
let i = n.to_int()
if i.to_int64() == n {
Some(i)
} else {
None
}
}
Float(_) => None
}
}
///|
/// If Number is an integer, return as Int64 (alias for as_i64)
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int(7).as_int64().unwrap(), 7L)
/// }
/// ```
pub fn Number::as_int64(self : Number) -> Int64? {
self.n.to_i64()
}
///|
/// Get Number value as Double (alias for as_f64)
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int(3).as_float(), 3.0)
/// }
/// ```
pub fn Number::as_float(self : Number) -> Double {
N::to_f64(self.n)
}
///|
/// Create Number from Int
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int(-5).as_int().unwrap(), -5)
/// }
/// ```
pub fn Number::from_int(i : Int) -> Number {
if i < 0 {
{ n: NegInt(i.to_int64()) }
} else {
{ n: PosInt(i.to_uint64()) }
}
}
///|
/// Create Number from Int64
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_int64(-5L).as_int64().unwrap(), -5L)
/// }
/// ```
pub fn Number::from_int64(i : Int64) -> Number {
{ n: N::from_i64(i) }
}
///|
/// Create Number from UInt64
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_uint64(5UL).as_u64().unwrap(), 5UL)
/// }
/// ```
pub fn Number::from_uint64(u : UInt64) -> Number {
{ n: PosInt(u) }
}
///|
/// Create Number from Double
///
/// ```mbt check
/// test {
/// assert_eq(Number::from_double(3.5).as_float(), 3.5)
/// assert_eq(Number::from_double(42.0).as_int().unwrap(), 42)
/// }
/// ```
pub fn Number::from_double(d : Double) -> Number {
{ n: N::from_finite_f64(d) }
}
// Eq for Number
///|
impl Eq for Number with fn equal(self, rhs) {
self.n == rhs.n
}
// Compare for Number
///|
impl Compare for Number with fn compare(self, rhs) {
self.n.compare(rhs.n)
}
///|
extend Number with Compare::{compare}
// Add for Number
///|
impl Add for Number with fn add(self, rhs) {
{ n: self.n + rhs.n }
}
// Sub for Number
///|
impl Sub for Number with fn sub(self, rhs) {
{ n: self.n - rhs.n }
}
// Mul for Number
///|
impl Mul for Number with fn mul(self, rhs) {
{ n: self.n * rhs.n }
}
// Div for Number
///|
impl Div for Number with fn div(self, rhs) {
{ n: self.n / rhs.n }
}
// Mod for Number
///|
impl Mod for Number with fn mod(self, rhs) {
{ n: self.n % rhs.n }
}
// Neg for Number
///|
impl Neg for Number with fn neg(self) {
{ n: -self.n }
}
// Show for Number (removed: Show is deprecated, use Number::to_string / Debug)
///|
fn Number::to_string(self : Number) -> String {
N::to_string(self.n)
}
// Debug for Number
///|
fn Number::_to_debug(self : Number) -> String {
"Number(" + N::to_string(self.n) + ")"
}
// Debug trait for Number
///|
impl @moonbitlang/core/debug.Debug for Number with fn to_repr(self) {
@moonbitlang/core/debug.Repr::integer(N::to_string(self.n))
}