Fortran 中是否有等效的 MATLAB dec2bin 函数?
Is there an equivalent MATLAB dec2bin function in Fortran?
我想在 MATLAB 中找到与 dec2bin(d,n)
类似的函数,它为 Fortran 生成至少具有 n
位的二进制表示。我在网上找到了this code,它可以将十进制转换为二进制,但没有决定位数的机制,它要求用户决定它是正整数还是正实数...
这是一个基于 B
编辑运算符的解决方案(根据 High Performance Mark 的评论),它需要相当新的编译器。
!> Returns a binary representation of d as a string with at least n bits.
!> abs(d) < 2^52
function dec2bin(d, n)
implicit none
integer,intent(in) :: d
integer,intent(in),optional :: n
character(len=:),allocatable :: dec2bin
character(len=53) :: tmp
integer :: n_
character(len=8) :: f
if (present(n)) then
n_ = min(n, 53)
write(f,'(i2)') n_
f = '(B' // trim(adjustl(f)) // '.' // trim(adjustl(f)) // ')'
else
f = '(B53)'
endif
write(tmp,f) d
dec2bin = trim(adjustl(tmp))
end function
请注意,此函数不检查标志(因为标志已正确处理)。如果你想将它限制为正整数,你需要在函数之外进行。
因为我必须自己寻找正确的语法,这里是 Fortran 2008 Standard, Cl 的相关部分。 10.7.2.4 "B, O, and Z editing":
1 The Bw , Bw .m, Ow , Ow .m, Zw , and Zw .m edit descriptors indicate that the field to be edited occupies w
positions, except when w is zero. When w is zero, the processor selects the field width. On input, w shall not be
zero. The corresponding input/output list item shall be of type integer, real, or complex.
[...]
6 The output field for the Bw .m, Ow .m, and Zw .m edit descriptor is the same as for the Bw, Ow, and Zw edit descriptor, except that the digit-string or hex-digit-string consists of at least m digits. If necessary, sufficient
leading zeros are included to achieve the minimum of m digits. The value of m shall not exceed the value of w ,
except when w is zero. [...]
我想在 MATLAB 中找到与 dec2bin(d,n)
类似的函数,它为 Fortran 生成至少具有 n
位的二进制表示。我在网上找到了this code,它可以将十进制转换为二进制,但没有决定位数的机制,它要求用户决定它是正整数还是正实数...
这是一个基于 B
编辑运算符的解决方案(根据 High Performance Mark 的评论),它需要相当新的编译器。
!> Returns a binary representation of d as a string with at least n bits.
!> abs(d) < 2^52
function dec2bin(d, n)
implicit none
integer,intent(in) :: d
integer,intent(in),optional :: n
character(len=:),allocatable :: dec2bin
character(len=53) :: tmp
integer :: n_
character(len=8) :: f
if (present(n)) then
n_ = min(n, 53)
write(f,'(i2)') n_
f = '(B' // trim(adjustl(f)) // '.' // trim(adjustl(f)) // ')'
else
f = '(B53)'
endif
write(tmp,f) d
dec2bin = trim(adjustl(tmp))
end function
请注意,此函数不检查标志(因为标志已正确处理)。如果你想将它限制为正整数,你需要在函数之外进行。
因为我必须自己寻找正确的语法,这里是 Fortran 2008 Standard, Cl 的相关部分。 10.7.2.4 "B, O, and Z editing":
1 The Bw , Bw .m, Ow , Ow .m, Zw , and Zw .m edit descriptors indicate that the field to be edited occupies w positions, except when w is zero. When w is zero, the processor selects the field width. On input, w shall not be zero. The corresponding input/output list item shall be of type integer, real, or complex.
[...]
6 The output field for the Bw .m, Ow .m, and Zw .m edit descriptor is the same as for the Bw, Ow, and Zw edit descriptor, except that the digit-string or hex-digit-string consists of at least m digits. If necessary, sufficient leading zeros are included to achieve the minimum of m digits. The value of m shall not exceed the value of w , except when w is zero. [...]