You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrong behavior in float values using IronPython in a C# application #841
We are running a sandbox in a .NET windows application (.NET framework 4.7.2 and IronPython 2.7).
Once we call the execution of the specific script, they returned to us a DivisionByZero exception, because a normal decimal value we are sending from .NET are being converted to a very small number (1.03e-311), that causes the Zero Division issue.
Steps to Reproduce
Create the Runtime and Scope information
Load the Python code to execute (only has several arithmetic operations, nothing very complex neither using arrays or collections)
We receive the exception of the execution in a Try/Catch block in .NET.
We "debug" the code using a direct RaiseError and we identified the runtime is assuming a very small number tending to zero, that is the reason of the fail.
Error returned:
"An error occured while executing the underlying Python script (Traceback (most recent call last): :: File "", line 98, in GetTheFailureMode :: File "", line 699, in GetMaxFfsLoss :: File "", line 2043, in GetMaxHoopLoss :: File "", line 2197, in GetHoopMsop :: ZeroDivisionError: Attempted to divide by zero.)"
Values recovered in Python just calling the method:
"
An error occured while executing the underlying Python script (Traceback (most recent call last): :: File "", line 148, in GetMaxIliFfsLoss :: Exception: (
'WT: 6.95238308496e-310',
'Hybrid', <type 'float'>,
'Float Number: ', 1.034294700449062e-311,
'Float Number String', '0.0095',
'Convert From String To Float', 1.034294700449062e-311,
'Culture Number Format', <System.Globalization.NumberFormatInfo object at 0x000000000000002B [System.Globalization.NumberFormatInfo]>, 1.0343456283479938e-311,
False,
'0.0095',
1.034294700449062e-311,
1.0353302339822869e-311,
1.034294700449062e-311,
1.0334119502000876e-311,
True,
1.0338363493581819e-311,
'X60',
1.0349533679427365e-311))
"
How are you passing the parameters from .NET to Python? It's hard to tell what your doing from the description above. An example to reproduce the issue would be helpful...
How are you passing the parameters from .NET to Python? It's hard to tell what your doing from the description above. An example to reproduce the issue would be helpful...
Hi, thanks for your quick answer. Let me write some example of the code here, limited as by my company's IT policies ( I'm trying to include screenshots but is not possible for some reason)
//Create the RuntimepublicRuntimeProvider(){// set the callback to log errors.varoptions=newDictionary<string,object>();options["Debug"]=false;// Creates execution enginethis.myEngine=Python.CreateEngine(options);// This is for debugging Python code// this.myEngine.SetTrace(this.OnTraceback);this.myScope=this.myEngine.CreateScope();}// Load the scriptpublicvoidLoadScript(stringscriptSource,boolsameScope,boolsourceIsFileName){this.ScriptScope=this.RuntimeProvider.CreateScope();ScriptSourcesource;source=this.RuntimeProvider.Engine.CreateScriptSourceFromString(scriptSource);ScriptSources.Add(source);varcompiled=source.Compile();this.myExecutions.Add(compiled.Execute(this.ScriptScope));}// ProxyprivateTcoAlgorithmProxyLoadAlgorithmInstance(stringalgorithmCode,stringalgorithmDescriptionBuilder){varscriptLoader=newScriptLoader();varpythonInstance=this.LoadAlgorithm(algorithmCode,algorithmDescriptionBuilder,scriptLoader);// Creates the proxy to comunicate the code with the other application domain// This proxy is neccesary because the object that you want to sent to the other domain must intherit from MarshalByRefObjectvaralgorithm=newAlgorithmProxy(pythonInstance,scriptLoader.RuntimeProvider.Engine);returnalgorithm;}// Call functionprotectedoverridedouble?GetValue(IClusterableFeatureanomaly){varffsLoss=this.FfsAssessement.GetMaxIliFfsLoss(anomaly,lineType.Value,inBend.Value,pipeGrade.Value,anomaly.AddTolerance);returnffsLoss.Value;}// Specific function call in pythonpublicdoubleGetMaxIliFfsLoss(stringlineType,doublewallThicknessInMeter,doubledesignFactor,boolinBend,stringpofDefectClass,doublelengthInMeter,doublewidthInMeter,doubledepthPercentage,doubledesignPressureInMpa,stringpipeGrade,doubleouterDiameterInMeter,booladdTolerance){returnthis.GetValue(()=>this.myPythonAlgorithmInstance.GetMaxIliFfsLoss(lineType,wallThicknessInMeter,designFactor,inBend,wallThicknessInMeter.ToString("R"),lengthInMeter,widthInMeter,depthPercentage,designPressureInMpa,pipeGrade,outerDiameterInMeter,addTolerance));
And finally the simplified code in Python that is being executed:
It's interesting that Double.Parse("0.0095") would give you 1.034294700449062e-311. Is the code only failing once in a while or is it consistently incorrect? Unfortunately I haven't been able to reproduce. Which version of IronPython 2.7 and of the DLR are you using?
It's interesting that Double.Parse("0.0095") would give you 1.034294700449062e-311. Is the code only failing once in a while or is it consistently incorrect? Unfortunately I haven't been able to reproduce. Which version of IronPython 2.7 and of the DLR are you using?
As we are iterating over several calls to the python functions, the code works well in the first ones but after that starts to fail and we could never have a full execution of the algorithm.
The specific version of IronPython is 2.7.9. The solution is build in .NET framework 4.7.2. Seems also related with the Microsoft.Scripting 1.2.2.0.
Activity
slozier commentedon Aug 22, 2024
How are you passing the parameters from .NET to Python? It's hard to tell what your doing from the description above. An example to reproduce the issue would be helpful...
jandres861 commentedon Aug 22, 2024
Hi, thanks for your quick answer. Let me write some example of the code here, limited as by my company's IT policies ( I'm trying to include screenshots but is not possible for some reason)
And finally the simplified code in Python that is being executed:
And just using that RaiseError to print in the .NET Exception the values we identified the change to that very small numbers, not the desired ones.
slozier commentedon Aug 26, 2024
It's interesting that
Double.Parse("0.0095")would give you1.034294700449062e-311. Is the code only failing once in a while or is it consistently incorrect? Unfortunately I haven't been able to reproduce. Which version of IronPython 2.7 and of the DLR are you using?jandres861 commentedon Aug 26, 2024
As we are iterating over several calls to the python functions, the code works well in the first ones but after that starts to fail and we could never have a full execution of the algorithm.
The specific version of IronPython is 2.7.9. The solution is build in .NET framework 4.7.2. Seems also related with the Microsoft.Scripting 1.2.2.0.
Thanks!