Skip to content

Commit

Permalink
added magnitude function
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwilliams committed Aug 2, 2022
1 parent 3a5625f commit f9aa38f
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/math_module.f90
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,52 @@ module math_module

public :: cube_root
public :: wrap_angle
public :: magnitude

contains
!*****************************************************************************************

!*****************************************************************************************
!>
! Returns a positive number the same magnitude as the input,
! with only one significant digit.
!
! If `mina` is present, then `max(mina,mag(a))` is returned
!
! Examples:
!```
! mag(1234.56) -> 1000.0
! mag(-999.99) -> 900.0
! mag(1.456e-4) -> 0.0001
!```

pure elemental function magnitude(a,mina) result(m)

implicit none

real(wp),intent(in) :: a
real(wp),intent(in),optional :: mina
real(wp) :: m

real(wp) :: x,tmp

x = abs(a)

if (x==0.0_wp) then
if (.not. present(mina)) then
m = 1.0_wp
else
m = mina
end if
else
tmp = 10.0_wp ** floor(log10(x))
m = tmp * floor(x/tmp)
if (present(mina)) m = max(mina,m)
end if

end function magnitude
!*****************************************************************************************

!*****************************************************************************************
!>
! Wrap an angle (in rad) from -pi to pi.
Expand Down

0 comments on commit f9aa38f

Please sign in to comment.