News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Rounding problem

Started by Hans, February 09, 2007, 07:08:56 PM

Previous topic - Next topic

Hans

I'm new.
My problem is "I think" a rounding problem.

program fa;
#include( "stdlib.hhf" );
storage
   filehandle:         dword;
procedure readfile;
begin readfile;
   fileio.open( "data.txt", fileio.r );
   mov( eax, filehandle );
      fileio.get( filehandle, ST0 );
   fileio.close( filehandle );
end readfile;

ST0 = +6.32000000000000028e+0000

In the file "data.txt" stands the value 6.32 so i want that same value "+6.32000000000000+000" in ST0.
what do i wrong?

thanks

Evenbit

Use 'fileio.getf' like so:
program float;
#include("stdlib.hhf")

var
fFirst :real80;
fSecond :real80;
hFile :dword;

static
vOne :dword := 35;
vTwo :dword := 100;
vThree :dword := 85;

begin float;

finit();
fild( vOne );
fild( vTwo );
fdiv(); // 35 / 100 = 0.35
fild( vThree );
fadd(); // 0.35 + 85 = 85.35
fstp( fFirst );

stdout.put( fFirst, nl );

fileio.openNew( "float.dat" );
mov( eax, hFile );
fileio.pute80( hFile, fFirst, 12 );
fileio.close( hFile );

fileio.open( "float.dat", fileio.r );
mov( eax, hFile );
fileio.getf( hFile );
fstp( fSecond );
fileio.close( hFile );

stdout.put( fSecond, nl );

end float;


Another option is to try the new conversion routines (float-to-string and string-to-float) in the 2.0 download of the standard library.

Nathan.

raymond

#2
Hans, you are absolutely right. It's simply a rounding problem.

At this point, I don't know if your file contained the 6.32 as a string or as a float. Regardless, that 6.32, although seemingly a finite number in the decimal system, cannot be converted into a finite number in the binary system. If you round that binary number to a REAL4 or REAL8 and then load it to the FPU with its REAL10 registers, the rounding error will appear. In some cases, it may also appear as a slightly lower number (such as 6.30999999999999972).

If you want to learn more about the FPU, you could have a glance at the following:

http://www.ray.masmcode.com/fpu.html

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com