Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Phil Scadden
Guest
 
Posts: n/a
#1: Nov 17 '05
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
Guest
 
Posts: n/a
#2: Nov 17 '05

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


Phil Scadden wrote:[color=blue]
> 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");[/color]

Try

test_out = fopen("test.out", "rb");
[color=blue]
> fread (&(output),sizeof(double),1,test_out);
> fclose(test_out);
> printf("Output: %e\n", output);
> }
>
> Problem is that output is wrong [...][/color]

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

V


Carl Daniel [VC++ MVP]
Guest
 
Posts: n/a
#3: Nov 17 '05

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


Phil Scadden wrote:[color=blue]
> Couldnt submit this to MS without paying $$$ but here is a bug with
> C++.[/color]

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
Guest
 
Posts: n/a
#4: Nov 17 '05

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


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.
[color=blue]
> http://lab.msdn.microsoft.com/productfeedback/[/color]

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]
Guest
 
Posts: n/a
#5: Nov 17 '05

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


Phil Scadden wrote:[color=blue]
> 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.[/color]

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.
[color=blue]
>[color=green]
>> http://lab.msdn.microsoft.com/productfeedback/[/color]
>
> 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.[/color]

VS 2003 is C++ .NET 2003 (or rather, a superset of it).
[color=blue]
>
> 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.[/color]

-cd


Phil Scadden
Guest
 
Posts: n/a
#6: Nov 17 '05

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


> 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.


Closed Thread