Fortran Coder

查看: 6434|回复: 4

[讨论] 如何判断两个无限多态变量相等

[复制链接]

125

帖子

34

主题

1

精华

大师

F 币
1130 元
贡献
587 点
发表于 2019-9-26 20:36:03 | 显示全部楼层 |阅读模式
本帖最后由 weixing1531 于 2019-9-27 10:40 编辑

这种方法好像很麻烦
[Fortran] 纯文本查看 复制代码
program main
	implicit none
	write(*,*)EqualClass(1,2)
	write(*,*)EqualClass(1,1)
	write(*,*)EqualClass(1.0,1.001)
	write(*,*)EqualClass(1.0,1.0)
	write(*,*)EqualClass('abc','abcd')
	write(*,*)EqualClass('abc','abc')
	write(*,*)EqualClass(.true.,.false.)
	write(*,*)EqualClass(.true.,.true.)
contains
pure logical function EqualClass(self, other)
	class(*), intent(in) :: self
	class(*), intent(in) :: other
	EqualClass=.false. !初值为假

	select type(self)
		type is(Integer)
			select type(other)
				type is(Integer)
					EqualClass=(self==other)
			end select
		type is(Real)
			select type(other)
				type is(Real)
					EqualClass=(abs(self-other)<1.0e-5)
			end select
		type is(Character(*))
			select type(other)
				type is(Character(*))
          EqualClass=(self==other)
			end select
		type is(Logical)
			select type(other)
				type is(Logical)
					EqualClass=(self.eqv.other)
			end select
	end select
end function
end program

953

帖子

0

主题

0

精华

大师

F 币
180 元
贡献
73 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
发表于 2019-9-27 09:15:13 | 显示全部楼层
我觉得不太可能吧,不同的数据类型,比较相等的操作不一样呀。
比如 logical ,非0都认为是真~~那么 0x01 和 0xFF 被认为是相等的。
还有 real 类型,要通过比较两者的差的绝对值,才有相等的意义。

130

帖子

10

主题

0

精华

大师

F 币
617 元
贡献
372 点

贡献勋章管理勋章帅哥勋章元老勋章星光勋章规矩勋章

发表于 2020-7-18 22:24:35 | 显示全部楼层
是很麻煩, 更麻煩得還有 KIND/RANK

桌布(wrapper)一擈就清爽了, 前人種樹後人乘涼

不考慮有效位數問題, 全對應(TRANSFER?)到 INTEGER*1 array, 一翻兩瞪眼

178

帖子

15

主题

0

精华

大宗师

F 币
4973 元
贡献
1152 点
发表于 2020-7-19 03:43:39 | 显示全部楼层
vvt 发表于 2019-9-27 09:15
我觉得不太可能吧,不同的数据类型,比较相等的操作不一样呀。
比如 logical ,非0都认为是真~~那么 0x01  ...

哈哈,这个时候就看出OOP的好处了
所有的判断是否相等,都是在类内自我实现的,不用这么费劲再写这样的通用函数

79

帖子

17

主题

0

精华

专家

齊天大聖

F 币
433 元
贡献
266 点
发表于 2020-8-3 15:07:06 | 显示全部楼层
liudy02 发表于 2020-7-19 03:43
哈哈,这个时候就看出OOP的好处了
所有的判断是否相等,都是在类内自我实现的,不用这么费劲再写这样的通 ...

有点东西
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-3-29 21:01

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表