|
in addition to Dan's link, did you know you can declare a float as a literal by suffixing with an 'f' and save the expense of casting from a double (or int).
float a = 93.3f;
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I try to make it short:
If you compute float numbers, the computer uses "MachineNumbers" which are to a certain level adequate. So the computer has internal numbers which are taken when given a float to compute.
e.g the Machinenumbers are
0.1,
0.2,
0.3,
0.4,
0.5,
1,
1.2,
1.5,
2,
2.4,
2.5
now you pass the number 1.2 -> computer takes 1.2 because it exists
then you take 1.3 -> computer takes again 1.2 because it is the nearest "MachineNumber"
you summ them and allthough there would be a "MachineNumber" 2.5
the computer computes 1.2 + 1.2 and you get the nearest "MachineNumber" which is 2.4
That's been very simplified, but I hope that helps
|
|
|
|
|
Thanks to everyone for answers!
|
|
|
|
|
If you want to do floating point math, I've found that using the decimal type is more precise. Try it this way:
decimal a = 93.3M;
decimal b = 2.3M;
decimal c = a + b;
You should end up with precisely 95.6.
If you need to, you can follow that up by casting the result to a float or double as need be.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
The caveat to using the decimal type is that unlike floats it's not implemented in hardware directly and is significantly slower to use as result. This isn't a problem if you're doing light computations, but if you're doing larger amounts of calculation working within the limits of native floating points may be needed.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
To further and agree with your statement, decimal should only be used for financial or monetary values in .NET. At least that is my rule of thumb.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.
|
|
|
|
|
To really jump in on the good vibes agreeance bandwagon, if the 96.1000001 is annoying you, then you really should be using an appropriate format string when you display it to the user.
|
|
|
|
|
Additionally to what the others already said, it's not safe to compare two float values like this:
if(float1 == float2)
Instead, for floats you should rather use something like this:
if((float1 - float2) < delta)
where delta can be a small number like 0.001.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hello!
I'm currently fiddling around with pixel drawing on Windows Forms using VC# Express, and I'm crashing into this problem.
I want to try and set up Bitmap which is configured to 8-bit colours and make use of a ColorPalette object, but I'm having slight trouble how to set this up, as I'm quite new to C# compared to C/C++.
Here is my current code:
<< in the namespace/class: >>
Rectangle pixRect;
BitmapData pixData;
Bitmap pixBitmap;
Color[] myCols;
ColorPalette myPal;
<< in the form constructor: >>
unsafe
{
myPal = new ColorPalette();
pixBitmap = new Bitmap(50, 20, PixelFormat.Format8bppIndexed);
pixBitmap.Palette = myPal;
myCols = myPal.Entries;
pictureBox1.Image = pixBitmap;
pixRect = new Rectangle(0, 0, pixBitmap.Width, pixBitmap.Height);
myCols[0] = Color.FromArgb(255, 0, 255);
}
I can actually tell that "myPal" ColorPalette is no assigned, occording to a compile warning message, but there doesn't seem to be a ColorPalette constructor.
Any suggestions ?
Thanks in advance,
OCrowley.
|
|
|
|
|
OCrowley wrote: Any suggestions ?
Not sure. Do you have something against reading the documentation?[^]
led mike
|
|
|
|
|
Well, I was expecting a more informative answer than just a LINK TO ANOTHER MSDN PAGE, but yes I have read that and yes I find it misleading that it doesn't show an example of how to set one up, hense why I asked here. kthnx.
Anyone else? and please, don't bite my head off.
|
|
|
|
|
OCrowley wrote: Well, I was expecting a more informative answer
You were expecting? Really? Wow, perhaps a more informative initial post might garner that. Perhaps the information that you only provide in your second post should have been in the first.
OCrowley wrote: I have read that and yes I find it misleading that it doesn't show an example of how to set one up,
Expecting? Yeah, exactly why I have given up on any expectation of "Personal Responsibility".
led mike
|
|
|
|
|
You may not like led mike's link, but the very first sentence states:
"You are not allowed to construct a ColorPalette object directly."
You can get the Bitmap's ColorPalette using its Image.Palette Property,
also mentioned at the link.
pixBitmap = new Bitmap(50, 20, PixelFormat.Format8bppIndexed);
myPal = pixBitmap.Palette;
...
Mark Salsbery
Microsoft MVP - Visual C++
modified on Thursday, October 9, 2008 12:56 PM
|
|
|
|
|
Hi.
I know it was nearly 4 years ago when I wrote this topic, but I just want to apologise for being rude in the last post. I was young and ignorant back then. MSDN is a great collection and I shouldn't insult the work put into it... although I still think it should have more example code. I sorted the C# problem out since btw.
No hard feelings.
OCrowley
|
|
|
|
|
Hai all,
I created an exe that take a reference of Excel 2007,(C# 2005). My problem is when i am trying to run the exe from another machine(that machine uses Excel 2003) it will produce an exception like ' Interop.Excel ...dll' not found.. How to solve this?
Thanks in Advance
Praji..
|
|
|
|
|
|
If you know we have a thing that is named Accelerator in Visual C++ for working with shortcut and keyboard
do we have any things like this in C#.
if no; Please help me to impelement like this
Hello Friends
|
|
|
|
|
Laji59 wrote: If you know we have a thing that is named Accelerator in Visual C++ for working with shortcut and keyboard
do we have any things like this in C#.
Are you kidding? [^]
Good luck, you're going to need it.
led mike
|
|
|
|
|
Well, I don't no anything about Accelerator! What do you want to do In C#?
While (true) { Human.isLearnable = true; }
|
|
|
|
|
I want to manage some Keys like "Alt+F4" ( for example for exit)
but I don`t want use "KeyDown" event of controls
can I do it in any way ?
Hello Friends
|
|
|
|
|
If this is WinForms, set the form's KeyPreview property to true and handle the form's various key events.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
When you turn on KeyPreview, you can manage all events occurring by keyboard in Form_KeyDown:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.Q)
Application.Exit();
}
While (true) { Human.isLearnable = true; }
|
|
|
|
|
I have a VB6 DLL that has a class, ICommonEnv, that is used as an interface definition for another VB6 class, clsCommonEnv, that implements ICommonEnv. When I build the VB6 DLL, in the VS2005 Object viewer I see clsCommonEnv, _clsCommonEnv, __clsCommonEnv, clsCommonEnvClass, ICommonEnv,
_ICommonEnv, and __ICommonEnv. I understand that the "_" is VB6's class interface and "__" is VB6's events inerface.
I want to use an instance of clsCommonEnv in my C# app, and, of course, I referenced the COM DLL and added the "using" statement in my .cs file.
I have not found any documentation that tells me when and where to use which of the interfaces and classes. For example, I also pass this object to C# and other VB6 objects that expect the ICommonEnv interface. I have tried this code by trial and error, but I would like to find some documentation that really explains what to use and why.
For example...
private _ICommonEnv m_oCommonEnvCOM;
m_oCommonEnvCOM = new clsCommonEnvClass();
Int32 lRet = someVB6COMObject.Init(ref m_oCommonEnvCOM)
Thanks in advance for those who can help point me to useful documentation.
|
|
|
|
|
|
I appreciate the quick response. The article covers stuff I've read before, and doesn't address the peculiar aspects of VB6 COM objects in C# (or .NET in general). Unfortunately, I've inherited production code that has the poor design of using ICommonEnv as an "interface class" in VB6 from which the clsCommonEnv class implements that interface. Good VB6 OO design is making the clsCommonEnv class (and no ICommonEnv class whatsoever), then having any other classes implement that interface (Implements clsCommonEnv instead of Implements ICommonEnv).
An article like the one you posted that is geared to VB6 COM DLLs, rather than C++ COM DLLs is what I am looking for.
Thanks, again, though for being kind enough to post a quick response.
|
|
|
|