Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function __MultiplyAngleByBigInt__(angle : __Angle__, factor : BigInt) : __Angle
let (value, size) = angle!;
let value : BigInt = Std.Convert.IntAsBigInt(value);
let value = (value * factor) % Std.Convert.IntAsBigInt(1 <<< size);
let value = Std.Convert.BoolArrayAsInt(Std.Convert.BigIntAsBoolArray(value, size));
let value = Std.Convert.BigIntAsInt(value);
new __Angle__ { Value = value, Size = size }
}

Expand Down
31 changes: 31 additions & 0 deletions library/src/tests/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,37 @@ fn check_bigint_as_bool_array() {
);
}

#[test]
fn check_big_int_as_int_0() {
test_expression("Std.Convert.BigIntAsInt(0L)", &Value::Int(0));
}

#[test]
fn check_big_int_as_int_1() {
test_expression("Std.Convert.BigIntAsInt(1L)", &Value::Int(1));
}

#[test]
fn check_big_int_as_int_n1() {
test_expression("Std.Convert.BigIntAsInt(-1L)", &Value::Int(-1));
}

#[test]
fn check_big_int_as_int_max() {
test_expression(
"Std.Convert.BigIntAsInt(9_223_372_036_854_775_807L)",
&Value::Int(i64::MAX),
);
}

#[test]
fn check_big_int_as_int_min() {
test_expression(
"Std.Convert.BigIntAsInt(-9_223_372_036_854_775_808L)",
&Value::Int(i64::MIN),
);
}

#[test]
fn check_bool_array_as_big_int() {
test_expression(
Expand Down
31 changes: 30 additions & 1 deletion library/std/src/Std/Convert.qs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,34 @@ function BigIntAsBoolArray(number : BigInt, bits : Int) : Bool[] {
result
}

/// # Summary
/// Converts a BigInt number into Int. Raises an error if the number is too large to fit.
///
/// # Input
/// ## number
/// A BigInt number to be converted.
///
/// # Output
/// Int representation of a number.
function BigIntAsInt(number : BigInt) : Int {
let max = 9_223_372_036_854_775_807L;
let min = -9_223_372_036_854_775_808L;
Fact(number >= min and number <= max, $"`number`=number is too big to fit into Int.");

mutable result = 0;
mutable powL = 1L;
mutable pow = 1;
for _ in 0..63 {
if number &&& powL != 0L {
result |||= pow;
}
powL <<<= 1;
pow <<<= 1;
}

result
}

/// # Summary
/// Produces a non-negative integer from a string of Results in little-endian format.
///
Expand Down Expand Up @@ -289,9 +317,10 @@ export
IntAsBoolArray,
BoolArrayAsBigInt,
BigIntAsBoolArray,
BigIntAsInt,
ResultArrayAsInt,
ResultArrayAsBoolArray,
BoolArrayAsResultArray,
ComplexAsComplexPolar,
ComplexPolarAsComplex,
DoubleAsStringWithPrecision;
DoubleAsStringWithPrecision;
Loading