Skip to content

Commit

Permalink
Change sqrt of ArbInt to produce an ArbInt no matter the value.
Browse files Browse the repository at this point in the history
  • Loading branch information
ummarikar committed Nov 1, 2019
1 parent 5debb2a commit c832923
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lang_tests/int29.som
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ VM:
Integer
Integer
Double
Double
Integer
"

int29 = (
run = (
(4 sqrt class) println.
((1 << 200) sqrt class) println.
(3 sqrt class) println.
((1 << 103) sqrt class) println.
((1 << 199) sqrt class) println.
)
)
22 changes: 5 additions & 17 deletions src/lib/vm/objects/integers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use std::convert::TryFrom;

use abgc_derive::GcLayout;
use num_bigint::{BigInt, ToBigInt};
use num_traits::{FromPrimitive, ToPrimitive};
use num_bigint::BigInt;
use num_traits::{FromPrimitive, ToPrimitive, Zero};

use crate::vm::{
core::{VMError, VM},
Expand Down Expand Up @@ -156,22 +156,10 @@ impl Obj for ArbInt {
}

fn sqrt(&self, vm: &VM) -> Result<Val, Box<VMError>> {
if let Some(result) = self.val.to_f64() {
if result < 0.0 {
Err(Box::new(VMError::DomainError))
} else {
let root = result.sqrt();
if root.round() == root {
match root.to_bigint() {
Some(i) => ArbInt::new(vm, i),
None => Err(Box::new(VMError::DomainError)),
}
} else {
Ok(Double::new(vm, root))
}
}
if self.val < Zero::zero() {
Err(Box::new(VMError::DomainError))
} else {
Err(Box::new(VMError::CantRepresentAsDouble))
ArbInt::new(vm, self.val.sqrt())
}
}

Expand Down

0 comments on commit c832923

Please sign in to comment.