dynamicsoar's log

主に研究関係のメモ

切り捨て、四捨五入と整数化について

前も書いたかもしれないけど。
参考: http://fortran.hiroshism.com/ref.html
追加: http://nkmrtkhd.blogspot.com/2010/07/fortranintnint.html

program test_anint
  implicit none
  real(8) :: foo = 7.536d0
  
  write(*,"(2x,'       foo  = ',f9.4)")  foo
  
  write(*,"(2x,'  aint(foo) = ',f9.4)")  aint(foo) !! 小数点以下切り捨て
  
  write(*,"(2x,' anint(foo) = ',f9.4)") anint(foo) !! 小数点以下を四捨五入(実数型→実数型のまま)
  
  write(*,"(2x,'  nint(foo) = ',i9)")  nint(foo) !! 小数点以下を四捨五入してから、実数型→整数型
  ! write(*,"(2x,'  nint(foo) = ',f9.4)")  nint(foo)  ← f9.4 はコンパイルエラーになる
  
  stop
endprogram test_anint

追記:
これらに加えて、int は「小数点以下を切り捨て → 整数化」のようだ。