C# Knows Math

More a warning than a problem

Page 1 of 1

7 Replies - 2149 Views - Last Post: 01 January 2008 - 01:35 PM Rate Topic: -----

#1 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

C# Knows Math

Post icon  Posted 28 December 2007 - 07:58 AM

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:
float v = 3.28F;
Debug.WriteLine(string.Format("float={0}\ndouble={1}", v, Convert.ToDouble(v)));



Gets you:
float=3.28
double=3.27999997138977



Um, yeah. :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: C# Knows Math

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: C# Knows Math

Posted 28 December 2007 - 08:09 AM

It used to be a problem with a few Intel processors before.
Hence the joke: " 2 + 2 = 5 for extremely large values of 2 "
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: C# Knows Math

Posted 28 December 2007 - 08:26 AM

That's exactly why I stopped using C# for programming mission critical shuttle systems. ;)

Hhahaha...seriously though, problems such as this have occurred in other languages as well :)
Was This Post Helpful? 0
  • +
  • -

#4 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: C# Knows Math

Posted 29 December 2007 - 07:07 AM

View Postborn2c0de, on 29 Dec, 2007 - 02:09 AM, said:

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. :P
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: C# Knows Math

Posted 31 December 2007 - 11:22 AM

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)
Was This Post Helpful? 0
  • +
  • -

#6 kurent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-December 07

Re: C# Knows Math

Posted 01 January 2008 - 07:17 AM

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:
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: C# Knows Math

Posted 01 January 2008 - 11:59 AM

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.
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: C# Knows Math

Posted 01 January 2008 - 01:35 PM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1