CEILING and FLOOR function in Fortran - aren't they supposed to return INTEGERS?

99封情书 提交于 2019-12-10 07:06:04

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29378282/ceiling-and-floor-function-in-fortran-arent-they-supposed-to-return-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!