program demo_acos
use, intrinsic :: iso_fortran_env, only : real_kinds,real32,real64,real128
implicit none
character(len=*),parameter :: all='(*(g0,1x))'
real(kind=real64) :: x , d2r
! basics
x = 0.866_real64
print all,'acos(',x,') is ', acos(x)
! acos(-1) should be PI
print all,'for reference &
&PI ~= 3.14159265358979323846264338327950288419716939937510'
write(*,*) acos(-1.0_real64)
d2r=acos(-1.0_real64)/180.0_real64
print all,'90 degrees is ', d2r*90.0_real64, ' radians'
! elemental
print all,'elemental',acos([-1.0,-0.5,0.0,0.50,1.0])
! complex
print *,'complex',acos( (-1.0, 0.0) )
print *,'complex',acos( (-1.0, -1.0) )
print *,'complex',acos( ( 0.0, -0.0) )
print *,'complex',acos( ( 1.0, 0.0) )
end program demo_acos
acos( 0.86599999999999999 ) is 0.52364958093182890
for reference PI ~= 3.14159265358979323846264338327950288419716939937510
3.1415926535897931
90 degrees is 1.5707963267948966 radians
elemental 3.14159274 2.09439516 1.57079637 1.04719758 0.00000000
complex (3.14159274,-0.00000000)
complex (2.23703575,1.06127501)
complex (1.57079637,0.00000000)
complex (0.00000000,-0.00000000)
The result has a value equal to a processor-dependent approximation to
the inverse hyperbolic cosine function of X.
如果 x 是 complex,则结果的虚部以弧度为单位,介于
0 <= aimag(acosh(x)) <= PI
示例程序:
program demo_acosh
use,intrinsic :: iso_fortran_env, only : dp=>real64,sp=>real32
implicit none
real(kind=dp), dimension(3) :: x = [ 1.0d0, 2.0d0, 3.0d0 ]
write (*,*) acosh(x)
end program demo_acosh
0.000000000000000E+000 1.31695789692482 1.76274717403909
asin(3) computes the arcsine of its argument x.
反正弦是正弦函数的反函数。当已知直角三角形的斜边和对边的长度时,它通常用于三角几何学中求取角度。
xThe value to compute the arcsine of
类型应为 real 和小于或等于 1 的量级;或者是_complex_。
The result has a value equal to a processor-dependent approximation
to arcsin(x).
If x is real the result is real and it is expressed in radians
and lies in the range
PI/2 <= ASIN (X) <= PI/2.
If the argument (and therefore the result) is imaginary the real part
of the result is in radians and lies in the range
-PI/2 <= real(asin(x)) <= PI/2
当你知道角度对边与斜边的比率时,反正弦将允许你找到直角的度量。
因此,如果你知道火车轨道在 50 英里长的轨道上上升 1.25 英里,你可以使用反正弦来确定轨道的平均倾斜角度。给定
sin(theta) = 1.25 miles/50 miles (opposite/hypotenuse)
示例程序:
program demo_asin
use, intrinsic :: iso_fortran_env, only : dp=>real64
implicit none
! value to convert degrees to radians
real(kind=dp),parameter :: D2R=acos(-1.0_dp)/180.0_dp
real(kind=dp) :: angle, rise, run
character(len=*),parameter :: all='(*(g0,1x))'
! given sine(theta) = 1.25 miles/50 miles (opposite/hypotenuse)
! then taking the arcsine of both sides of the equality yields
! theta = arcsine(1.25 miles/50 miles) ie. arcsine(opposite/hypotenuse)
rise=1.250_dp
run=50.00_dp
angle = asin(rise/run)
print all, 'angle of incline(radians) = ', angle
angle = angle/D2R
print all, 'angle of incline(degrees) = ', angle
print all, 'percent grade=',rise/run*100.0_dp
end program demo_asin
angle of incline(radians) = 2.5002604899361139E-002
angle of incline(degrees) = 1.4325437375665075
percent grade= 2.5000000000000000
百分比等级是斜率,写成百分比。要计算斜率,你将上升高等除以运行路程。在示例中,50 英里的路程上升 1.25 英里,因此坡度为 1.25/50 = 0.025。写成百分比,这是 2.5 %。
对于美国来说,2% 和 1/2% 通常被认为是上限。这意味着前进 100 英尺时上升 2.5 英尺。在美国,这是美国第一条主要铁路巴尔的摩和俄亥俄州的最高等级。注意曲线会增加火车上的摩擦阻力,从而降低允许坡度。
FORTRAN 77 , for a complex argument Fortran 2008
反函数: sin(3)
Resources
维基百科:反三角函数
fortran-lang intrinsic descriptions (license: MIT) @urbanjost
asinh
asinh(3) - [MATHEMATICS:TRIGONOMETRIC] 反双曲正弦函数
Synopsis
result = asinh(x)
elemental TYPE(kind=KIND) function asinh(x)
TYPE(kind=KIND) :: x
The result has a value equal to a processor-dependent approximation
to the inverse hyperbolic sine function of x.
If x is complex, the imaginary part of the result is in radians and lies
between -PI/2 <= aimag(asinh(x)) <= PI/2.
示例程序:
program demo_asinh
use,intrinsic :: iso_fortran_env, only : dp=>real64,sp=>real32
implicit none
real(kind=dp), dimension(3) :: x = [ -1.0d0, 0.0d0, 1.0d0 ]
! elemental
write (*,*) asinh(x)
end program demo_asinh
-0.88137358701954305 0.0000000000000000 0.88137358701954305
elemental TYPE(kind=KIND) function atan(y,x)
TYPE(kind=KIND),intent(in) :: x
TYPE(kind=**),intent(in),optional :: y
If y is present x and y must both be real.
Otherwise, x may be complex.
KIND can be any kind supported by the associated type.
The returned value is of the same type and kind as x.
atan(3) computes the arctangent of x.
xThe value to compute the arctangent of.
if y is present, x shall be real.
返回值与x的类型和种类相同。如果y存在,则结果与atan2(y,x)相同。否则,它是x的反正切,其中结果的实部以弧度为单位,位于**-PI/2<=atan(X)<=PI/2**的范围内
示例程序:
program demo_atan
use, intrinsic :: iso_fortran_env, only : real_kinds, &
& real32, real64, real128
implicit none
character(len=*),parameter :: all='(*(g0,1x))'
real(kind=real64),parameter :: &
Deg_Per_Rad = 57.2957795130823208767981548_real64
real(kind=real64) :: x
x=2.866_real64
print all, atan(x)
print all, atan( 2.0d0, 2.0d0),atan( 2.0d0, 2.0d0)*Deg_Per_Rad
print all, atan( 2.0d0,-2.0d0),atan( 2.0d0,-2.0d0)*Deg_Per_Rad
print all, atan(-2.0d0, 2.0d0),atan(-2.0d0, 2.0d0)*Deg_Per_Rad
print all, atan(-2.0d0,-2.0d0),atan(-2.0d0,-2.0d0)*Deg_Per_Rad
end program demo_atan
1.235085437457879
.7853981633974483 45.00000000000000
2.356194490192345 135.0000000000000
-.7853981633974483 -45.00000000000000
-2.356194490192345 -135.0000000000000
atan2(3) computes in radians a processor-dependent approximation of
the arctangent of the complex number ( x, y ) or equivalently the
principal value of the arctangent of the value y/x (which determines
a unique angle).
If y has the value zero, x shall not have the value zero.
The resulting phase lies in the range -PI <= ATAN2 (Y,X) <= PI and is equal to a
processor-dependent approximation to a value of arctan(Y/X).
yThe imaginary component of the complex value (x,y) or the y
component of the point <x,y>.
The value returned is by definition the principal value of the complex
number (x, y), or in other terms, the phase of the phasor x+i*y.
The principal value is simply what we get when we adjust a radian value
to lie between -PI and PI inclusive,
The classic definition of the arctangent is the angle that is formed
in Cartesian coordinates of the line from the origin point <0,0>
to the point <x,y> .
Pictured as a vector it is easy to see that if x and y are both
zero the angle is indeterminate because it sits directly over the origin,
so atan(0.0,0.0) will produce an error.
Range of returned values by quadrant:
> +PI/2
> PI/2 < z < PI | 0 > z < PI/2
> +-PI -------------+---------------- +-0
> PI/2 < -z < PI | 0 < -z < PI/2
> -PI/2
NOTES:
If the processor distinguishes -0 and +0 then the sign of the
returned value is that of Y when Y is zero, else when Y is zero
the returned value is always positive.
! basic usage
! ATAN2 (1.5574077, 1.0) has the value 1.0 (approximately).
z=atan2(1.5574077, 1.0)
write(*,*) 'radians=',z,'degrees=',r2d(z)
! elemental arrays
write(*,*)'elemental',atan2( [10.0, 20.0], [30.0,40.0] )
! elemental arrays and scalars
write(*,*)'elemental',atan2( [10.0, 20.0], 50.0 )
! break complex values into real and imaginary components
! (note TAN2() can take a complex type value )
c=(0.0,1.0)
write(*,*)'complex',c,atan2( x=c%re, y=c%im )
! extended sample converting cartesian coordinates to polar
COMPLEX_VALS: block
real :: ang, radius
complex,allocatable :: vals(:)
vals=[ &
( 1.0, 0.0 ), & ! 0
( 1.0, 1.0 ), & ! 45
( 0.0, 1.0 ), & ! 90
(-1.0, 1.0 ), & ! 135
(-1.0, 0.0 ), & ! 180
(-1.0,-1.0 ), & ! 225
( 0.0,-1.0 )] ! 270
do i=1,size(vals)
call cartesian_to_polar(vals(i)%re, vals(i)%im, radius,ang)
write(
*,101)vals(i),ang,r2d(ang),radius
enddo
101 format( &
& 'X= ',f5.2, &
& ' Y= ',f5.2, &
& ' ANGLE= ',g0, &
& T38,'DEGREES= ',g0.4, &
& T54,'DISTANCE=',g0)
endblock COMPLEX_VALS
contains
elemental real function r2d(radians)
! input radians to convert to degrees
doubleprecision,parameter :: DEGREE=0.017453292519943d0 ! radians
real,intent(in) :: radians
r2d=radians / DEGREE ! do the conversion
end function r2d
subroutine cartesian_to_polar(x,y,radius,inclination)
! return angle in radians in range 0 to 2*PI
implicit none
real,intent(in) :: x,y
real,intent(out) :: radius,inclination
radius=sqrt(x**2+y**2)
if(radius.eq.0)then
inclination=0.0
inclination=atan2(y,x)
if(inclination < 0.0)inclination=inclination+2*atan2(0.0d0,-1.0d0)
endif
end subroutine cartesian_to_polar
end program demo_atan2
> radians= 1.000000 degrees= 57.29578
> elemental 0.3217506 0.4636476
> elemental 0.1973956 0.3805064
> complex (0.0000000E+00,1.000000) 1.570796
> X= 1.00 Y= 0.00 ANGLE= .000000 DEGREES= .000 DISTANCE=1.000000
> X= 1.00 Y= 1.00 ANGLE= .7853982 DEGREES= 45.00 DISTANCE=1.414214
> X= 0.00 Y= 1.00 ANGLE= 1.570796 DEGREES= 90.00 DISTANCE=1.000000
> X= -1.00 Y= 1.00 ANGLE= 2.356194 DEGREES= 135.0 DISTANCE=1.414214
> X= -1.00 Y= 0.00 ANGLE= 3.141593 DEGREES= 180.0 DISTANCE=1.000000
> X= -1.00 Y= -1.00 ANGLE= 3.926991 DEGREES= 225.0 DISTANCE=1.414214
> X= 0.00 Y= -1.00 ANGLE= 4.712389 DEGREES= 270.0 DISTANCE=1.000000
program demo_atanh
implicit none
real, dimension(3) :: x = [ -1.0, 0.0, 1.0 ]
write (*,*) atanh(x)
end program demo_atanh
> -Infinity 0.0000000E+00 Infinity
x is of type real or complex of any valid kind.
KIND may be any kind supported by the associated type of x.
The returned value will be of the same type and kind as the argument
cos(3) computes the cosine of an angle x given the size of
the angle in radians.
一个 real 的余弦是直角三角形的相邻边与斜边的比率。.
xThe angle in radians to compute the cosine of.
The return value is the tangent of x.
If x is of the type real, the return value is in radians and lies in
the range -1 <= cos(x) <= 1 .
If x is of type complex, its real part is regarded as a value in
radians, often called the phase.
示例程序:
program demo_cos
implicit none
character(len=*),parameter :: g2='(a,t20,g0)'
doubleprecision,parameter :: PI=atan(1.0d0)*4.0d0
write(*,g2)'COS(0.0)=',cos(0.0)
write(*,g2)'COS(PI)=',cos(PI)
write(*,g2)'COS(PI/2.0d0)=',cos(PI/2.0d0),'EPSILON=',epsilon(PI)
write(*,g2)'COS(2*PI)=',cos(2*PI)
write(*,g2)'COS(-2*PI)=',cos(-2*PI)
write(*,g2)'COS(-2000*PI)=',cos(-2000*PI)
write(*,g2)'COS(3000*PI)=',cos(3000*PI)
end program demo_cos
> COS(0.0)= 1.000000
> COS(PI)= -1.000000000000000
> COS(PI/2.0d0)= .6123233995736766E-16
> EPSILON= .2220446049250313E-15
> COS(2*PI)= 1.000000000000000
> COS(-2*PI)= 1.000000000000000
> COS(-2000*PI)= 1.000000000000000
> COS(3000*PI)= 1.000000000000000
cosh(3) computes the hyperbolic cosine of x.
If x is of type complex its imaginary part is regarded as a value
in radians.
xthe value to compute the hyperbolic cosine of
If x is complex, the imaginary part of the result is in radians.
如果 x 为 real,则返回值的下限为 1,cosh(x) >= 1。
示例程序:
program demo_cosh
use, intrinsic :: iso_fortran_env, only : &
& real_kinds, real32, real64, real128
implicit none
real(kind=real64) :: x = 1.0_real64
write(*,*)'X=',x,'COSH(X=)',cosh(x)
end program demo_cosh
> X= 1.00000000000000 COSH(X=) 1.54308063481524
x may be any real or complex type
KIND may be any kind supported by the associated type of x.
The returned value will be of the same type and kind as the argument
sin(3) computes the sine of an angle given the size of the angle
in radians.
直角三角形中角的正弦是给定角对边的长度除以斜边长度的比值(对比斜)。
xThe angle in radians to compute the sine of.
The return value contains the processor-dependent approximation of
the sine of x
If X is of type real, it is regarded as a value in radians.
If X is of type complex, its real part is regarded as a value
in radians.
Haversine Formula
来自维基百科中关于“Haversine 公式”的文章:
The haversine formula is an equation important in navigation,
giving great-circle distances between two points on a sphere from
their longitudes and latitudes.
因此,要显示美国田纳西州纳什维尔国际机场 (BNA) 和美国加利福尼亚州洛杉矶国际机场 (LAX) 之间的大圆距离,你可以从它们的纬度和经度开始,通常为
BNA: N 36 degrees 7.2', W 86 degrees 40.2'
LAX: N 33 degrees 56.4', W 118 degrees 24.0'
转换为以度为单位的浮点值是:
Latitude Longitude
- BNA
36.12, -86.67
- LAX
33.94, -118.40
然后使用半正弦公式粗略计算出沿地球表面的位置之间的距离:
示例程序:
program demo_sin
implicit none
real :: d
d = haversine(36.12,-86.67, 33.94,-118.40) ! BNA to LAX
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function haversine(latA,lonA,latB,lonB
) result (dist)
! calculate great circle distance in kilometers
! given latitude and longitude in degrees
real,intent(in) :: latA,lonA,latB,lonB
real :: a,c,dist,delta_lat,delta_lon,lat1,lat2
real,parameter :: radius = 6371 ! mean earth radius in kilometers,
! recommended by the International Union of Geodesy and Geophysics
! generate constant pi/180
real, parameter :: deg_to_rad = atan(1.0)/45.0
delta_lat = deg_to_rad*(latB-latA)
delta_lon = deg_to_rad*(lonB-lonA)
lat1 = deg_to_rad*(latA)
lat2 = deg_to_rad*(latB)
a = (sin(delta_lat/2))**2 + &
& cos(lat1)*cos(lat2)*(sin(delta_lon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program demo_sin
> distance: 2886.4446 km
The result has a value equal to a processor-dependent approximation
to sinh(X). If X is of type complex its imaginary part is regarded
as a value in radians.
示例程序:
program demo_sinh
use, intrinsic :: iso_fortran_env, only : &
& real_kinds, real32, real64, real128
implicit none
real(kind=real64) :: x = - 1.0_real64
real(kind=real64) :: nan, inf
character(len=20) :: line
! basics
print *, sinh(x)
print *, (exp(x)-exp(-x))/2.0
! sinh(3) is elemental and can handle an array
print *, sinh([x,2.0*x,x/3.0])
! a NaN input returns NaN
line='NAN'
read(line,*) nan
print *, sinh(nan)
! a Inf input returns Inf
line='Infinity'
read(line,*) inf
print *, sinh(inf)
! an overflow returns Inf
x=huge(0.0d0)
print *, sinh(x)
end program demo_sinh
-1.1752011936438014
-1.1752011936438014
-1.1752011936438014 -3.6268604078470190 -0.33954055725615012
Infinity
Infinity
the TYPE of x may be real or complex of any supported kind
The returned value will be of the same type and kind as the argument
tan(3) computes the tangent of x.
xThe angle in radians to compute the tangent of for real input.
If x is of type complex, its real part is regarded as a value
in radians.
program demo_tan
use, intrinsic :: iso_fortran_env, only : real_kinds, &
& real32, real64, real128
implicit none
real(kind=real64) :: x = 0.165_real64
write(*,*)x, tan(x)
end program demo_tan
0.16500000000000001 0.16651386310913616
Returns the hyperbolic tangent of x.
If x is complex, the imaginary part of the result is regarded as
a radian value.
If x is real, the return value lies in the range
-1 <= tanh(x) <= 1.
program demo_tanh
use, intrinsic :: iso_fortran_env, only : &
& real_kinds, real32, real64, real128
implicit none
real(kind=real64) :: x = 2.1_real64
write(*,*)x, tanh(x)
end program demo_tanh
2.1000000000000001 0.97045193661345386
random_number(3) returns a single pseudorandom number or an array of
pseudorandom numbers from the uniform distribution over the range
0 <= x < 1.
harvest应为 real 类型的标量或数组。
示例程序:
program demo_random_number
use, intrinsic :: iso_fortran_env, only : dp=>real64
implicit none
integer, allocatable :: seed(:)
integer :: n
integer :: first,last
integer :: i
integer :: rand_int
integer,allocatable :: count(:)
real(kind=dp) :: rand_val
call random_seed(size = n)
allocate(seed(n))
call random_seed(get=seed)
first=1
last=10
allocate(count(last-first+1))
! To have a discrete uniform distribution on the integers
! [first, first+1, ..., last-1, last] carve the continuous
! distribution up into last+1-first equal sized chunks,
! mapping each chunk to an integer.
! One way is:
! call random_number(rand_val)
! choose one from last-first+1 integers
! rand_int = first + FLOOR((last+1-first)*rand_val)
count=0
! generate a lot of random integers from 1 to 10 and count them.
! with a large number of values you should get about the same
! number of each value
do i=1,100000000
call random_number(rand_val)
rand_int=first+floor((last+1-first)*rand_val)
if(rand_int.ge.first.and.rand_int.le.last)then
count(rand_int)=count(rand_int)+1
write(*,*)rand_int,' is out of range'
endif
enddo
write(*,'(i0,1x,i0)')(i,count(i),i=1,size(count))
end program demo_random_number
1 10003588
2 10000104
3 10000169
4 9997996
5 9995349
6 10001304
7 10001909
8 9999133
9 10000252
10 10000196
subroutine random_seed( size, put, get )
integer,intent(out),optional :: size
integer,intent(in),optional :: put(*)
integer,intent(out),optional :: get(*)
size a scalar default integer
put a rank-one default integer array
get a rank-one default integer array
the result
random_seed(3) restarts or queries the state of the pseudorandom
number generator used by random_number.
如果在没有参数的情况下调用 random_seed,它会使用从操作系统检索到的随机数据作为种子。
sizespecifies the minimum size of the arrays used with the put
and get arguments.
exp(3) returns the value of e (the base of natural logarithms)
raised to the power of x.
"e" is also known as Euler's constant.
If x is of type complex, its imaginary part is regarded as a value
in radians such that if (see Euler's formula):
cx=(re,im)
exp(cx) = exp(re) * cmplx(cos(im),sin(im),kind=kind(cx))
由于exp(3)是log(3)的反函数,x 分量的最大有效幅值x 是log(huge(x)) 。
x类型应该是 real 或者 complex.
结果的值为 e**x 其中 e 是欧拉常数。
If x is of type complex, its imaginary part is
regarded as a value in radians.
示例程序:
program demo_exp
implicit none
real :: x, re, im
complex :: cx
x = 1.0
write(*,*)"Euler's constant is approximately",exp(x)
!! complex values
! given
re=3.0
im=4.0
cx=cmplx(re,im)
! complex results from complex arguments are Related to Euler's formula
write(*,*)'given the complex value ',cx
write(*,*)'exp(x) is',exp(cx)
write(*,*)'is the same as',exp(re)*cmplx(cos(im),sin(im),kind=kind(cx))
! exp(3) is the inverse function of log(3) so
! the real component of the input must be less than or equal to
write(*,*)'maximum real component',log(huge(0.0))
! or for double precision
write(*,*)'maximum doubleprecision component',log(huge(0.0d0))
! but since the imaginary component is passed to the cos(3) and sin(3)
! functions the imaginary component can be any real value
end program demo_exp
Euler's constant is approximately 2.718282
given the complex value (3.000000,4.000000)
exp(x) is (-13.12878,-15.20078)
is the same as (-13.12878,-15.20078)
maximum real component 88.72284
maximum doubleprecision component 709.782712893384
xThe value to compute the natural log of.
If x is real, its value shall be greater than zero.
If x is complex, its value shall not be zero.
The natural logarithm of x.
If x is the complex value (r,i) , the imaginary part "i" is in the range
-PI < i <= PI
If the real part of x is less than zero and the imaginary part of
x is zero, then the imaginary part of the result is approximately
PI if the imaginary part of PI is positive real zero or the
processor does not distinguish between positive and negative real zero,
and approximately -PI if the imaginary part of x is negative
real zero.
示例程序:
program demo_log
implicit none
real(kind(0.0d0)) :: x = 2.71828182845904518d0
complex :: z = (1.0, 2.0)
write(*,*)x, log(x) ! will yield (approximately) 1
write(*,*)z, log(z)
end program demo_log
2.7182818284590451 1.0000000000000000
(1.00000000,2.00000000) (0.804718971,1.10714877)
log10(3) computes the base 10 logarithm of x. This is generally
called the "common logarithm".
x一个 real 值 > 0 以获取日志。
program demo_log10
use, intrinsic :: iso_fortran_env, only : real_kinds, &
& real32, real64, real128
implicit none
real(kind=real64) :: x = 10.0_real64
x = log10(x)
write(*,'(*(g0))')'log10(',x,') is ',log10(x)
! elemental
write(*, *)log10([1.0, 10.0,
100.0, 1000.0, 10000.0, &
& 100000.0, 1000000.0, 10000000.0])
end program demo_log10
> log10(1.000000000000000) is .000000000000000
> 0.0000000E+00 1.000000 2.000000 3.000000 4.000000
> 5.000000 6.000000 7.000000
sqrt(3) computes the principal square root of x.
考虑其平方根的数字被称为 radicand(弧度) 。
In mathematics, a square root of a radicand x is a number y
such that y*y = x.
Every nonnegative radicand x has two square roots of the same unique
magnitude, one positive and one negative. The nonnegative square root
is called the principal square root.
例如,9 的主平方根是 3,即使 (-3)*(-3) 也是 9。
Square roots of negative numbers are a special case of complex numbers,
where with complex input the components of the radicand need
not be positive in order to have a valid square root.
xThe radicand to find the principal square root of.
If x is real its value must be greater than or equal to zero.
The principal square root of x is returned.
For a complex result the real part is greater than or equal to zero.
When the real part of the result is zero, the imaginary part has the
same sign as the imaginary part of x.
示例程序:
program demo_sqrt
use, intrinsic :: iso_fortran_env, only : real_kinds, &
& real32, real64, real128
implicit none
real(kind=real64) :: x, x2
complex :: z, z2
! basics
x = 2.0_real64
! complex
z = (1.0, 2.0)
write(*,*)'input values ',x,z
x2 = sqrt(x)
z2 = sqrt(z)
write(*,*)'output values ',x2,z2
! elemental
write(*,*)'elemental',sqrt([64.0,121.0,30.0])
! alternatives
x2 = x**0.5
z2 = z**0.5
write(*,*)'alternatively',x2,z2
end program demo_sqrt
input values 2.00000000000000 (1.000000,2.000000)
output values 1.41421356237310 (1.272020,0.7861513)
elemental 8.000000 11.00000 5.477226
alternatively 1.41421356237310 (1.272020,0.7861513)
hypot(3) - [MATHEMATICS] Returns the Euclidean distance - the distance between a point and the origin.
Synopsis
result = hypot(x, y)
elemental real(kind=KIND) function hypot(x,y)
real(kind=KIND),intent(in) :: x
real(kind=KIND),intent(in) :: y
hypot(3) is referred to as the Euclidean distance function. It is
equal to
sqrt(x**2+y**2)
without undue underflow or overflow.
在数学上,欧氏空间中两点之间的 欧氏距离 是两点之间直线段的长度。
hypot(x,y) 返回点 <x,y> 与原点之间的距离.
x其类型应该是 real.
program demo_hypot
use, intrinsic :: iso_fortran_env, only : &
& real_kinds, real32, real64, real128
implicit none
real(kind=real32) :: x, y
real(kind=real32),allocatable :: xs(:), ys(:)
integer :: i
character(len=*),parameter :: f='(a,/,SP,*(3x,g0,1x,g0:,/))'
x = 1.e0_real32
y = 0.5e0_real32
write(*,*)
write(*,'(*(g0))')'point <',x,',',y,'> is ',hypot(x,y)
write(*,'(*(g0))')'units away from the origin'
write(*,*)
! elemental
xs=[ x, x**2, x*10.0, x*15.0, -x**2 ]
ys=[ y, y**2, -y*20.0, y**2, -y**2 ]
write(*,f)"the points",(xs(i),ys(i),i=1,size(xs))
write(*,f)"have distances from the origin of ",hypot(xs,ys)
write(*,f)"the closest is",minval(hypot(xs,ys))
end program demo_hypot
point <1.00000000,0.500000000> is 1.11803401
units away from the origin
the points
+1.00000000 +0.500000000
+1.00000000 +0.250000000
+10.0000000 -10.0000000
+15.0000000 +0.250000000
-1.00000000 -0.250000000
have distances from the origin of
+1.11803401 +1.03077638
+14.1421356 +15.0020828
+1.03077638
the closest is
+1.03077638
the Bessel function of the first kind of order 0 of x.
The result lies in the range -0.4027 <= bessel(0,x) <= 1.
示例程序: