Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
weird problems with float and double : Why 262.4 - 313.83 = -51.4299927 ?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.general.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Matthieu Tudury
Hi, i got a weird problem with decimal numbers,

Here is a test sample :

float a, b, c;
double da, db, dc;

da = 313.83;
db = 262.4;
dc = db -da;

a = (float)da;
b = (float)db;
c = b - a;

Console.WriteLine(c.ToString());
Console.WriteLine(dc.ToString());

DataSet ds = new DataSet();
ds.Tables.Add("titi");
ds.Tables[0].Columns.Add("number",typeof(float));
ds.Tables[0].Columns.Add("number2",typeof(double));
DataRow dr = ds.Tables[0].NewRow();

dr[0] = c;
dr[1] = dc;
ds.Tables[0].Rows.Add(dr);

Console.WriteLine(ds.GetXml());

and the result :

-51,42999
-51,43
<NewDataSet>
<titi>
<number>-51.4299927</number>
<number2>-51.430000000000007</number2>
</titi>
</NewDataSet>

If Someone can help me solve this problem...

Thanks

Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Jon Skeet [C# MVP] (VIP)
Matthieu Tudury <Click here to reveal e-mail address> wrote:
> Hi, i got a weird problem with decimal numbers,

<snip>

I believe the problem is in your understanding of how floating point
numbers work.

See http://www.pobox.com/~skeet/csharp/floatingpoint.html

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Matthieu Tudury
Use decimal...

"Matthieu Tudury" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.Console
System.Data.DataRow
System.Data.DataSet




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734