Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Big value cause an overflow of Truncate #1139

Open
weucode opened this issue Mar 29, 2023 · 0 comments
Open

Big value cause an overflow of Truncate #1139

weucode opened this issue Mar 29, 2023 · 0 comments
Labels
bug Something isn't working needs triage An initial review by a maintainer is needed

Comments

@weucode
Copy link

weucode commented Mar 29, 2023

Describe the bug

When passing a big number to Truncate, it cause an overflow because of System.Convert.ToInt64. The related code is here.

public override Func<double, long> __Body__ => (arg) => System.Convert.ToInt64(System.Math.Truncate(arg));

According to the document, if value>Int64.MaxValue, ToInt64 will throw an OverflowException, so it is suggested that add a check before conversion.

To Reproduce

The reduced test case:

namespace NameSpace {
    open Microsoft.Quantum.Math;
    open Microsoft.Quantum.Intrinsic;


	@EntryPoint()
        operation main() : Unit {
            let var = 9.223372036854775296E+18;
            // 9.2233720368547752959E+18 is a safe value
            let res1 = Truncate(var);
	}
}

The origin test case:

namespace NameSpace {
    open Microsoft.Quantum.Math;
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Convert;

	@EntryPoint()
        operation main() : Unit {
            mutable k = 36;
            let numerator = IntAsDouble(Binom(2 * k, k)) * IntAsDouble(k % 2 == 0 ? -1 | +1);
	}
}

Actual behavior

Unhandled exception. System.OverflowException: Arithmetic operation resulted in an overflow.
---> NISLNameSpace.main on D:\00Files\00QuantumFuzzing\demo1\Program.qs:line 9

Unhandled exception: System.OverflowException: Arithmetic operation resulted in an overflow.
at System.Convert.ToInt64(Double value)
at Microsoft.Quantum.Math.Truncate.Native.<>c.<get___Body__>b__2_0(Double arg)
at Microsoft.Quantum.Simulation.Core.Function2.Apply(I a) at Microsoft.Quantum.Simulation.Core.Function2.Microsoft.Quantum.Simulation.Core.ICallable<I,O>.Apply(I args)
at NISLNameSpace.main.<get___Body__>b__12_0(QVoid in) in D:\00Files\00QuantumFuzzing\demo1\Program.qs:line 9
at Microsoft.Quantum.Simulation.Core.Operation2.Apply(I a) at Microsoft.Quantum.Simulation.Core.Operation2.Apply[GenO](Object args)
at Microsoft.Quantum.Simulation.Common.SimulatorBase.Execute[T,I,O](I
args)
at Microsoft.Quantum.Simulation.Common.SimulatorBase.<>c__DisplayClass68_03.<Run>b__0() at System.Threading.Tasks.Task1.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__272_0(Object obj)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback
callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback
callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
at Microsoft.Quantum.EntryPointDriver.Simulation3.RunSimulator(TIn input, Func1 createSimulator)
at Microsoft.Quantum.EntryPointDriver.Simulation`3.Simulate(IEntryPoint entryPoint, TIn input, DriverSettings settings, String simulator)
at System.CommandLine.Invocation.CommandHandler.GetResultCodeAsync(Object value, InvocationContext context)
at System.CommandLine.Invocation.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<b__20_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass15_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass24_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<b__21_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<b__19_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<b__11_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<b__10_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass13_0.<b__0>d.MoveNext()

System information

operating system : Windows10 & Ubuntu18.04

dotnet version : 7.0.100

QDK : 0.27.258160

@weucode weucode added bug Something isn't working needs triage An initial review by a maintainer is needed labels Mar 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs triage An initial review by a maintainer is needed
Projects
None yet
Development

No branches or pull requests

1 participant