Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

REAL(4) to REAL(8) conversion

zzunic
Einsteiger
1.046Aufrufe

Dear All,

I have a problem with a real number conversion. I would like to convert a single precision real number into a double precision. I wrote a simple program which uses DBLE() function.

% cat conversion.f
program conversion

real(4) sx1
real(8) dx1

sx1=1.000010
dx1=dble(sx1)
write(*,*) sx1,dx1

end

When I run the program I get the following result:

% ./conversion
1.000010 1.00001001358032

I wish that the result of conversion is 1.00001000000000 (or perhaps 1.00001000000001 or. 1.00000999999999).

With Best Regards,

Zoran

0 Kudos
3 Antworten
TimP
Geehrter Beitragender III
1.046Aufrufe
If you are serious about this desire, you will have to WRITE the single precision value to a character string, and READ it back to the double precision value. Or, you could use
dx1 = 1.00001d0
The conversion you have written performs an exact binary extension. As 1.00001d0 is not exactly representable in single precision, it is different from 1.00001, as you showed.
zzunic
Einsteiger
1.046Aufrufe

Dear Tim,

thanks for the answer. Yes, I was serious with this question, but I was thinking decimaly and not binary :-) .

Here is the listing of the expanded test:

% cat conversion.f
program conversion

real(4) sx1
real(8) dx1
character string*15

sx1=1.00001
dx1=dble(sx1)
write(*,*) sx1,dx1

write(string,*) sx1
read(string,*) dx1
write(*,*) sx1,dx1

end

% ./conversion
1.000010 1.00001001358032
1.000010 1.00001000000000

Regards, Zoran

Steven_L_Intel1
Mitarbeiter
1.046Aufrufe
I would recommend you read the series of three articles The Perils of Real Numbers - it will help you understand what is going on here.
Antworten