UFO ET IT

포트란 : 정수 * 4 대 정수 (4) 대 정수 (종류 = 4)

ufoet 2021. 1. 15. 07:40
반응형

포트란 : 정수 * 4 대 정수 (4) 대 정수 (종류 = 4)


저는 포트란을 배우려고 노력하고 있으며 많은 다른 정의가 전달되는 것을보고 있으며 그들이 같은 일을 수행하려고 시도하고 있는지 궁금합니다. 다음의 차이점은 무엇입니까?

  • integer*4
  • integer(4)
  • integer(kind=4)

Fortran> = 90에서 가장 좋은 방법은 내장 함수를 사용하여 필요한 정밀도를 지정하는 것입니다. 이것은 이식성을 보장하고 필요한 정밀도를 얻을 수 있도록합니다. 예를 들어, 정수를 얻기 위해 i그리고 my_int그 적어도 8 진수를 지원합니다, 당신은 사용할 수 있습니다 :

integer, parameter :: RegInt_K = selected_int_kind (8)
integer (kind=RegInt_K) :: i, my_int

으로 정의 RegInt_K(또는 선택한 이름)하면 parameter코드 전체에서 기호로 사용할 수 있습니다. 또한 정밀도를 쉽게 변경할 수 있습니다.

8 자리 또는 9 자리 십진수를 요청하면 일반적으로 4 바이트 정수를 얻습니다.

integer*44 바이트 정수를 지정하기 위해 이전 FORTRAN으로 돌아가는 일반적인 확장입니다. 그러나이 구문은 표준 Fortran이 아니며 표준이 아닙니다.

integer (4)integer (RegInt_K)에 대한 짧은 integer (kind=4)또는 integer (kind=RegInt_K). integer (4)동일하지 않습니다 integer*4및 비 휴대용 - 언어 표준 종류의 숫자 값을 지정하지 않습니다. 대부분의 컴파일러는 사용 kind=4이 컴파일러에 대한 - 4 바이트 정수에 대한 integer*4그리고 integer(4)그렇게하지만 예외가 있습니다 - 같은 정수 유형을 제공 할 것입니다 integer(4)비 휴대용 및 피해야합니다.

현실에 대한 접근 방식은 비슷합니다.

업데이트 : 필요한 정밀도로 숫자 유형을 지정하지 않고 대신 사용할 저장소로 지정하려는 경우 Fortran 2008은 방법을 제공합니다. 실수 및 정수는 이후 저장의 비트 수에 의해 특정 될 수 useING를 ISO_FORTRAN_ENV4 바이트 (32 비트) 정수, 예를 들면, 모듈 :

use ISO_FORTRAN_ENV
integer (int32) :: MyInt

gfortran 매뉴얼에는 "내재 모듈"아래에 문서가 있습니다.


종류가 무엇인지 한 가지 더 명시적인 설명입니다. 컴파일러에는 다양한 숫자 유형의 테이블이 있습니다. 모든 정수 타입은 다른 종류의 기본 유형의 - integer. 컴파일러에 1 바이트, 2 바이트, 4 바이트, 8 바이트 및 16 바이트 integer(또는 real) 종류 가 있다고 가정 해 보겠습니다 . 테이블에서 컴파일러는 이러한 종류 각각에 대한 인덱스를 가지고 있습니다.이 인덱스는 종류 번호입니다.

많은 컴파일러가이 번호 매기기를 선택합니다.

kind number    number of bytes
1              1
2              2
4              4
8              8
16             16

그러나 그들은 다른 번호 매기기를 선택할 수 있습니다. 명백한 가능성 중 하나는

kind number    number of bytes
1              1
2              2
3              4
4              8
5              16

이 접근 방식을 선택하는 컴파일러 (적어도 g77 및 NAG)가 있습니다. 이를 변경할 수있는 옵션도 있습니다. 따라서 kind숫자는 이식 가능하지 integer(kind=4)않거나 integer(4)컴파일러에 따라 4 바이트 정수 또는 8 바이트 정수를 의미합니다.

integer*4항상 4 바이트를 의미한다는 의미에서 이식성이 있습니다. 그러나 다른 한편으로는 표준의 일부가 아니기 때문에 이식성이 없습니다. 이 표기법을 사용하는 프로그램은 유효한 Fortran 77, 90 또는 다른 Fortran이 아닙니다.

종류 번호를 설정하는 방법에 대한 올바른 옵션을 보려면 MSB의 답변을 참조하십시오.

real데이터 유형 에도 동일한 개념이 적용 됩니다. Fortran 90 종류 매개 변수 (mataap의 답변)를 참조하세요 .


