加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Fortran程序中直接使用度(°)计算三角函数值

(2016-04-19 11:08:43)
分类: Fortran
大家看到这个题目不要惊讶,实际上也就是自定义函数。
Fortran函数库的三角函数计算都是使用弧度做单位,因此计算是需要做单位转换。
我将这个MODULE提供出来,将该模块放在主程序之前,再在主程序中,加入“ use trigonometric ”这个语句就可以了。
在主程序中调用时,sind(45.0),cosd(135.0),tan(225.0) 就可以了。其实很简单,大家都会写。

  module trigonometric
  implicit none
  real(kind=8),parameter :: pi=3.1415926535
  
  contains
  real function sind(angle)
  implicit none
  real angle
  sind=sin(angle*pi/180)
  return
  end function
  
  real function cosd(angle)
  implicit none
  real angle
  cosd=cos(angle*pi/180)
  return
  end function
  
  real function tand(angle)
  implicit none
  real angle
  tand=tan(angle*pi/180)
  return
  end function
  
  end module

下边我举个例子:
  program main
  use trigonometric
  implicit none
  write(*,*) "sin(45d)= ",sind(45.0)  !d-degree
  write(*,*) "cos(135d)= ",cosd(135.0)
  write(*,*) "tan(225d)= ",tand(225.0)
  stop
  end

这样,如果在主程序中需要较多的这类三角函数,就使用MODULE,然后就可以在主程序中直接使用sind,cosd,tand函数,就像在MATLAB中一样。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有