TRUE的数值等效值为-1? [英] Numerical equivalent of TRUE is -1?

查看:123
本文介绍了TRUE的数值等效值为-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2012中使用Intel Fortran来编译Fortran代码.当我尝试使用逻辑运算符时,我注意到一个独立的逻辑表达式会按预期产生T或F.但是,如果我需要数字T或F(0或1),则逻辑结果为T时,我将得到-1.

I am using Intel Fortran in Visual Studio 2012 to compile a Fortran code. When I try to use logical operators I have noticed that a standalone logical expression results in T or F as expected. However, if I need the numerical T or F (0 or 1), I get a -1 when logical result is T.

例如:

integer*4 a
a = 1
logicval = (node(5,L).gt.0)
numval =  1*(node(5,L).gt.0)
write(*,*) logicval, numval

将输出

T,-1

有没有一种方法可以重新定义分配给T&的数值?F?

Is there a way in which I can redefine numerical values assigned to T & F?

推荐答案

是的,因为所有位都设置为1,所以可以预期Intel Fortran的TRUE确实为-1.

Yes, that is expected Intel Fortran's TRUE is indeed -1, because all bits are set to 1.

此外,将整数用作逻辑(布尔)变量是完全不标准的.您应该将代码延长几行,并始终进行正确的转换.您的 integer_var = 1 *(node(5,L).gt.0)在Fortran中是不允许的,并且将被许多编译器拒绝.如果您放下 1 * ,gfortran将发出警告,但您的表单将导致错误.

In addition, your use of integers as logical (Boolean) variables is completely non-standard. You should make your code a few lines longer and always do proper conversion. Your integer_var = 1*(node(5,L).gt.0) is not allowed in Fortran and it will be refused by many compilers. If you drop the 1* gfortran will issue a warning, but your form results in an error.

您可以简单地以标准的方式转换逻辑和整数

You can simply convert your logicals and integers in a standard conforming way

   if (l) then
     x = 1
   else
     x = 0
   end if

您可以使用本征 MERGE()或使用 WHERE 轻松地将Fortran逻辑数组转换为1和0的整数数组.

You can convert a Fortran logical arrays to integer arrays with 1 and 0 easily using the MERGE() intrisic or using WHERE.

Intel Fortran的一个简单修补程序可能是Intel Fortran的 -fpscomp逻辑,它切换了对 LOGICAL 类型的处理,以将非零值视为真,并使.true.常量等于整数1.

An easy fix for Intel Fortran is probably -fpscomp logicals for Intel Fortran which switches the treatment of LOGICAL type to consider anything nonzero as true and makes the .true. constant to be equivalent to integer 1.

仍然要小心,因为它不能使您的程序具有可移植性,它只能解决一种特定编译器中的一个可移植性问题.

Still be careful because it does not make your program portable, it just works around one portability issue in one particular compiler.

您可以使用C互操作逻辑类型将定义与Intel C表示形式相匹配:

You can use the C interoperable logical kind to match the definition to the Intel C representation:

use iso_c_binding

logical(c_bool) :: variable

如C99规定的_Bool一样,这些值的值分别为+1和0.如果您需要它们与 int 互操作,则必须进行一些简单的转换. C_int c_bool 不兼容.

These will have values of +1 and 0 as C99 dictates for _Bool. If you need them to be interoperable with int you must do some simple conversion. C_int is not compatible with c_bool.

根据英特尔编译器的版本,您可能需要使用-标准语义(或 -fpscomp逻辑)来获得正确的 logical(c_bool).我认为这很不幸.

Depending on the version of Intel Compiler you may need to use -standard-semantics (or -fpscomp logicals) for correct logical(c_bool). I consider this to be very unfortunate.

这篇关于TRUE的数值等效值为-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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