@SteveLionel이 최근에 작성한 이 계몽 기사를 참조하고 지금까지 다른 답변에없는 세부 정보를 다루려고합니다.


  1. 구문에 표시 integer*n또는 real*n다른 컴퓨터 아키텍처 정수와 실제 값의 메모리 포맷에 대해 서로 다른 디자인을 가지고 시작했을 때 오래 전, 컴파일러가 제공하는 일반적인 확장했다 n이었다 바이트 크기의 저장 값을. 그러나 그 값의 범위 나 정밀도에 대해서는 언급하지 않았습니다. 예를 들어 16 비트 정수의 다른 구현은 다른 범위와 제한 값을 제공 할 수 있습니다.

레지스터 크기는 8, 12, 16, 30, 32, 36, 48, 60 또는 64 비트가 될 수 있으며, 일부 CDC 시스템에는 1로 보완 된 정수가 있으며 (정수에 대해 마이너스 0을 허용합니다!), PDP-11 라인에는 여러 가지 부동이 있습니다. 시리즈에 따라 포인트 형식, IBM 360/370은 부동 소수점 등을위한 "16 진 정규화"를 가졌습니다. [...] 이러한 확장은 많은 프로그래머가이 구문이 표준 포트란이라고 생각하는 (그리고 오늘날까지도 많은 생각) ; 그렇지 않습니다!


  1. 포트란 (90)가 나왔을 때, kind매개 변수는 고유 조회 기능과 함께, 언어에 추가되었습니다 (특별히 kind, selected_int_kind그리고 selected_real_kind뿐만 아니라 다른 사람 같은 precision, digits, epsilon...)에 대한 minimun 요구 사항을 지정하는 프로그래머를 돕기 위해 정밀도와 범위 (숫자 유형을 여전히 스토리지 모델이나 바이트에 대한 공식적인 언급은 없습니다.) 구문은 integer(kind=n)심지어 integer(n)여기서 n컴파일러에 의해 지원되는 정수의 종류에 대응되는 상수 값이다. 리터럴 상수의 경우 구문은 12_n또는 3.4e-2_n입니다.

    The advantage of this solution was that Fortran didn't (and still don't) make any assumptions about the implementation details of data-types other than the results of the inquiry functions used to choose the type, so the code is parameterized by the problem being solved, not by the language or the hardware. The gotcha is, as said in other answers, each compiler can choose their kind numbers, thus assuming magic number like integer(4) is not portable.


  1. Also with Fortran 90 came the concept of default kinds, that is what you get when you don't specify a kind.

The default kinds were implementation dependent, though up through Fortran 2008 a compiler was required to support only one integer kind and two real kinds. (That's still true in Fortran 2018, but there's an added requirement that at least one integer kind support 18 decimal digits.) If you write a constant literal without a kind specifier, you got the default kind.


  1. With Fortran 2003 and the inclusion of the intrinsic module ieee_arithmetic, you can inquire and select a real type with IEEE floating point capabilities, if avaliable.

There are architectures where both IEEE and non-IEEE floating types are available, such as the HP (formerly Compaq formerly DEC) Alpha. In this case you can use IEEE_SELECTED_REAL_KIND from intrinsic module IEEE_ARITHMETIC to get an IEEE floating kind. And what if there is no supported kind that meets the requirements? In that case the intrinsics return a negative number which will (usually, depending on context) trigger a compile-time error.


  1. Finally, Fortran 2003 brought the iso_fortran_env intrinsic module, that had functions to inquire the storage size of the types implemented by a compiler, with intrinsics like numeric_storage_size and bit_size. Another addition of Fortran 2003 revision was the iso_c_binding intrinsic module, that provided kind parameter values to guarantee compatibility with C types, in storage, precision and range.

Intrinsic module ISO_C_BINDING declares constants for Fortran types that are interoperable with C types, for example C_FLOAT and C_INT. Use these if you're declaring variables and interfaces interoperable with C.


  1. As a final note, I will mention the recent Fortran 2008 Standard, that extended intrinsic module iso_fortran_env to include named constants int8, int16, int32m int64, real32, real64 and real128, whose values correspond to the kinds of integer and real kinds that occupy the stated number of bits. The gotcha is that those constants only assure storage size, not precision or range. Only use them when this is exactly what you want.

In my view, this is little better than the old *n extension in that it tells you that a type fits in that many bits, but nothing else about it. As an example, there's one compiler where REAL128 is stored in 128 bits but is really the 80-bit "extended precision" real used in the old x86 floating point stack registers. If you use these you might think you're using a portable feature, but really you're not and may get bitten when the kind you get doesn't have the capabilities you need.

ReferenceURL : https://stackoverflow.com/questions/3170239/fortran-integer4-vs-integer4-vs-integerkind-4

반응형