From: Mark Thom Date: Sat, 19 Mar 2022 01:00:53 +0000 (-0600) Subject: correct float version of sign/1 (#1360) X-Git-Tag: v0.9.1~102 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=c2fc33afec002119f733c77f558e303b3b12f525;p=scryer-prolog.git correct float version of sign/1 (#1360) --- diff --git a/Cargo.toml b/Cargo.toml index 3b0615fc..e74616d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ libc = "0.2.62" modular-bitfield = "0.11.2" nix = "0.15.0" num-rug-adapter = { version = "0.1.6", optional = true } -ordered-float = "2.1.1" +ordered-float = "2.6.0" phf = { version = "0.9", features = ["macros"] } ref_thread_local = "0.0.0" rug = { version = "1.15.0", optional = true } diff --git a/src/forms.rs b/src/forms.rs index a3c2b354..1b61546a 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -627,6 +627,22 @@ impl ArenaFrom for HeapCellValue { } impl Number { + pub(crate) fn sign(&self) -> Number { + match self { + &Number::Float(f) if f == 0.0 => Number::Float(OrderedFloat(0f64)), + &Number::Float(f) => Number::Float(OrderedFloat(f.signum())), + _ => { + if self.is_positive() { + Number::Fixnum(Fixnum::build_with(1)) + } else if self.is_negative() { + Number::Fixnum(Fixnum::build_with(-1)) + } else { + Number::Fixnum(Fixnum::build_with(0)) + } + } + } + } + #[inline] pub(crate) fn is_positive(&self) -> bool { match self { diff --git a/src/machine/arithmetic_ops.rs b/src/machine/arithmetic_ops.rs index 2541f0a1..b4378afd 100644 --- a/src/machine/arithmetic_ops.rs +++ b/src/machine/arithmetic_ops.rs @@ -82,16 +82,6 @@ fn numerical_type_error( }) } -pub(crate) fn sign(n: Number) -> Number { - if n.is_positive() { - Number::Fixnum(Fixnum::build_with(1)) - } else if n.is_negative() { - Number::Fixnum(Fixnum::build_with(-1)) - } else { - Number::Fixnum(Fixnum::build_with(0)) - } -} - fn isize_gcd(n1: isize, n2: isize) -> Option { if n1 == 0 { return n2.checked_abs().map(|n| n as isize); @@ -1278,7 +1268,7 @@ impl MachineState { atom!("\\") => self.interms.push( drop_iter_on_err!(self, iter, bitwise_complement(a1, &mut self.arena)) ), - atom!("sign") => self.interms.push(sign(a1)), + atom!("sign") => self.interms.push(a1.sign()), _ => { let evaluable_stub = functor_stub(name, 1); std::mem::drop(iter); diff --git a/src/machine/dispatch.rs b/src/machine/dispatch.rs index aaa22f18..0a110681 100644 --- a/src/machine/dispatch.rs +++ b/src/machine/dispatch.rs @@ -714,7 +714,7 @@ impl Machine { &Instruction::Sign(ref a1, t) => { let n = try_or_throw!(self.machine_st, self.machine_st.get_number(a1)); - self.machine_st.interms[t - 1] = sign(n); + self.machine_st.interms[t - 1] = n.sign(); self.machine_st.p += 1; } &Instruction::Neg(ref a1, t) => { diff --git a/src/machine/machine_state_impl.rs b/src/machine/machine_state_impl.rs index 8cac8b92..0f128e08 100644 --- a/src/machine/machine_state_impl.rs +++ b/src/machine/machine_state_impl.rs @@ -2594,15 +2594,6 @@ impl MachineState { ) } - /* - pub fn setup_built_in_call(&mut self, ct: BuiltInClauseType) { - self.num_of_args = ct.arity(); - self.b0 = self.b; - - self.p = CodePtr::BuiltInClause(ct, self.p.local()); - } - */ - pub fn deallocate(&mut self) { let e = self.e; let frame = self.stack.index_and_frame(e);