简体   繁体   English

Fortran中的浮点运算

[英]Floating point arithmetic in Fortran

I've inherited some Fortran code that I'm trying to make sense of. 我继承了一些我想理解的Fortran代码。 It uses REAL variables in many places that, I think, it shouldn't - but maybe I'm misunderstanding how this works in Fortran (as compared to C++ which I'm much more familiar with), hence this question. 我认为它不应该在许多地方使用REAL变量-但也许我误解了它在Fortran中的工作原理(与我更熟悉的C ++相比),因此出现了这个问题。

So the variables in question are essentially 'categorical values', 'factors' or 'enums' depending on how you look at it/want to call it. 因此,所涉及的变量本质上是“分类值”,“因素”或“枚举”,具体取决于您如何看待/想要调用它。 They are of data type REAL and can only take on a finite number of pre-determined, integer values. 它们的数据类型为REAL,并且只能采用有限数量的预定整数值。 So say variable a can only be of value 1, 2 or 3. These values are read in from external files; 假设变量a只能是值1、2或3。这些值是从外部文件中读取的; in these external files, they are represented as integers, so it's not a case of 'rounding issues in external data sources' or something. 在这些外部文件中,它们表示为整数,因此不是“外部数据源中的舍入问题”或其他情况的情况。

However in the code, it never does a straight comparison, always a greater than/lower than check. 但是,在代码中,它从不进行直接比较,始终大于或小于检查。 So, instead of doing 因此,与其做

if (a == 1) then

it does 它确实

if (a > 0.9 .and. a < 1.1) then

You can imagine that this gets very confusing/tiresome to read, especially when it needs to check if a value is one of multiple categories. 您可以想象,这变得非常令人困惑/阅读起来很麻烦,尤其是当它需要检查某个值是否为多个类别之一时。

So I think this is a case where someone at some point heard 'never compare REAL values' (because of the nature of the finite precision of storing floating point values, this same problem exists in every programming language), but then didn't really understand when that applies (I guess the first error is that categorical values should have been represented as integer values but that situation is what it is for now). 因此,我认为在这种情况下,某个人有时会听到“从不比较REAL值”(由于存储浮点值的有限精度的本质,每种编程语言都存在相同的问题),但实际上并没有了解什么时候适用(我猜第一个错误是分类值应该已经表示为整数值,但目前情况是这样)。

OTOH maybe I'm just misunderstanding how REAL and INTEGER values are represented and work in Fortran? OTOH也许我只是误解了REAL和INTEGER值如何在Fortran中表示和工作? Could there ever be a case where 可能会有情况

b = 1.5
a = REAL(INT(b))

if (a > 0.9 .and. a < 1.1) then

would make sense? 会有意义吗?

ONLY in the case you're not performing any operation with the real values (just assigning a value and comparing equality with the same literal you assigned, with same kind parameter), you won't need tolerance. 仅在不对实际值执行任何操作的情况下(只需分配一个值并将相等性与所分配的相同文字和相同种类参数进行比较),就不需要公差。

The thing is, for a real variable a such as: 问题是,对于一个实数变量,例如:

Real a
a = 2

You can be sure that 您可以确定

a == 2

Will be always .true. 永远是.true. . But eg, for another real value represented by b: 但是例如,对于由b表示的另一个实数值:

a  / b * b == 2

(Or any other operation) is not guaranteed to be .true. (或任何其他操作)不能保证为.true。

If for some reason the variable has to stay REAL then you may use the intrinsic Fortran function NINT (nearest integer) in the comparisons: 如果由于某种原因该变量必须保持实数,则可以在比较中使用固有的Fortran函数NINT(最近整数):

if( nint(a) == 1 ) then 
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM