HardwareHeaven.com

HardwareHeaven.com

Looking for the skin chooser?
 
 
  • Home

  • Hardware reviews

  • Articles

  • News

  • Tools

  • Gaming at HardwareHeaven

  • Forums

 

Go Back   HardwareHeaven.com > Forums > Software / Tools > Programming, Coding, (Web)Design


Programming, Coding, (Web)Design Discuss all your programming or design needs with likeminded people.

Reply
 
Thread Tools
Old Oct 14, 2003, 05:58 PM   #1
Zeeky H. Bomb
 
brc64's Avatar
 
Join Date: May 2002
Location: Evansville, IN
Posts: 826
Rep Power: 0
brc64 is on a distinguished road

anybody here have Visual Studio .NET?

A programmer friend of mine just got .NET from his school and decided to give it a try. After spending almost an entire day trying to figure out a problem he was having in C++, he finall figured out that apparently float is broken in .NET.

If somebody who has a copy of .NET reads this, could you try this simple program?

Code:
#include <iostream>

using namespace std;
int main(){
   float x = 0;
   x += .02;
   x += .02;
   x += .02;

   if(x < .06)
      cout << "wtf?" << endl << x << endl;
   else
      cout << "works fine" << endl;

   return 0;
}
The problem my friend is running into is that after adding .02 a few times the value ends up being around .05999998. double has the same problem. He tried the exact same code in Visual Studio 6 and it worked correctly. I'm trying to figure out if this is just a strange problem that only he has, or if float and double are actually "broken" in .NET (which seems difficult to believe).

- Me
brc64 is offline   Reply With Quote


Old Oct 15, 2003, 10:42 PM   #2
Yarr... I be blind!
 
Jeff's Avatar
 
Join Date: May 2002
Location: Calgary, Canada
Posts: 3,191
Rep Power: 80
Jeff has much to be proud ofJeff has much to be proud ofJeff has much to be proud ofJeff has much to be proud ofJeff has much to be proud ofJeff has much to be proud ofJeff has much to be proud ofJeff has much to be proud ofJeff has much to be proud of

I get 0.06. However I am using VS.NET2003, not the original (2002).
__________________
Jeff is offline   Reply With Quote
Old Oct 16, 2003, 03:47 AM Threadstarter Thread Starter   #3
Zeeky H. Bomb
 
brc64's Avatar
 
Join Date: May 2002
Location: Evansville, IN
Posts: 826
Rep Power: 0
brc64 is on a distinguished road

He tells me he's using the 2003 version as well. It's a very strange problem. I don't have any idea what could be causing it.

- Me
brc64 is offline   Reply With Quote
Old Oct 16, 2003, 05:27 AM   #4
HardwareHeaven Extreme Member
 
The_Neon_Cowboy's Avatar
 
Join Date: Dec 2002
Location: U.S.A.
Posts: 16,009
Rep Power: 92
The_Neon_Cowboy is a jewel in the roughThe_Neon_Cowboy is a jewel in the roughThe_Neon_Cowboy is a jewel in the rough
System Specs

arg ...olny got Visual Studio 6

thats a wierd problem i'd contact micosoft
__________________
The_Neon_Cowboy is offline   Reply With Quote
Old Oct 20, 2003, 04:34 AM   #5
DriverHeaven Newbie
 
Join Date: Oct 2003
Posts: 2
Rep Power: 0
|-|ahaha is on a distinguished road

It is not a problem with VS .NET it is a problem with all computers! Simply put: the computer cannot represent real numbers (floating point) without an error. Just to be sure I compiled it under dev-c++ (gcc) and it gave the same kind of behaviour. The problem is atenuated by using double precision (double), but it is still there.
|-|ahaha is offline   Reply With Quote
Old Oct 20, 2003, 04:54 PM Threadstarter Thread Starter   #6
Zeeky H. Bomb
 
brc64's Avatar
 
Join Date: May 2002
Location: Evansville, IN
Posts: 826
Rep Power: 0
brc64 is on a distinguished road

Quote:
Originally posted by |-|ahaha
It is not a problem with VS .NET it is a problem with all computers! Simply put: the computer cannot represent real numbers (floating point) without an error. Just to be sure I compiled it under dev-c++ (gcc) and it gave the same kind of behaviour. The problem is atenuated by using double precision (double), but it is still there.
That thought had occured to me, but that doesn't explain why the same program works perfectly fine in Visual Studio 6. Obviously SOMETHING is different between VS6 and VS.NET, but we can't figure out what.

- Me
brc64 is offline   Reply With Quote
Old Oct 20, 2003, 05:20 PM   #7
A Legend in Underwear
 
UberLord's Avatar
 
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0
UberLord will become famous soon enough

Interesting.

GCC 3.3.1 reports
Code:
wtf?
0.06
If it really holds a value other than 0.06 then the cout float is borked or the boolean evaluation of x < 0.06 is borked.

I also suffer similar problems when using reading floats from SQL server and storing them in doubles in VB 6 (vb doesn't know what a float is) so I think that the assumption that computers hate floats is correct
__________________
Gentoo Linux - Developer (baselayout)
Read my blog

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours."
Stephen Roberts
UberLord is offline   Reply With Quote
Old Oct 20, 2003, 10:10 PM   #8
DriverHeaven Addict
 
Join Date: Nov 2002
Location: England
Posts: 320
Rep Power: 0
FluffyChicken is on a distinguished road

A quick serch got me this http://www.codeguru.com/mfc/comments/4387.shtml
(using google and Visual Studio .NET float problem)
Which shows the problem is there, not just with you.

Go here for (hopefuly answers)http://msdn.microsoft.com/support/
or try here as well
Newsgroups: microsoft.public.dotnet.languages.vc
__________________
[COLOR=#455b64]Team mauisun.org
[/COLOR]
FluffyChicken is offline   Reply With Quote
Old Mar 5, 2004, 11:05 AM   #9
DriverHeaven Junior Member
 
Join Date: Jul 2002
Posts: 89
Rep Power: 0
bocybo is on a distinguished road

I know this is an *old* thread, but this is a very commonly come across problem. When comparing a double(or float in this case) to a number, be it 0, 5, or .06 you always need to have an epsilon value to counteract the floating point inaccuracy. So you could do this:

if((x<e+.06)&&(x>.06-e)) say e is like .0001
...

This should fix the incarrucacy. It is definatly not just .net, but has to do with the way that the PC stores floating point digits.
__________________
Barton 2500 @ 11.0x215
Epox 8RDA+
1GB Corsair PC3200
ATI Radeon 9800 Pro @ 420/380
SB Audigy
Lite-on 52x32x52
Maxtor 120GB ATA133
bocybo is offline   Reply With Quote
Reply

Thread Tools