|
I'm getting some unpredictable results when I divide two doubles. Here is an example.
If
double[] a = new double[6]{30.0, 3.0, 0.3, 0.03, 0.003, 0.0003};
double[] b = new double[6]{20.0, 2.0, 0.2, 0.02, 0.002, 0.0002};
then according to C# (.NET?)
a[0] / b[0] = 1.5
a[1] / b[1] = 1.5
a[2] / b[2] = 1.4999999999999998
a[3] / b[3] = 1.5
a[4] / b[4] = 1.5
a[5] / b[5] = 1.4999999999999998
Not a big difference in magnitude but my code also involves rounding the results which really throws things off.
Any help with an explanation and/or solution is greatly appreciated.
|
|
|
|
|
ldself wrote:
Any help with an explanation and/or solution is greatly appreciated.
Check out the Double.Epsilon Field[^], it is explained there. Here is a quick example:
double diff = (double)1/3 - (double).33333;
if(Math.Abs(diff) < Double.Epsilon)
Console.WriteLine("They are equal.");
else
Console.WriteLine("They are not equal.");
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi there,
The reason why 0.3/0.2 does not give an exact result is the following :
C# (and every other compiler) represents your variables in binary values.
For example:
2 is represented by 10 in binary which stands for (1*2^1 + 0*2^0)
4 100 (1*2^2 + 0*2^1 + 0*2^0)
30 11110 (1*2^4 + 1*2^3 + 1*2^2 + 1*2^1 + 0*2^0)
and so on
for values like 0.3, the binary value is not exact, because it is impossible to represent it exactly with this notation...
0.3 is 0,01001100110011... but the number never ends.
Since a double value is represented with 64 0 and 1, you loose a little precision in your operations. There is no real way to avoid this unfortunatly.
I hope this helps
|
|
|
|
|
h_migneron wrote:
There is no real way to avoid this unfortunatly
Actually there is. Double.Epsilon is a field that is defined to be the smallest postive double greater than zero. The idea is that if you take the absolute value of the difference between the two values you wish to compare and their result is less than Double.Epsilon , they are considered equal.
- Nick Parker My Blog | My Articles
|
|
|
|
|
A question for C# language experts:
If I have a struct that implements IDisposable:
struct C : IDisposable
{
public int clan;
public void Dispose()
{
Console.WriteLine("Disposing");
}
}
And later in the code something like:
using (C s = new C())
{
s.clan = 1;
}
A compiler error is reported:
error CS0131: The left-hand side of an assignment must be a variable, property or indexer
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
The problem lies with the fact that structs cant be safely casted, in this case IDisposable. The compiler will typical generate the following for the 'using' block:
IDisposable o = new C();
try
{
C s = o as C;
}
finally
{
o.Dispose();
}
Anyways, my best guess :p
top secret Download xacc-ide 0.0.3 now! See some screenshots
|
|
|
|
|
Hmmmm, but using as with structs causes
error CS0077: The as operator must be used with a reference type ('sharptest.C' is a value type)
And my code complains about assigning a value to a member of s.
Also, this compiles (and works) fine:
using (C s = new C())
{
Console.WriteLine(s.clan.ToString());
}
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
If U have the struct in the same namespace as ur code
the code below works.
C c = new C();
c.clan = 1;
Console.WriteLine(c.clan.ToString());
and if in different namespace -- in that case using can follow the initial using block. I mean immediately after
using System;
using System.Data;
....
using NameSpaceName.C;
C c = new C();
c.clan = 1;
Console.WriteLine(c.clan.ToString());
I hope this help and that i have not missunderstood ur question.
Ketty
|
|
|
|
|
|
According to the C# language spec[^]:
Local variables declared in a resource-acquisition are read-only, and must include an initializer. A compile-time error occurs if the embedded statement attempts to modify these local variables (by assignment or the ++ and -- operators) or pass them as ref or out parameters.
Wow!
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Structures are allocated on the stack and not on the heap as classes.
Write the following code instead, it does the same trick:
C s = new C();
using (s)
{
s.clan = 1;
}
Ami
|
|
|
|
|
Ami Bar wrote:
Structures are allocated on the stack and not on the heap as classes.
That is correct in this context - I just didn't know that variables declared in using nlock need to be read-only.
BTW, what exactly "read-only" really means in C#? It does not have const methods like C++.
Whatever... Thanks for your answer
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
|
Good day all.
While attempting to use the ListView column Textalign property to 'Center' text with in the first column of my Listbox, it always uses the Left text align variable. I have a three column ListView. The sub items of this List accepts and aligns the text within the other columns 'Centered'.
Thank you in advance for your assistance.
Have a great coding day.
Regards,
Tony D. Abel
|
|
|
|
|
Please check all your columns and there TextAlign property in your listview control. And make sure the value of TextAlign property is 'Center'.
Sreejith Nair
[ My Articles ]
|
|
|
|
|
hi, i've added user control onto my main form. but when i run my program, i realise it eats up a lot of resources. my com becames very lag. what's the problem?
Chris
|
|
|
|
|
What sort of user control that you are using ? i mean what it does ?
Sreejith Nair
[ My Articles ]
|
|
|
|
|
i've this main window form where it contains 4 user control. each of this user control connects to the database. after i added these user control onto my main form and run my program, my com became very lag.
Chris
|
|
|
|
|
Try to create a class which do database communication insted of user controls. And try to get the reference of class which have the implimentation part of data access into your user control.
Sreejith Nair
[ My Articles ]
|
|
|
|
|
ok i try tt. thanks
chris
|
|
|
|
|
hi, i've managed to put the database connection coding into a separate class.
namespace WindowsApplication2
{
public class db1
{
public OleDbConnection conn;
public string display_SQL;
public OleDbCommand display_cmd;
public OleDbDataAdapter display_adapter;
public DataSet display_ds;
public db1()
{
conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source =C:\Documents and Settings\Desktop\WindowsApplication2\bin\Debug\mydbms.mdb";
display_SQL = "SELECT * FROM tblYear";
display_cmd = new System.Data.OleDb.OleDbCommand (display_SQL, conn);
display_adapter = new System.Data.OleDb.OleDbDataAdapter(display_cmd);
display_ds = new System.Data.DataSet();
display_adapter.Fill(display_ds,"tblYear");
}
}
i'm not really good in using separate class.so how do we do these same connection into a function in this same class instead of doing it in the constructor? and how do we call that function from a button?
anw is there a wweb site where they teach about windows classes which may assist more??
Chris
|
|
|
|
|
sorry i was wrong. i don't know how to call the function from a different class if i do a database connection in that function and not in the constructor of the separate class?? is it public void connection(){} or ??
|
|
|
|
|
Handle it on button click.
create an object of ur class
Class DBConnection
{
public void Connect()
{
....do ur code;
}
}
Button1_Click()
{
DbConnection DbConn = new DBConnection();
DBConn.Connect();
}
U can pass and return parameters too to this Connect Function;
I hope I am up to ur expection...
Ketty
|
|
|
|
|
hey thanks a lot. but i've got one question let say i've got this form which has a textbox and dataGrid. then i will create a separate class like wat u taught me. if my SQl statement goes like this: "Select * From Table Where Col1 ='" + textBox1.Text + "'";..... how should i pass in the textBox1.Text value to this connection function??
private void button1_Click(object sender, System.EventArgs e)<br />
{<br />
db1 a = new db1();<br />
dataGrid1.DataSource = ??<br />
dataGrid1.DataMember = ??<br />
}<br />
Class DBConnection<br />
{<br />
public void Connect()<br />
{<br />
string SQL = "Select * From Table Where Col1 ='" + textBox1.Text + "'";
....do ur code;<br />
}<br />
}
Chris
|
|
|
|
|
In that case
private void button1_Click(object sender, System.EventArgs e)
{
db1 a = new db1();
dataGrid1.DataSource = Connect(txtbox1.text);
dataGrid1.DataMember = ??
}
Class DBConnection
{
public DataSet Connect(String TextVal)
{
DataSet ds = new DataSet();
string SQL = "Select * From Table Where Col1 ='" + TextVal + "'"; //??
....do ur code;
Return ds
}
}
Is that fine now
Ketty
|
|
|
|