|
How come Math.Floor(0.3 / 0.1) returns 2.0??? I assume it is some sort of rounding issue. How can I fix this?
Edit: This is what I came up with: Math.Floor(Math.Round(0.3 / 0.1, 2)); Is there an easier way?
Thanks
modified on Tuesday, December 15, 2009 1:03 PM
|
|
|
|
|
It's because 0.3/0.1 returns a double, and there are all sorts of rounding problems associated with doubles.
Console.WriteLine(Math.Floor(0.3M / 0.1M)); uses decimal arithmatic, and you get the expected answer.
CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks
|
|
|
|
|
keefb is 100% correct. If you type "0.3 / 0.1" into the immediate window, it spits out 2.9999999999999996... Now, we all know that's pretty much "3", but if you do a Floor(), it rounds down to 2. As keefb said, use decimals and you'll get the right answer.
But if you need to work with doubles, or have other constraints, you could "hack" it by, say, adding 0.0000001 before going through the Floor()... Kind of like the stone-age trick of adding 0.5 before a truncation to simulate rounding up or down to an integer.
|
|
|
|
|
How can I get all the running process list from a remote IPs / LAN and send a request to stop a particular application/process using C#.
You suggestion required.....
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
|
Thanks for your prompt response.. Yes; It will work on windows system.. Thanks once again for the link..
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
I want to get the last element of a dictionary collection. The methods listed in the IDE for the class seem pretty sparse, looking online led me to this:
http://msdn.microsoft.com/en-us/library/ckzcawb8.aspx[^]
However these are listed as extension methods, and don't appear available to me in .Net 2.0. Is this in fact the case? If it is, is there no other way than to manually loop till the last element? Isn't that a little absurd? Or am I missing something obvious here?
As others have mentioned I can use SortedDictionary and dictionary.Values[dictionary.Count - 1]. Thanks all!
modified on Tuesday, December 15, 2009 10:50 AM
|
|
|
|
|
Just use dictionary.Values[dictionary.Count - 1]; .
|
|
|
|
|
Jim Crafton wrote: However these are listed as extension methods, and don't appear available to me in .Net 2.0. Is this in fact the case?
Yes.
Jim Crafton wrote: I want to get the last element of a dictionary collection
Dictionary collections are unsorted, just pairing a value with a unique key, there is a SortedDictionary collection(analogous to a Collection and a List) which sorts using a comparer (defaults on Key)
In the either case in .net 2.0, you'd need to iterate round and find the "Last" item, though what last is is underfined as it could be key, last added etc.
CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks
|
|
|
|
|
Treat it as an array -
dict.Values[dict.Count - 1]
Whoops, was beaten to it while typing. Must type faster
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
modified on Tuesday, December 15, 2009 11:07 AM
|
|
|
|
|
Doh! I'm an idiot for missing that, thanks. I didn't realize you could do that.
|
|
|
|
|
Dictionary will not guarantee the order of contents. So an item inserted at the last won't be the last element of dictionary. So it doesn't make sense to get the last item.
Best wishes,
Navaneeth
|
|
|
|
|
Correct. The question is invalid.
|
|
|
|
|
Hi Jim,
I'm late to the party here, but certainly agree with the consensus that a dictionary by its nature ain't got no "last."
I'll have to try the "convert to Array" thing, though, out of curiousity to see what it does internally.
While you can definitely say a 'SortedDictionary has a "last" : that may not be at all what you are looking for here which I assume, is not "last" in the sense of "last in the sort order."
I can see two strategies :
1. switch to some kind of queue or stack where you know "who's on first" ... and last ? ... from the get go
2. define a class that inherits from Dictionary, and overrides the Add method and then keep an internal variable updated with the last key (or key/value pair) added.
Here's a quick idea for scenario 2 :
public class FirstLastDictionary<K, V> : Dictionary<K, V>
{
public K firstKeyAdded { get; set; }
public K lastKeyAdded { get; set; }
public void Add(K theKey, V theValue)
{
if(firstKeyAdded == null) firstKeyAdded = theKey;
lastKeyAdded = theKey;
base.Add(theKey, theValue);
}
}
and a quick and dirty test : assumes you have a button named 'button1 on a form that calls this event handler :
private void button1_Click(object sender, EventArgs e)
{
FirstLastDictionary<string, int> myFLdict = new FirstLastDictionary<string, int>();
Random r = new Random();
for (int i = 0; i < 100; i++)
{
int rndValue = r.Next();
myFLdict.Add("key " + rndValue.ToString(), rndValue);
Console.WriteLine("adding : " + rndValue);
}
Console.WriteLine("firstkeyadded = " + myFLdict.firstKeyAdded);
Console.WriteLine("firstkeyadded value = " + myFLdict[myFLdict.firstKeyAdded]);
Console.WriteLine("lastkeyadded = " + myFLdict.lastKeyAdded);
Console.WriteLine("lastkeyadded value = " + myFLdict[myFLdict.lastKeyAdded]);
}
best, Bill
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844
modified on Tuesday, December 15, 2009 4:52 PM
|
|
|
|
|
OK I think I must be prejudiced coming from a C++/STL background where you do have associative arrays and iterators that can give a first and last elements. I always assumed this was standard, and figured .net would have the same thing.
|
|
|
|
|
Hey folks,
I'm trying to write a video (multimedia) application. What I want is two video objects running video clips. Once the first one stops, the other fades in and than back.
I want to render the video images on a surface, but I'm having problems converting the video image to a Texture.
I Have an IVMRWindowlessControl9 (called windowlessCtrl) which is (obviously) my Video Mixing Renderer. Using the GetCurrentImage function, I'll be able to get a pointer to the current video image in memory.
windowlessCtrl.GetCurrentImage(out currentImage);
Now I need to convert the currentImage pointer (IntPtr) to a texture in order to render the image. Can anybody help me?
Thanks!
|
|
|
|
|
1st Check that the currentImage is not null
currentImage != IntPtr.Zero
and then
BitmapInfoHeader bih = (BitmapInfoHeader)Marshal.PtrToStructure(currentImage , typeof(BitmapInfoHeader));
take care it depends of your pin, insure that your media type is a set to video and the output format is a video type.
|
|
|
|
|
|
Hi Everyone,
If anyone has even a remote idea of what might be wrong, please let me know so that we can test it.
We have written a Workflow service in C#, generated and then installed it. Problem is, it is not starting on the server. It starts up on my Dev machine fine, and on 2 other Dev pc's, but as soon as we install it to the server, I get the following error.
"error 1053:the service did not respond to the start or control request in a timely fashion"
I tried on my dev with the same service account used on the service, which didn't start until i added the account to the Administrator group. then it worked on my local dev. When adding the account to the server Admin group, still getting the same error...
Anyone know anything we can try to get this up and running? Are there specific framework installs we need to add perhaps for workflow services? I literally can't think of anything else to try.
Thanks,
Yoken
PS. My Dev machine runs : Win7, VS2008 | the server runs WinServ2003, all .net frameworks installed (1,2,3+)
|
|
|
|
|
|
thanks for the input Migounette,
unfortunately those links didn't help much. The patch is allready loaded yes, and the remote debug only works once the service has started... which is the problem I'm having.
I'll post any new developments related to this issue.
|
|
|
|
|
Hi All,
Just to let you know, after how long of searching for an answer, in the end the problem was inside the code on initializing a public variable.
instead of having [very rough]
public class myclass
{
workflowservicehost host = new workflowservicehost();
public myclass(...) {}
}
I had to change it to
public class myclass
{
workflowservicehost host;
public myclass(...)
{
host = new workflowservicehost();
}
}
Doing this fixed the "Error 1053", and the service started without hassle!
Now, if only I can find out WHY that worked on my dev machine (Win7, VS2008) and not when deployed to the server (WinServ2003), it sure would be great! My only thought is something to do with the underlying infrastructure, but any guess further that that is well.. yet another guess.
|
|
|
|
|
How can I distinguish between unary minus operator and binary minus operator in string format.
For example;
string formula = "3-4"; //Binary minus operator
string formula = "4+-1"; //Unary minus operator
string formula = "(-2)+1"; //Unary minus operator
Thanks...
|
|
|
|
|
A '-' is only binary when it follows a ')' or value, otherwise it's unary
|
|
|
|
|
when you parse an expression (scanning it left-to-right), you are looking for values and operators, and they normally alternate, starting with a value where operator means one of +-*/ and maybe more, and value means one of:
- numeric constant (digits; could also be hex, or real)
- identifier (=variable name)
- expression inside parenthesis
as a result a '-' is a binary minus whenever you are looking for an operator, and a unary minus whenever you are looking for a value.
furthermore you may check what follows the unary minus so you could reject these:
4+--2
4+-a
4+-(1+2)
|
|
|
|
|