Created
April 17, 2019 11:41
-
-
Save vkbo/0e5d0c4b7b1ecb533a0486c79a30f741 to your computer and use it in GitHub Desktop.
Single, Double and Quad Precision PI in Fortran
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program main | |
use, intrinsic :: iso_fortran_env | |
implicit none | |
real(kind=real32), parameter :: pi32 = 3.1415925_real32 | |
real(kind=real64), parameter :: pi64 = 3.141592653589793238462643383279502884197169399375105820974_real64 | |
real(kind=real128), parameter :: pi128 = 3.141592653589793238462643383279502884197169399375105820974_real128 | |
real(kind=real32), parameter :: pi32x = transfer(z'40490fda',1.0_real32) | |
real(kind=real64), parameter :: pi64x = transfer(z'400921fb54442d18',1.0_real64) | |
real(kind=real128), parameter :: pi128x = transfer(z'4000921fb54442d18469898cc51701b8',1.0_real128) | |
write(*,"(a,f10.8,28x,z8.8,33x,b32.32)") "REAL32 : ",pi32, pi32, pi32 | |
write(*,"(a,f19.17,19x,z16.16,22x,b64.64)") "REAL64 : ",pi64, pi64, pi64 | |
write(*,"(a,f36.34,2x,z32.32,2x,b128.128)") "REAL128 : ",pi128,pi128,pi128 | |
write(*,"(a,f10.8,28x,z8.8,33x,b32.32)") "REAL32 : ",pi32x, pi32x, pi32x | |
write(*,"(a,f19.17,19x,z16.16,22x,b64.64)") "REAL64 : ",pi64x, pi64x, pi64x | |
write(*,"(a,f36.34,2x,z32.32,2x,b128.128)") "REAL128 : ",pi128x,pi128x,pi128x | |
end program main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This small piece of code was used for setting a series of constants for a physics simulation code that need to produce the same binary results on various platforms.
Note that in this example, single precision pi is rounded towards zero (bit-by-bit matching to double precision). Alternatively, the last bit can be set to one which is rounded to nearest.
The output of the above example is the following: