Fixed missing functionality in dashu with their methods still has some issue with move
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::parser::ast::*;
|
||||
use crate::parser::parser::*;
|
||||
|
||||
use dashu::integer::UBig;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use crate::arena::*;
|
||||
@@ -28,6 +29,8 @@ use crate::parser::dashu::Integer;
|
||||
use crate::parser::rug::rand::RandState;
|
||||
use crate::read::*;
|
||||
use crate::types::*;
|
||||
use rand::{Rng, SeedableRng};
|
||||
use rand::rngs::StdRng;
|
||||
|
||||
use ordered_float::OrderedFloat;
|
||||
|
||||
@@ -36,10 +39,12 @@ use indexmap::IndexSet;
|
||||
|
||||
use ref_thread_local::{RefThreadLocal, ref_thread_local};
|
||||
|
||||
use std::borrow::BorrowMut;
|
||||
use std::cell::Cell;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::BTreeSet;
|
||||
use std::convert::{TryFrom, Infallible};
|
||||
use std::convert::Infallible;
|
||||
use std::convert::TryFrom;
|
||||
use std::env;
|
||||
use std::ffi::CString;
|
||||
use std::fs;
|
||||
@@ -2727,7 +2732,7 @@ impl Machine {
|
||||
// n has already been confirmed as an integer, and
|
||||
// internally, Rational is assumed reduced, so its denominator
|
||||
// must be 1.
|
||||
r.numer().to_string()
|
||||
r.numerator().to_string()
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
@@ -2756,7 +2761,7 @@ impl Machine {
|
||||
// n has already been confirmed as an integer, and
|
||||
// internally, Rational is assumed reduced, so its
|
||||
// denominator must be 1.
|
||||
r.numer().to_string()
|
||||
r.numerator().to_string()
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
@@ -4103,10 +4108,25 @@ impl Machine {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn maybe(&mut self) {
|
||||
pub(crate) fn maybe(&mut self) {
|
||||
fn generate_random_bits(num_bits: usize) -> u64 {
|
||||
let mut rng = rand::thread_rng();
|
||||
let rand = rng.borrow_mut();
|
||||
let mut random_bits: u64 = 0;
|
||||
|
||||
for _ in 0..num_bits {
|
||||
random_bits <<= 1;
|
||||
|
||||
if rand.gen_bool(0.5) {
|
||||
random_bits |= 1;
|
||||
}
|
||||
}
|
||||
|
||||
random_bits
|
||||
}
|
||||
|
||||
let result = {
|
||||
let mut rand = RANDOM_STATE.borrow_mut();
|
||||
rand.bits(1) == 0
|
||||
generate_random_bits(1) == 0
|
||||
};
|
||||
|
||||
self.machine_st.fail = result;
|
||||
@@ -5189,7 +5209,7 @@ impl Machine {
|
||||
// n has already been confirmed as an integer, and
|
||||
// internally, Rational is assumed reduced, so its
|
||||
// denominator must be 1.
|
||||
r.numer().to_i32().unwrap()
|
||||
r.numerator().to_i32().unwrap()
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
@@ -5804,12 +5824,20 @@ impl Machine {
|
||||
#[inline(always)]
|
||||
pub(crate) fn set_seed(&mut self) {
|
||||
let seed = self.deref_register(1);
|
||||
let mut rand = RANDOM_STATE.borrow_mut();
|
||||
|
||||
|
||||
match Number::try_from(seed) {
|
||||
Ok(Number::Fixnum(n)) => rand.seed(&Integer::from(n)),
|
||||
Ok(Number::Integer(n)) => rand.seed(&*n),
|
||||
Ok(Number::Rational(n)) if n.denom() == &1 => rand.seed(n.numer()),
|
||||
Ok(Number::Fixnum(n)) => {
|
||||
let _: StdRng = SeedableRng::seed_from_u64(Integer::from(n).to_u64().unwrap());
|
||||
},
|
||||
Ok(Number::Integer(n)) => {
|
||||
let _: StdRng = SeedableRng::seed_from_u64(n.to_u64().unwrap());
|
||||
},
|
||||
Ok(Number::Rational(n)) => {
|
||||
if n.denominator() == &UBig::from(1 as u32) {
|
||||
let _: StdRng = SeedableRng::seed_from_u64(n.numerator().to_u64().unwrap());
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
self.machine_st.fail = true;
|
||||
}
|
||||
@@ -5823,7 +5851,7 @@ impl Machine {
|
||||
let time = match Number::try_from(time) {
|
||||
Ok(Number::Float(n)) => n.into_inner(),
|
||||
Ok(Number::Fixnum(n)) => n.get_num() as f64,
|
||||
Ok(Number::Integer(n)) => n.to_f64(),
|
||||
Ok(Number::Integer(n)) => n.to_f64().value(),
|
||||
_ => {
|
||||
unreachable!()
|
||||
}
|
||||
@@ -7469,7 +7497,7 @@ impl Machine {
|
||||
Number::Fixnum(Fixnum::build_with(n.get_num().count_ones() as i64))
|
||||
}
|
||||
Ok(Number::Integer(n)) => {
|
||||
Number::arena_from(n.count_ones().unwrap(), &mut self.machine_st.arena)
|
||||
Number::arena_from(n.count_ones(), &mut self.machine_st.arena)
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
|
||||
Reference in New Issue
Block a user