Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29282 Discussions

Double precision variable initialization

maciej
Beginner
2,421 Views
Dears,

Maybe you know the compiler switch that would do a full double precision variable initialization without inserting d0 after every number. I mean when I do:

program test
real*8 i
do i=0.1, 1, 0.1
print *,i
end do
pause
endprogram

the output is
0.100000001490116
0.200000002980232
0.300000004470348
0.400000005960464
0.500000007450581
0.600000008940697
0.700000010430813
0.800000011920929
0.900000013411045

And if my modify:

program test
real*8 i
do i=0.1d0, 1d0, 0.1d0
print *,i
end do
pause
endprogram

I get
0.100000000000000
0.200000000000000
0.300000000000000
0.400000000000000
0.500000000000000
0.600000000000000
0.700000000000000
0.800000000000000
0.900000000000000
1.00000000000000

I would like that if I initialize the double precision variable it is always initialized at its full precision not single. Always using the d0 symbols makes the loops less readable and so error prone.

regards,
Maciej

0 Kudos
6 Replies
lklawrie
Beginner
2,421 Views
"Always using the d0 symbols makes the loops less readable and so error prone."

But using them is more standard and will work with all compilers.

You may be looking for the /real:64 switch.

Linda
0 Kudos
Steven_L_Intel1
Employee
2,421 Views
And it's less error prone to require a switch to give the meaning you want? I am very strongly in favor of expressing in the source exactly what you mean, and not relying on something external to the source such as a switch.

Also, using a non-integer variable as a DO loop control variable is a "deleted feature" and prone to many problems.

I suggest you take advantage of modern Fortran and do it as I see many do:

program test
integer, parameter :: DP = selected_real_kind(15,300)
real(DP) x
integer i
x = 0.1_DP
do i=1,10
print *,x
x = x + 0.1_DP
end do
pause
endprogram

Of course, you should also understand that 0.1 is not exactly representable in binary floating point.

If you insist on hiding your intentions from the reader of your code, the switch you are looking for is /real_size:64.
0 Kudos
maciej
Beginner
2,421 Views

Thank you I will use a switch as propably noone will use my code later. And yes, for me the notation with D is less readable in the code but it is only my experience. In fact I created that forum thread after making notoriuos errors with this. It simply slows down my creating process :)

So I was always wondering why is that Fortrans feature for. I dont know why Fortran specifications assumes that I dont want to use full precision of variable if I process it with some constant values. I understand, that if I intentionally wanted that behaviour I should use a D or E. But default should work with precision selected by programmer on the stage of declaration. I know it is probably some specification since ancient times kept for backward compatibility.

These are just my thoughts only. But such a flowers always causes me thinking to try IMSL with Visual Basic NET (I am still very impressed of how much VB NET is a programmer friendly language). And I see there is an edition of IMSL 7 for C#/VB NET :)

regards and thanks,

Maciej

0 Kudos
Steven_L_Intel1
Employee
2,421 Views
In Fortran, items on the right side of an assignment are evaluated without knowledge of what the type on the left side is. In the days before Fortran 90, it was common for compilers to keep constants in the highest available precision and convert down only when required, but Fortran 90 established the concept that constants had a "kind" and if you don't use a kind specifier or an E/D exponent, you get "default real".

Intel Fortran also has a /fpconstant switch which may be closer to what you want - it keeps floating point constant in the higher precision.
0 Kudos
mecej4
Honored Contributor III
2,421 Views
>I dont know why Fortran specifications assumes that I dont want to use full precision of variable if I process it with some constant values.

It makes no such assumption. Variables in expressions have the types that they were declared with, but these declarations do not cover constants.

> But default should work with precision selected by programmer on the stage of declaration.

Declarations of variables (whether explicit or implicit) do not affect the types of constants used in expressions. Expressions can be mixed mode, i.e., be made up of parts of different types and kinds.

> I know it is probably some specification since ancient times kept for backward compatibility.

("I know" does not go well with "probably".) Although Fortran is an old language and backward compatibility may be more prized in it more than in other languages, useless and harmful features do get revised or dropped when the language is revised.

> But such a flowers always causes me thinking to try IMSL with Visual Basic NET

The grass is greener over the septic tank. For most people, language selection depends on more attributes than just flowers.
0 Kudos
maciej
Beginner
2,421 Views

Thank to all of you for the explanations.

Dear mecej4, I was certainly not clear enough. I provided sample code which shows what behavior I meant.

In my opinion the first code should give the same results as the second. I know that it doesnt and I have to obey the rule of this language. I wanted only to know an easier way to increase my programming, errors-free efficiency.

Best regards,

Maciej

0 Kudos
Reply