开发者

Fortran string comparison with .LT

开发者 https://www.devze.com 2023-01-12 14:37 出处:网络
I have to fix a bug in a very old and large financial system that uses Fortran, C and C++. I am primarily a C++ guy, have no idea abt Fortran! I have a problem understanding a Fortran statement which

I have to fix a bug in a very old and large financial system that uses Fortran, C and C++. I am primarily a C++ guy, have no idea abt Fortran! I have a problem understanding a Fortran statement which i think has caused a nasty bug in our systems....

if (instructions .lt. ' ')  then
    instructions = ' '
endif

instructions is a text/string.

How开发者_如何学Go does the above code behave, does it compare only the first character (atleast my tests suggest)? Basically this is a production issue, I am trying to give a workaround to my clients. A correct Fortran code to compare strings filed would also do...

Thanks in advance


 if (instructions .lt. ' ')  then 

.lt. is Fortram means "less than", so, if instructions is "less than" a space, replace it with a space. It will consider the entire string, but since the right-hand-side is just a single space, only needs to look at the first character of instructions; If the first character is less than a space (i.e., it's a control-char, if we are talking about ASCII), than it's less than; if the first chanracter is a space or greater (i.e. a printable character), than it's not less-than.


The verified answer is unfortunately incorrect: comparison will follow the full length of 'instructions', i.e. if the first character of instruction is a space the second one might still contain a character comparing less than to the (implicit) space, because the second operand will be padded with spaces to the length of 'instructions'. In that case the comparison will still evaluate to .true..

A correct code answer is impossible from the question as stated because the intention what needs to be compared is unclear.

One can interpret the question a full length comparison being sufficient, which the code as posted does.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号