CEILING和FLOOR函数在Fortran中 - 是不是应该返回INTEGERS? [英] CEILING and FLOOR function in Fortran - aren't they supposed to return INTEGERS?

查看:1671
本文介绍了CEILING和FLOOR函数在Fortran中 - 是不是应该返回INTEGERS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个关于Fortran 90/95中的 CEILING FLOOR 函数的简短问题。



根据文档: https://gcc.gnu .org / onlinedocs / gfortran / CEILING.html



我的印象是它们接受REALs,但返回INTEGER。然而,作为一个例子,对于这个简单的程序,我得到:

 程序示例
REAL :: X = - 3.4
nintx = NINT(x)
ceilx = CEILING(X)
floorx = FLOOR(X)
WRITE(*,*)nintx,ceilx,floorx
END PROGRAM

我得到-3,-3.00000和-4.00000。但基于文档,所有返回类型都是INTEGER。那么为什么小数点和尾随零显示 CEILING FLOOR 结果?

解决方案

CEILING 和 FLOOR 结果。但是,您并未打印这些结果:您正在打印真实类型的变量 celix floorx



这些变量是真实的,因为隐式输入。与 nintx 确实是一个整数变量。



列表控制的输出( write(*,*)部分)自然会得到格式化真实变量。 (*,*)NINT(x),CEILING(X),FLOOR(x), X)

您不会感到惊讶。

显而易见的是:不要使用隐式类型。作为第二行 implicit none


A quick question regarding the CEILING and FLOOR functions in Fortran 90/95.

Based on the documentation: https://gcc.gnu.org/onlinedocs/gfortran/CEILING.html

My impression is that they take in REALs, but return INTEGERs. However, as an example, for this simple program, I get:

PROGRAM example
REAL:: X=-3.4
nintx = NINT(x)
ceilx = CEILING(X)
floorx = FLOOR(X)
WRITE(*,*) nintx, ceilx, floorx
END PROGRAM

I get -3, -3.00000 and -4.00000. But based on the documentation all return types are INTEGERs. So why are the CEILING and FLOOR results displayed with the decimal point and trailing zeros?

解决方案

CEILING and FLOOR do return integer results. However, you are not printing those results: you are printing the variables celix and floorx which are of real type.

Those variables are real because of implicit typing. Contrast this with nintx which is indeed an integer variable.

List-directed output (the write(*,*) part) has as natural result formatting the real variables as you see. If you instead directly print the function results

write(*,*) NINT(x), CEILING(X), FLOOR(X)

you will be less surprised.

The obvious thing to say is: don't use implicit typing. Have as your second line implicit none.

这篇关于CEILING和FLOOR函数在Fortran中 - 是不是应该返回INTEGERS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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