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
hypot(3.0, 4.0)gives the 3-4-5 answer without overflow risk.atan2takes opposite and adjacent and returns the full-quadrant angle.modulowraps 370 degrees;nintrounds to nearest rather than truncating.
Keywords and builtins used here
endexphypotimplicitmodulonintnoneprintprogramrealsqrt
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.
Step 2 of 3 in Kinds & modules, step 23 of 27 in Language basics.