Skip to content

Instantly share code, notes, and snippets.

@vkbo
Created April 17, 2019 11:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vkbo/0e5d0c4b7b1ecb533a0486c79a30f741 to your computer and use it in GitHub Desktop.
Save vkbo/0e5d0c4b7b1ecb533a0486c79a30f741 to your computer and use it in GitHub Desktop.
Single, Double and Quad Precision PI in Fortran
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
@vkbo
Copy link
Author

vkbo commented Apr 17, 2019

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:

REAL32  : 3.14159250                            40490FDA                                 01000000010010010000111111011010
REAL64  : 3.14159265358979312                   400921FB54442D18                      0100000000001001001000011111101101010100010001000010110100011000
REAL128 : 3.1415926535897932384626433832795028  4000921FB54442D18469898CC51701B8  01000000000000001001001000011111101101010100010001000010110100011000010001101001100010011000110011000101000101110000000110111000
REAL32  : 3.14159250                            40490FDA                                 01000000010010010000111111011010
REAL64  : 3.14159265358979312                   400921FB54442D18                      0100000000001001001000011111101101010100010001000010110100011000
REAL128 : 3.1415926535897932384626433832795028  4000921FB54442D18469898CC51701B8  01000000000000001001001000011111101101010100010001000010110100011000010001101001100010011000110011000101000101110000000110111000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment