typestar

Math intrinsics in Fortran

The numeric toolbox is in the language: hypot, atan2, modulo, and friends.

program intrinsic_math
    implicit none
    real :: opposite = 3.0, adjacent = 4.0

    ! The numeric toolbox is built into the language.
    print *, "hypotenuse: ", hypot(opposite, adjacent)
    print *, "angle, deg: ", atan2(opposite, adjacent) * 57.29578
    print *, "square root: ", sqrt(2.0)
    print *, "e cubed:     ", exp(3.0)
    print *, "wrap 370:    ", modulo(370.0, 360.0)
    print *, "nearest int: ", nint(11.2), nint(11.7)
end program intrinsic_math

How it works

  1. hypot(3.0, 4.0) gives the 3-4-5 answer without overflow risk.
  2. atan2 takes opposite and adjacent and returns the full-quadrant angle.
  3. modulo wraps 370 degrees; nint rounds to nearest rather than truncating.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
433
Tokens
84
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 78 seconds.

Type this snippet

Step 2 of 3 in Kinds & modules, step 23 of 27 in Language basics.

← Previous Next →