Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

Floating point fread bug with C++ .NET 2003 [code attached]

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Phil Scadden - 05 Oct 2005 04:41 GMT
Couldnt submit this to MS without paying $$$ but here is a bug with C++.

This tiny snippet reads from a file containing a single DOUBLE

#include<stdio.h>

int main()
{
double output;
FILE *test_out;

test_out = fopen("test.out","r");
fread (&(output),sizeof(double),1,test_out);
fclose(test_out);
printf("Output:  %e\n", output);
}

Problem is that output is wrong - should be 4890.695 but get rubbish
instead. It is a different no. to what
was written with fwrite. The bizarre thing is that for most numbers it works
fine but this one (and others),
I get rubbish. If I compile exactly the same code with other C compilers on
the same machine, they read the
file correctly. I infer this is BUG in MS C++.

Attached is file with the code and the 8 byte input file if anyone wants to
confirm this.
Victor Bazarov - 05 Oct 2005 05:16 GMT
> Couldnt submit this to MS without paying $$$ but here is a bug with
> C++.
[quoted text clipped - 9 lines]
>
> test_out = fopen("test.out","r");

Try

 test_out = fopen("test.out", "rb");

> fread (&(output),sizeof(double),1,test_out);
> fclose(test_out);
> printf("Output:  %e\n", output);
> }
>
> Problem is that output is wrong [...]

RTFM.  If you open the file to be read unformatted, open it as binary.

V
Carl Daniel [VC++ MVP] - 05 Oct 2005 14:32 GMT
> Couldnt submit this to MS without paying $$$ but here is a bug with
> C++.

In addition to Victor's completely correct response, and for the benefit of
other readers, you can submit bug reports for visual studio at the MSDN
Product Feedback Center:

http://lab.msdn.microsoft.com/productfeedback/

...although you probably should research your "bug" a bit more carefully
before concluding it's a bug and reporting it.

-cd
Phil Scadden - 05 Oct 2005 21:55 GMT
Thank you Victor. Changing the open statement does work. In my defense, I
will say that is code is being ported onto Windows, and in its original
platform, the manual say "b" mean binary but is unnecessary. Oddly, it works
for 99% of floating point values - well to be precise it got the number
wrong twice in run over 89000 values. This bug report came down from
detailed investigation of those two strange value that did not match other
original from other systems nor from gcc on windows.

> http://lab.msdn.microsoft.com/productfeedback/

Thank you very much for that link. I did not find this from the MS support
site. In fact the MS New Zealand site does not have a product support link
for C++ .NET 2003, only 2002 or VS 2003 (which does not
accept the C++ standard product id). Phone support did not point to this
link either.

However, the error is mine in the code and I am extremely appreciative of
the help that given. My response on Microsoft support was surly for which I
apologise but offer the above frustrating experience in mitigation.

Thanks again, one and all.
Carl Daniel [VC++ MVP] - 06 Oct 2005 06:17 GMT
> Thank you Victor. Changing the open statement does work. In my
> defense, I will say that is code is being ported onto Windows, and in
[quoted text clipped - 4 lines]
> strange value that did not match other original from other systems
> nor from gcc on windows.

Actually, it'll have problems for any values that have the byte 0x0d in
their binary representation. - overall, something on the order of 1 in 50
double values should have problems.

>> http://lab.msdn.microsoft.com/productfeedback/
>
[quoted text clipped - 3 lines]
> accept the C++ standard product id). Phone support did not point to
> this link either.

VS 2003 is C++ .NET 2003 (or rather, a superset of it).

> However, the error is mine in the code and I am extremely
> appreciative of the help that given. My response on Microsoft support
> was surly for which I apologise but offer the above frustrating
> experience in mitigation.
>
> Thanks again, one and all.

-cd
Phil Scadden - 06 Oct 2005 22:00 GMT
> VS 2003 is C++ .NET 2003 (or rather, a superset of it).

It would not accept the C++ .NET 2003 product id however (it was clearly not
Enterprise nor architect which were the two options offered.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.