Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,176 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,993 people online right now. Registration is fast and FREE... Join Now!




C# Knows Math

 
Reply to this topicStart new topic

C# Knows Math, More a warning than a problem

baavgai
28 Dec, 2007 - 06:58 AM
Post #1

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Had some strange results coming out of a database. The problem wasn't in the database, is was in the program.

I've had funky number conversions from Microsoft before. This kind of thing is just too annoying.

This:
CODE

float v = 3.28F;
Debug.WriteLine(string.Format("float={0}\ndouble={1}", v, Convert.ToDouble(v)));


Gets you:
CODE

float=3.28
double=3.27999997138977


Um, yeah. blink.gif
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: C# Knows Math
28 Dec, 2007 - 07:09 AM
Post #2

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
It used to be a problem with a few Intel processors before.
Hence the joke: " 2 + 2 = 5 for extremely large values of 2 "
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: C# Knows Math
28 Dec, 2007 - 07:26 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
That's exactly why I stopped using C# for programming mission critical shuttle systems. wink2.gif

Hhahaha...seriously though, problems such as this have occurred in other languages as well smile.gif
User is offlineProfile CardPM
+Quote Post

RodgerB
RE: C# Knows Math
29 Dec, 2007 - 06:07 AM
Post #4

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,131



Thanked: 16 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
QUOTE(born2c0de @ 29 Dec, 2007 - 02:09 AM) *

It used to be a problem with a few Intel processors before.


It still is a problem though. It must have something to do with the Convert.ToDouble() function, somewhere along the lines it might be trying to round the number and obviously failing at it. tongue.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C# Knows Math
31 Dec, 2007 - 10:22 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
What bugs me about this is not the strange conversion since it follows the rules of significant digits and so should not affect the results of a well written program -- what bugs me is that I can find no documentation on it.

From what I can piece together this oddity is due to a attempt to maintain precision across different platforms. The floats are kept in an 80bit format internally and converted to the platforms native format for manipulation. As the number gets converted to and from this internal format the language can only ensure the number of significant digits, not the exact value. (BUT, at this point I have not come across a solid reference on the point)
User is offlineProfile CardPM
+Quote Post

kurent
RE: C# Knows Math
1 Jan, 2008 - 06:17 AM
Post #6

New D.I.C Head
*

Joined: 27 Dec, 2007
Posts: 8


My Contributions
There is also a bug in Math.round. Something about rounding up and down, can't remember what the problem was exactly. I do remember it isn't mentioned in MSDN help. blink.gif

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: C# Knows Math
1 Jan, 2008 - 10:59 AM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
There is also, and Ill try to find the documentation on it, where sometimes the IsNumeric will return false positives and positive false's, so I always just write my own check, normally using Regular Expressions.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C# Knows Math
1 Jan, 2008 - 12:35 PM
Post #8

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Strictly speaking the conversion is not a *bug* as it is expected behavior. What I feel is wrong is poor documentation on why this is expected behavior. There are numerous warnings in the documentation that equality can not be assumed. I believe the *bug* in the round function is also expected behavior just not what programmers expect.

The round function uses a "bankers round" meaning that it will round up when a number if closer to the ceiling value than the floor value.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:55AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month