diff --git a/src/ast.rs b/src/ast.rs index 17276093..378ece56 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -283,7 +283,7 @@ impl Numb { } else if val.is_nan() { format!("+NaN") } else { - format!("{:.7e}", val) + format!("{:?}", val) } } _ => { diff --git a/src/hvm.c b/src/hvm.c index 1562acac..3a4cda49 100644 --- a/src/hvm.c +++ b/src/hvm.c @@ -77,6 +77,10 @@ typedef u32 Numb; // Numb ::= 29-bit (rounded up to u32) #define SWIT 0x7 // Numbers +static const f32 U24_MAX = (f32) (1 << 24) - 1; +static const f32 U24_MIN = 0.0; +static const f32 I24_MAX = (f32) (1 << 23) - 1; +static const f32 I24_MIN = (f32) (i32) ((-1u) << 23); #define TY_SYM 0x00 #define TY_U24 0x01 #define TY_I24 0x02 @@ -469,7 +473,7 @@ static inline Numb cast(Numb a, Numb b) { if (isnan(val)) { return new_u24(0); } - return new_u24((u32) clamp(val, 0.0, 16777215)); + return new_u24((u32) clamp(val, U24_MIN, U24_MAX)); } if (get_sym(a) == TY_I24 && get_typ(b) == TY_U24) { @@ -483,7 +487,7 @@ static inline Numb cast(Numb a, Numb b) { if (isnan(val)) { return new_i24(0); } - return new_i24((i32) clamp(val, -8388608.0, 8388607.0)); + return new_i24((i32) clamp(val, I24_MIN, I24_MAX)); } if (get_sym(a) == TY_F24 && get_typ(b) == TY_U24) return new_f24((f32) get_u24(b)); diff --git a/src/hvm.rs b/src/hvm.rs index c8f0c404..b494822d 100644 --- a/src/hvm.rs +++ b/src/hvm.rs @@ -25,6 +25,10 @@ pub struct APair(pub AtomicU64); // Number pub struct Numb(pub Val); +const U24_MAX : u32 = 1 << 24 - 1; +const U24_MIN : u32 = 0; +const I24_MAX : i32 = 1 << 23 - 1; +const I24_MIN : i32 = -1 << 23; // Tags pub const VAR : Tag = 0x0; // variable @@ -295,11 +299,11 @@ impl Numb { match (a.get_sym(), b.get_typ()) { (TY_U24, TY_U24) => b, (TY_U24, TY_I24) => Self::new_u24(b.get_i24() as u32), - (TY_U24, TY_F24) => Self::new_u24(b.get_f24().clamp(0.0, 16777215.0) as u32), + (TY_U24, TY_F24) => Self::new_u24((b.get_f24() as u32).clamp(U24_MIN, U24_MAX)), (TY_I24, TY_U24) => Self::new_i24(b.get_u24() as i32), (TY_I24, TY_I24) => b, - (TY_I24, TY_F24) => Self::new_i24(b.get_f24().clamp(-8388608.0, 8388607.0) as i32), + (TY_I24, TY_F24) => Self::new_i24((b.get_f24() as i32).clamp(I24_MIN, I24_MAX)), (TY_F24, TY_U24) => Self::new_f24(b.get_u24() as f32), (TY_F24, TY_I24) => Self::new_f24(b.get_i24() as f32),