Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 867
JackReacher Avatar asked Dec 11 '22 00:12

JackReacher


1 Answers

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.

like image 124
francescalus Avatar answered May 03 '23 11:05

francescalus