Skip to content

Commit 2cfe1e5

Browse files
Merge cbc8289 into 90961b2
2 parents 90961b2 + cbc8289 commit 2cfe1e5

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

compiler/qsc_qasm/src/stdlib/QasmStd/src/QasmStd/Angle.qs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ function __MultiplyAngleByBigInt__(angle : __Angle__, factor : BigInt) : __Angle
232232
let (value, size) = angle!;
233233
let value : BigInt = Std.Convert.IntAsBigInt(value);
234234
let value = (value * factor) % Std.Convert.IntAsBigInt(1 <<< size);
235-
let value = Std.Convert.BoolArrayAsInt(Std.Convert.BigIntAsBoolArray(value, size));
235+
let value = Std.Convert.BigIntAsInt(value);
236236
new __Angle__ { Value = value, Size = size }
237237
}
238238

library/src/tests/convert.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,37 @@ fn check_bigint_as_bool_array() {
170170
);
171171
}
172172

173+
#[test]
174+
fn check_big_int_as_int_0() {
175+
test_expression("Std.Convert.BigIntAsInt(0L)", &Value::Int(0));
176+
}
177+
178+
#[test]
179+
fn check_big_int_as_int_1() {
180+
test_expression("Std.Convert.BigIntAsInt(1L)", &Value::Int(1));
181+
}
182+
183+
#[test]
184+
fn check_big_int_as_int_n1() {
185+
test_expression("Std.Convert.BigIntAsInt(-1L)", &Value::Int(-1));
186+
}
187+
188+
#[test]
189+
fn check_big_int_as_int_max() {
190+
test_expression(
191+
"Std.Convert.BigIntAsInt(9_223_372_036_854_775_807L)",
192+
&Value::Int(i64::MAX),
193+
);
194+
}
195+
196+
#[test]
197+
fn check_big_int_as_int_min() {
198+
test_expression(
199+
"Std.Convert.BigIntAsInt(-9_223_372_036_854_775_808L)",
200+
&Value::Int(i64::MIN),
201+
);
202+
}
203+
173204
#[test]
174205
fn check_bool_array_as_big_int() {
175206
test_expression(

library/std/src/Std/Convert.qs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,34 @@ function BigIntAsBoolArray(number : BigInt, bits : Int) : Bool[] {
162162
result
163163
}
164164

165+
/// # Summary
166+
/// Converts a BigInt number into Int. Raises an error if the number is too large to fit.
167+
///
168+
/// # Input
169+
/// ## number
170+
/// A BigInt number to be converted.
171+
///
172+
/// # Output
173+
/// Int representation of a number.
174+
function BigIntAsInt(number : BigInt) : Int {
175+
let max = 9_223_372_036_854_775_807L;
176+
let min = -9_223_372_036_854_775_808L;
177+
Fact(number >= min and number <= max, $"`number`=number is too big to fit into Int.");
178+
179+
mutable result = 0;
180+
mutable powL = 1L;
181+
mutable pow = 1;
182+
for _ in 0..63 {
183+
if number &&& powL != 0L {
184+
result |||= pow;
185+
}
186+
powL <<<= 1;
187+
pow <<<= 1;
188+
}
189+
190+
result
191+
}
192+
165193
/// # Summary
166194
/// Produces a non-negative integer from a string of Results in little-endian format.
167195
///
@@ -289,9 +317,10 @@ export
289317
IntAsBoolArray,
290318
BoolArrayAsBigInt,
291319
BigIntAsBoolArray,
320+
BigIntAsInt,
292321
ResultArrayAsInt,
293322
ResultArrayAsBoolArray,
294323
BoolArrayAsResultArray,
295324
ComplexAsComplexPolar,
296325
ComplexPolarAsComplex,
297-
DoubleAsStringWithPrecision;
326+
DoubleAsStringWithPrecision;

0 commit comments

Comments
 (0)