Archive

Archive for the ‘fortran’ Category

Get the value of PI without a constant

December 24, 2025 Leave a comment

Problem

You are using a new programming language and you need the value of PI. However, there’s no such constant in the language. What do you do?

Solution

You can copy the value of PI from somewhere and create your own constant. Or, you can use a math function for that.

>>> import math
>>> from math import *
>>> 
>>> cos(math.pi)
-1.0
>>> acos(-1)
3.141592653589793
>>> acos(-1) == math.pi
True

I must have learnt it in high school, but it was long forgotten. It was nice to re-discover it :)

Fortran

At the end of December 2025, I started to learn Fortran (just for fun). Actually, Modern Fortran is pretty good! I like it. However, the constant PI is missing. So I came up with this:

use iso_fortran_env, only: real64
real(real64), parameter :: PI = acos(-1.0_real64)

Categories: fortran, python Tags: , ,
Design a site like this with WordPress.com
Get started