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

run-time calculation give different results than compile-time calculation.

John_D_B_
Beginner
746 Views
This is my first post. I appoligize in advance.

short program:

============================================================

program prime_number_table

!

implicit none

!

integer(kind=8), parameter :: MAX_PRIME = 4294967295 ! 2**32-1

integer(kind=8), parameter :: ROOT_MAX = int(sqrt(real(MAX_PRIME,8)),8)

integer(kind=8), parameter :: PI_MAX_PRIME = 150000

!

write(*,*) "MAX_PRIME: ", MAX_PRIME

write(*,*) "real(MAX_PRIME): ", real(MAX_PRIME,8)

write(*,*) "sqrt(real(MAX_PRIME)): ", sqrt(real(MAX_PRIME))

write(*,*) "int(sqrt(real(MAX_PRIME))): ", int(sqrt(real(MAX_PRIME)),8)

write(*,*) "ROOT_MAX: ", ROOT_MAX

read(*,*)

end program prime_number_table

=======================================================

Results:

MAX_PRIME: 4294967295

real(MAX_PRIME): 4294967295.00000

sqrt(real(MAX_PRIME)): 65536.00

int(sqrt(real(MAX_PRIME))): 65536

ROOT_MAX: 65535

========================================================

Is this a bug or a feature?

compiler version: Intel Visual Fortran Composer XE 2011 Integration for Microsoft Visual Studio* 2005, 12.0.3471.2005, Copyright (C) 2002-2011 Intel Corporation

Thank you very much in advance.

John D Bauer

0 Kudos
3 Replies
rasa
Beginner
746 Views
Lets Try this one.

[bash]program prime_number_table
implicit none

!-- Kinds for specified real precisions.
integer, parameter :: r4_kind = selected_real_kind(6,30)   !-- 6 digits
integer, parameter :: r8_kind = selected_real_kind(12,30)  !-- 12 digits

!-- Kinds for specified integer ranges.
!-- Caveat: Not all compilers have a suitable i8_kind.
integer, parameter :: i1_kind = selected_int_kind(2)  !-- 99 max
integer, parameter :: i2_kind = selected_int_kind(4)  !-- 9,999 max
integer, parameter :: i4_kind = selected_int_kind(9)  !-- 999,999,999 max
integer, parameter :: i8_kind = selected_int_kind(18) !-- 10**18-1 max

integer(kind=i8_kind), parameter :: MAX_PRIME = 4294967295           ! 2**32-1
integer(kind=i8_kind), parameter :: ROOT_MAX = int(sqrt(real(MAX_PRIME,r8_kind)),i8_kind)
integer(kind=i8_kind), parameter :: PI_MAX_PRIME = 150000

!
write(*,*) "MAX_PRIME: ", MAX_PRIME
write(*,*) "real(MAX_PRIME): ", real(MAX_PRIME,r8_kind)
write(*,*) "sqrt(real(MAX_PRIME)): ", sqrt(real(MAX_PRIME, r8_kind))
write(*,*) "int(sqrt(real(MAX_PRIME))): ", int(sqrt(real(MAX_PRIME, r8_kind)),i8_kind)
write(*,*) "ROOT_MAX: ", ROOT_MAX
read(*,*)

end program prime_number_table
[/bash]
Output goes.
[bash] MAX_PRIME:             4294967295
 real(MAX_PRIME):    4294967295.00000     
 sqrt(real(MAX_PRIME)):    65535.9999923706     
 int(sqrt(real(MAX_PRIME))):                  65535
 ROOT_MAX:                  65535
[/bash]

HTH.
0 Kudos
Steven_L_Intel1
Employee
746 Views
The key here is that:

real(MAX_PRIME,8)

as in the definition of ROOT_MAX

is not the same as:

real(MAX_PRIME)

in the WRITE that is different. The second one returns a single precision REAL value which does not have enough precision to hold MAX_PRIME.
0 Kudos
John_D_B_
Beginner
746 Views
The key here is that:

real(MAX_PRIME,8)

as in the definition of ROOT_MAX

is not the same as:

real(MAX_PRIME)

in the WRITE that is different. The second one returns a single precision REAL value which does not have enough precision to hold MAX_PRIME.

Thank you very much. the program from the compilers point of view reads:


program prime_number_table
!
implicit none
!
integer(kind=8), parameter :: MAX_PRIME = 4294967295 ! 2**32-1
integer(kind=8), parameter :: ROOT_MAX = int(sqrt(real(MAX_PRIME,8)),8)
integer(kind=8), parameter :: PI_MAX_PRIME = 150000
!
write(*,*) "MAX_PRIME: ", 4294967295
write(*,*) "real(MAX_PRIME): ", real(4294967295,8) ! This is 64-bit floating point
write(*,*) "sqrt(real(MAX_PRIME)): ", sqrt(real(4294967295)) ! This is 32-bit floating point
write(*,*) "int(sqrt(real(MAX_PRIME))): ", int(sqrt(real(4294967295)),8) ! The square root return a 32-bit
! floating point number
write(*,*) "ROOT_MAX: ", 65536
read(*,*)

end program prime_number_table

============================================================

to be continued.....

0 Kudos
Reply