Fortran:最大和最小整数 [英] Fortran: the largest and the smallest integer

查看:27
本文介绍了Fortran:最大和最小整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fortran 对我来说是全新的,有人可以帮我解决以下问题吗?我想在我的电脑上找出所有整数种类数以及每个种类数的最大值和最小值.我的代码如下:

Fortran is completely new for me, can anybody help me to solve the follwing problem? I want to find out all the integer kind numbers and the largest and the smallest value for each kind number on my pc. I have code listed below:

program intkind
implicit none

integer :: n=1
integer :: integer_range =1


do while(integer_range /= -1)
    print*, "kind_number ", selected_int_kind(n)
    call rang(integer_range)
    n = n *2
    integer_range = selected_int_kind(n)
end do

contains
subroutine rang(largest) 
    integer largest
    print*, huge(largest)

end subroutine

end 

我得到的整数种类数是:1,2,4,8.

The integer kind numbers what I get are : 1,2,4,8.

  1. 为什么每个种类数的最大整数都相同:2147483647?最小整数是否有内在函数?

  1. Why is each largest integer for each kind number the same: 2147483647? And is there a intrinsic function for the smallest integer?

当子程序 rang 被调用时,如何保持整数种类编号?我认为它是最大整数的关键.

How can I keep the integer kind number when the subroutine rang is called? I think it is the key to the largest integer.

推荐答案

你的子程序:

subroutine rang(largest) 
    integer :: largest
    print *, huge(largest)
end subroutine

将默认大小的整数作为输入,并打印适合该默认大小整数的最大可能值.它将总是返回巨大的(默认整数),在大多数系统上,巨大的(4 字节整数)或 2147483647.huge 只考虑变量类型;它不会以任何方式解释变量.您可以执行上述操作的唯一方法是使用参数化派生类型,这些派生类型足够新,以至于在编译器中对它的支持仍然有些参差不齐.

takes as input a default-sized integer, and prints the largest possible value that will fit in that default-sized integer. It will always return huge(default integer) which is, on most systems, huge(4-byte-integer), or 2147483647. huge considers only the variable type; it doesn't interpret the variable in any way. The only way you could do what you're trying to do above is with parameterized derived types, which are new enough that support for it in compilers is still a little spotty.

如果您想查看不同种类的 INTEGER 的范围,则必须使用不同的变量:

If you want to take a look at ranges of different KINDs of INTEGERs, you'll have to use different variables:

program integerkinds
    use iso_fortran_env
    implicit none

    integer :: i
    integer(kind=int8)  :: i8
    integer(kind=int16) :: i16
    integer(kind=int32) :: i32
    integer(kind=int64) :: i64

    integer(kind=selected_int_kind(6)) :: j6
    integer(kind=selected_int_kind(15)):: j15

    print *,'Default:'
    print *, huge(i)
    print *,'Int8:'
    print *, huge(i8)
    print *,'Int16:'
    print *, huge(i16)
    print *,'Int32:'
    print *, huge(i32)
    print *,'Int64:'
    print *, huge(i64)

    print *,''

    print *,'Selected Integer Kind 6:'
    print *, huge(j6)

    print *,'Selected Integer Kind 15:'
    print *, huge(j15)

end program integerkinds

跑步给出:

$ ./intkinds
 Default:
  2147483647
 Int8:
  127
 Int16:
  32767
 Int32:
  2147483647
 Int64:
  9223372036854775807

 Selected Integer Kind 6:
  2147483647
 Selected Integer Kind 15:
  9223372036854775807

这篇关于Fortran:最大和最小整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