this is really my first time asking here, but I'm facing a problem which I'm sure of I'm missing something simple.
I wrote a C# class library with a function that returns a List>. The function itself works fine if used from a console application that I created to test the DLL.
The ending part of the function is:
/* Sorto i risultati in base al ranking */
List<KeyValuePair<string, double>> SortedList = new List<KeyValuePair<string, double>>(matchIndexList_rank);
SortedList.Sort(delegate(KeyValuePair<string, double> firstPair,
KeyValuePair<string, double> secondPair)
{
return (-1) * firstPair.Value.CompareTo(secondPair.Value);
}
);
stopWatch.Stop();
response.success = true;
response.executionTime = stopWatch.ElapsedMilliseconds;
response.resultListKVPStrDoub = SortedList;
return response;
Here for example the double part of the first value in the list is 15.5796761265999 (sorry cannot include pictures yet!)
The problem rises if I include the DLL in a ASP.NET MVC Application that uses the function. In this case the double part of the List> is returned without the decimal part.
The corresponding value to the one presented above is returned as 155796761265999.0
Here is a little bit of code where i get the wrong value returned:
searchEngine.Core.search search = new searchEngine.Core.search(HttpContext.Current.Server.MapPath("~/index_" + oIdentity.comapanyId.ToString()));
searchEngine.Core.genericResponse response = search.doSearch(searchStr);
double maxScore = response.resultListKVPStrDoub.Count > 0 ? response.resultListKVPStrDoub[0].Value : 1;
In example maxScore get the 155796761265999.0 value, but also every other value in the List suffers the same problem.
If I inspect the variables from both sides Visual Studio 2013 states that the type is indeed a double.
Both projects are developed on the same machine which also is my test environment. I use .Net Framework 4.0 on both projects and all build parameters seems to be equal on both projects.
I'm sure I'm missing something simple, but just can't get.
I'd appreciate any help.
Thanks
Lorenzo
Hi, problem solved ! (sorry but I can't yet answer by own question before 8 hours from posting)
Thanks to Radu Pascal that put me on the right way: normally I always add a class library to a solution to debug it, but in this case I had it developed for another project and it was working fine so I ignored the obviuos.
Thanks to Marc Gravell, he spotted the problem: even if I forgot it at the base of my search engine there is a file based index. There is where i reed my values from text and there is where I do the reading wrong.
The real mystery is that the same DLL used in the console application used for testing and even on another ASP.NET MVC on the same machine works fine. I solved using double.parse instead of Convert.ToDouble and imposing an invariant culture.
idfDict[idItem] = double.Parse(s.Split('|')[1], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo);
Thanks to all!
Lorenzo
Hi, problem solved !
Thanks to Radu Pascal that put me on the right way: normally I always add a class library to a solution to debug it, but in this case I had it developed for another project and it was working fine so I ignored the obviuos.
Thanks to Marc Gravell, he spotted the problem: even if I forgot it at the base of my search engine there is a file based index. There is where i reed my values from text and there is where I do the reading wrong.
The real mystery is that the same DLL used in the console application used for testing and even on another ASP.NET MVC on the same machine works fine. I solved using double.parse instead of Convert.ToDouble and imposing an invariant culture.
idfDict[idItem] = double.Parse(s.Split('|')[1], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo);
Thanks to all!
Lorenzo
Related
I use Properies.Resources.resx file in my project for alternate values with russian language (Resources.ru.resx), but for some reason class Resources returns only defualt values.
I was strugling for two days with that problem and realised that I can add MyProject\bin\Debug\ru\AssemblyName.resources.dll as reference to my project and so I can use russian values. It resolves my problem for now, but I think that there should be some normal solution to the problem.
//it works with reference, but does not without it
Resources.Culture = System.Globalization.CultureInfo.GetCultureInfo("ru");
TaskDialog.Show("Info", Properties.Resources.IsRoomName);
//returns "Имя комнаты"
As I understand, it should be done by itself but works only with already builded dll.
i'm new here and my English isn't very good, so i'll try to explain as well as possible.
I'm doing a web application in ASP.NET and C# about steganalysis.
I was looking for internet a function that calculates the observed significance level, or p-value in a chi-square test
for my algorithm and I found it in Java:
This is the result of mi search:
chi[block]= chiSquareTest(expectedValues, pod);
chiSquareTest(double[] expected, long[] observed)
Returns the observed significance level, or p-value,
associated with a Chi-square goodness of fit test comparing
the observed frequency counts to those in the expected array.
My question is, Are there any equivalent function in C# that returns the same parameter?
Thank you in advance,
Ana.
The MathNet nugetpackage contains the ChiSquared distribution - you can get the cumulative or inverse cumulative.
ChiSquared c = new ChiSquared(degreesOfFreedom);
return c.CumulativeDistribution(testValue);
I doubt there is an inbuilt function.
You should try looking for a library that contains the function or implement it yourself.
A quick search returned me this
http://www.alglib.net/specialfunctions/distributions/chisquare.php
and
http://www.codeproject.com/Articles/432194/How-to-Calculate-the-Chi-Squared-P-Value
I know this is an old question but I figured I'd post anyway.
The Accord.NET framework has a library and NuGet package Accord.Statistics which has a ChiSquareTest class that can be used in a similar manor as mentioned in your question:
ChiSquareTest chiSquareTest = new ChiSquareTest(observedArray, expectedArray, DOF);
chiSquareTest.PValue; // gets p-value
chiSquareTest.Significant; // true if statistically significant
The only thing is that you'll have to calculate your DOF - degrees of freedom.
I have values greater than 1.97626258336499E-323
I cant use BigInteger also as it handler only integer values
Any help is appreciated
Here is the code that failed also failed with some solution given by some users:
BigValue / (Math.Pow((1 + ret), j));
WHere BigValue is something like 15000.25
ret is -0.99197104212554987
And j will go to around 500-600.
I am not gettting how to use Rational Class for this too
BigRational from the base class library team from Microsoft. It uses big integers to store it as a fraction, but supports all kinds of operators.
When it comes to printing it as a decimal, I think you need to write your own implementation for that. I have one written somewhere for this class, but I'd have to find it.
Here is something that may be useful. I used it a while back with no problem. It is a .Net BigDecimal class, you can download it from codeplex(or just look at the source):
http://bigdecimal.codeplex.com/releases/view/44790
It is written in VB.Net (.Net 4.0), but that shouldn't matter.
An example of its use in C#: http://www.dreamincode.net/forums/blog/217/entry-2522-the-madman-scribblings/
You will have to switch languages to one that has a BigFloat type (e.g. Haskel and Python have native packages) or else find a third party big float library with a C# binding. There was some discussion of such a binding for GNU MP, but it faded away. Maybe you'll write one!
See also this discussion where MS BigRational is discussed. However this is different from BigFloat.
One solution might be to do your problems in log space instead.
your example would become:
exp(log(Number) - log(1-0.9999999) * 400)
Learn how to use logs to work with numbers like these. Yes, you CAN use a big float package, but that is overkill almost always. You can usually get what you need using logs.
I have a new laptop at work and code that worked earlier in the week does not work today.
The code that worked before is, simplified:
while (dr.Read())
{
int i = int.Parse(dr.GetString(1))
}
Now it fails when the database value is 0. Sometimes, but not reliably, this will work instead:
while (dr.Read())
{
int i = Convert.ToInt32(dr["FieldName"]))
}
Am I missing something stupid?
Oddly enough, ReSharper is also having tons of weird errors with the same error message that I am getting with the above code: "input string was not in the correct format." (Starts before I even load a project.)
Any ideas? Anyone having any SP issues? I did try to make sure all my SPs were up-to-date when I got the machine.
EDIT: I understand how to use Try.Parse and error-handling. The code here is simplified. I am reading test cases from a database table. This column has only 0, 1, and 2 values. I have confirmed that. I broke this down putting the database field into a string variable s and then trying int.Parse(s). The code worked earlier this week and the database has not changed. The only thing that has changed is my environment.
To completely simplify the problem, this line of code throws an exception ("input string was not in the correct format"):
int.Parse("0");
EDIT: Thanks to everyone for helping me resolve this issue! The solution was forcing a reset of my language settings.
A possible explanation:
Basically, the problem was the
sPositiveSign value under
HKEY_CURRENT_USER\Control
Panel\International being set to 0,
which means the positive sign is '0'.
Thus, while parsing the "positive sign
0" is being cut off and then the rest
of the string ("") is parsed as a
number, which doesn't work of course.
This also explains why int.Parse("00")
wasn't a problem. Although you can't
set the positive sign to '0' through
the Control Panel, it's still possible
to do it through the registry, causing
problems. No idea how the computer of
the user in the post ended up with
this wrong setting...
Better yet, what is the output of this on your machine:
Console.WriteLine(System.Globalization.NumberFormatInfo.GetInstance(null).PositiveSign);
I'm willing to bet yours prints out a 0... when mine prints out a + sign.
I suggest checking your Control Panel > Regional and Language Options settings... if they appear normal, try changing them to something else than back to whatever language you're using (I'm assuming English).
I think it's generally not considered a good idea to call Convert.ToInt32 for the value reading out of database, what about the value is null, what about the value cannot be parsed. Do you have any exception handling code here.
Make sure the value is not null.
Check the value can be parsed before call Int32.Parse. Consider Int32.TryParse.
consider use a nullable type like int? in this case.
HTH.
Edit:
#Mike's response made me think that is extremely odd behavior and a simple google search yielded this result: int.Parse weird behavior
An empty string would also cause this issue.
You could check for dbnull before parsing, also it is good to validate parsed data.
You could use a default value and TryParse..
int i = -1;
if(!int.TryParse(dr["MyColumn"] as string, out i))
//Uh Oh!
Edit:
I posted this as a comment in #Chris' answer, but if the sql datatype is int then why not just use the GetInt32 method on the DataReater instead of retrieving it as a string and manual parsing it out?
Are you sure it's "0" and not "null"? What exception do you get?
EDIT:
Just out of curiosity, if it is really faulting on int.Parse("0"), can you try int.Parse("0", CultureInfo.InvariantCulture);?
Otherwise, post your query. Any joins?
you should check dr["FieldName"] != DBNull.Value and you should use TryParse if it passes the DBNull test...
if ( dr["FieldName"] != DBNull.Value )
{
int val = 0;
if ( int.TryParse( dr["FieldName"], out val ) )
{
i = val;
}
else
{
i = 0; // or some default value
}
}
I have seen this issue crop up with .NET Double class, parsing from string "0" as well.
Here's the really wacky part: you can get past the issue by using a different user account to run the program, and sometimes if you destroy and re-create the current user account on the machine, it will run fine.
I have yet to track this down, but you might get past it this way at least.
This is way out of left field, but check your localization settings. I had a number of "input string was not in a correct format" when I moved a web site to a Canadian server. The problem was in a DateTime.Parse method, and was fixed by setting the culture to "en-US".
Yes, your situation is different — but hey, you never know.
are you checking for null ?
if(!dr.IsNull("FieldName")){
int i = Convert.ToInt32(dr["FieldName"]))
}
I'm messing around with some windows functions using p/invoke. Occasionally, I get an error code that is not ERROR_SUCCESS (such an odd name).
Is there a way to look these up within the program? Forexample, if I get error 1017. Can I tell the user
The system has attempted to load or
restore a file into the registry, but
the specified file is not in a
registry file format.
(ERROR_NOT_REGISTRY_FILE: 0x3F9)
Instead of
Error Code: 1017
I'm not sure if there's a niifty .NET wrapper, but you could call the FormatMessage API using P/Invoke.
See this answer for how it would normally be called from native code. Though the question refers to grabbing error codes from HRESULTs, the answer also applies for retreiving codes from the regular OS error codes coming from GetLastError/GetLastWin32Error).
EDIT: Thanks Malfist for pointing me to pinvoke.net, which includes alternative, managed API:
using System.ComponentModel;
string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
Console.WriteLine(errorMessage);
You could take the defines from winerror.h at Rensselaer Polytechnic Institute, and put them into an Enum:
public enum Win32ErrorCode : long
{
ERROR_SUCCESS = 0L,
NO_ERROR = 0L,
ERROR_INVALID_FUNCTION = 1L,
ERROR_FILE_NOT_FOUND = 2L,
ERROR_PATH_NOT_FOUND = 3L,
ERROR_TOO_MANY_OPEN_FILES = 4L,
ERROR_ACCESS_DENIED = 5L,
etc.
}
Then if your error code is in a variable error_code you would use :
Enum.GetName(typeof(Win32ErrorCode), error_code);
I landed on this page while in search of a managed alternative to calling FormatMessage through P/Invoke.
As others have said, there is no way to get those capitalized, underscored names, short of looking them up in winerror.h, which I have seen reproduced online in various places where I landed in the course of searching for information about resolving specific status codes. A quick Google search, for winerror.h, itself, uncovered a page, at Rensselaer Polytechnic Instutute, where someone has helpfully extracted the #define statements from it.
Looking at it gave me an idea; I think there may be a way to get there, working from the source code of winerror.h, which I have, as part of the Windows Platform SDK that ships with every recent version of Microsoft Visual Studio.
Right now, I am in the middle of sorting out a pressing issue in the .NET assembly that brought me to this page. Then, I'll see what I can cobble together; this kind of challenge is right up my alley, and somebody threw down a gauntlet.
Yes there's a function that does that but I don't remember what it is. In the mean time, you can use the error lookup tool (Tools->Error Lookup) to see what a particular code means from within Visual Studio.