|
|||||||
| Programming, Coding, (Web)Design Discuss all your programming or design needs with likeminded people. |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Zeeky H. Bomb
Join Date: May 2002
Location: Evansville, IN
Posts: 826
Rep Power: 0 ![]() |
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;
}
- Me |
|
|
|
|
|
#2 |
|
Yarr... I be blind!
Join Date: May 2002
Location: Calgary, Canada
Posts: 3,191
Rep Power: 80 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
I get 0.06. However I am using VS.NET2003, not the original (2002).
__________________
|
|
|
|
|
|
|
|
Zeeky H. Bomb
Join Date: May 2002
Location: Evansville, IN
Posts: 826
Rep Power: 0 ![]() |
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 |
|
|
|
|
|
#4 |
|
HardwareHeaven Extreme Member
|
arg ...olny got Visual Studio 6
thats a wierd problem i'd contact micosoft
__________________
|
|
|
|
|
|
#5 |
|
DriverHeaven Newbie
Join Date: Oct 2003
Posts: 2
Rep Power: 0 ![]() |
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.
|
|
|
|
|
|
|
|
|
Zeeky H. Bomb
Join Date: May 2002
Location: Evansville, IN
Posts: 826
Rep Power: 0 ![]() |
Quote:
- Me |
|
|
|
|
|
|
#7 |
|
A Legend in Underwear
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0 ![]() |
Interesting.
GCC 3.3.1 reports Code:
wtf? 0.06 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 |
|
|
|
|
|
#8 |
|
DriverHeaven Addict
Join Date: Nov 2002
Location: England
Posts: 320
Rep Power: 0 ![]() |
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
__________________
|
|
|
|
|
|
#9 |
|
DriverHeaven Junior Member
Join Date: Jul 2002
Posts: 89
Rep Power: 0 ![]() |
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 |
|
|
|
![]() |
| Thread Tools | |
|
|