Describe the bug
Running the following programs, PrepareSparseMultiConfigurationalState leads to an out of memory error. By looking for the reason, it is because the length of excitations is 0. This causes negative infinity when the Lg function is evaluated inside the function. The debug information:

In addition, none of the functions calling truncate (ceiling, floor, round) check the special values such as infinity and NaN, so Truncate (value) is responsible for the out-of-memory error.
It is recommended to add come code to check whether the parameter of the Truncate is valid.
To Reproduce
namespace TEST {
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Chemistry.JordanWigner;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Simulation;
open Microsoft.Quantum.Preparation;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Intrinsic;
@EntryPoint()
operation main() : Unit {
mutable initialStatePreparation = SwapReverseRegister;
mutable excitations = EmptyArray<JordanWignerInputState>();
use qubits = Qubit[1];
mutable APIResult = PrepareSparseMultiConfigurationalState(initialStatePreparation, excitations, qubits);//excitations is an empty array
}
}
Expected behavior
- PrepareSparseMultiConfigurationalState function excitations should consider the situation that
excitations is an empty array.
- Truncate adds validation to the parameter.
Actual behavior

System.OverflowException:“Arithmetic operation resulted in an overflow.”
System information
- OS: [Windows, Ubuntu]
- .NET Core Version: [6.0.300]
- QDK "Microsoft.Quantum.Sdk/0.28.263081"
Additional context
I can be honored to submit a PR fix. :)