How to resolve the algorithm Test integerness step by step in the Fortran programming language
How to resolve the algorithm Test integerness step by step in the Fortran programming language
Table of Contents
Problem Statement
Mathematically,
This means that each of those larger sets, and the data types used to represent them, include some integers. Given a rational, real, or complex number of any type, test whether it is mathematically an integer. Your code should handle all numeric data types commonly used in your programming language. Discuss any limitations of your code. For the purposes of this task, integerness means that a number could theoretically be represented as an integer at no loss of precision (given an infinitely wide integer type). In other words: Optionally, make your code accept a tolerance parameter for fuzzy testing. The tolerance is the maximum amount by which the number may differ from the nearest integer, to still be considered an integer. This is useful in practice, because when dealing with approximate numeric types (such as floating point), there may already be round-off errors from previous calculations. For example, a float value of 0.9999999998 might actually be intended to represent the integer 1. (The types and notations shown in these tables are merely examples – you should use the native data types and number literals of your programming language and standard library. Use a different set of test-cases, if this one doesn't demonstrate all relevant behavior.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Test integerness step by step in the Fortran programming language
Source code in the fortran programming language
MODULE ZERMELO !Approach the foundations of mathematics.
CONTAINS
LOGICAL FUNCTION ISINTEGRAL(X) !A whole number?
REAL*8 X !Alas, this is not really a REAL number.
INTEGER*8 N !Largest available.
IF (ISNAN(X)) THEN !Avoid some sillyness.
ISINTEGRAL = .FALSE. !And possible error messages.
ELSE !But now it is safe to try.
N = KIDINT(X) !This one truncates.
ISINTEGRAL = N .EQ. X !Any difference?
END IF !A floating-point number may overflow an integer.
END FUNCTION ISINTEGRAL !And even if integral, it will not seem so.
LOGICAL FUNCTION ISINTEGRALZ(Z) !For complex numbers, two tests.
DOUBLE COMPLEX Z !Still not really REAL, though.
ISINTEGRALZ = ISINTEGRAL(DBLE(Z)) .AND. ISINTEGRAL(DIMAG(Z)) !Separate the parts.
END FUNCTION ISINTEGRALZ!No INTEGER COMPLEX type is offered.
END MODULE ZERMELO !Much more mathematics lie elsewhere.
PROGRAM TEST
USE ZERMELO
DOUBLE COMPLEX Z
WRITE (6,*) "See if some numbers are integral..."
WRITE (6,*) ISINTEGRAL(666D0),666D0
Z = DCMPLX(-3D0,4*ATAN(1D0))
WRITE (6,*) ISINTEGRALZ(Z),Z
END
X = 1
10 X = X*BASE
Y = X + 1
D = Y - X
IF (D .EQ. 1) GO TO 10
MODULE ZERMELO !Approach the foundations of mathematics.
INTERFACE ISINTEGRAL !And obscure them with computerese.
MODULE PROCEDURE ISINTEGRALF4, ISINTEGRALF8,
1 ISINTEGRALZ8, ISINTEGRALZ16
END INTERFACE !Selection is by parameter type and number.
CONTAINS !Sop, now for a grabbag of routines.
LOGICAL FUNCTION ISINTEGRALF8(X) !A whole number?
REAL*8 X !Alas, this is not really a REAL number.
INTEGER*8 N !Largest available.
INTEGER*8 BIG !The first number too big to have any fractional digits in floating-point.
PARAMETER (BIG = RADIX(X)**(DIGITS(X) - 1)) !These "functions" are in fact constants.
IF (ISNAN(X)) THEN !Avoid some sillyness.
ISINTEGRALF8 = .FALSE. !And possible error messages.
ELSE IF (ABS(X).GE.BIG) THEN !But now it is safe to try.
ISINTEGRALF8 = .TRUE. !Can't have fractional digits => integral.
ELSE !But smaller numbers can have fractional digits.
N = KIDINT(X) !So, truncate to an integral value.
ISINTEGRALF8 = N .EQ. X !Any difference?
END IF !So much for inspection.
END FUNCTION ISINTEGRALF8 !No need to look at digit sequences.
LOGICAL FUNCTION ISINTEGRALF4(X) !A whole number?
REAL*4 X !Alas, this is not really a REAL number.
INTEGER*4 N !Largest available.
IF (ISNAN(X)) THEN !Avoid some sillyness.
ISINTEGRALF4 = .FALSE. !And possible error messages.
ELSE IF (ABS(X) .GE. RADIX(X)**(DIGITS(X) - 1)) THEN !Constant results as appropriate for X.
ISINTEGRALF4 = .TRUE. !Can't have fractional digits => integral.
ELSE !But smaller numbers can have fractional digits.
N = INT(X) !So, truncate to an integral value.
ISINTEGRALF4 = N .EQ. X !Any difference?
END IF !A real*4 should not overflow INTEGER*4.
END FUNCTION ISINTEGRALF4 !Thanks to the size check.
LOGICAL FUNCTION ISINTEGRALZ8(Z) !For complex numbers, two tests.
COMPLEX Z !Still not really REAL, though.
ISINTEGRALZ8 = ISINTEGRAL(REAL(Z)) .AND. ISINTEGRAL(AIMAG(Z)) !Separate the parts.
END FUNCTION ISINTEGRALZ8 !No INTEGER COMPLEX type is offered.
LOGICAL FUNCTION ISINTEGRALZ16(Z) !And there are two sorts of complex numbers.
DOUBLE COMPLEX Z !Still not really REAL.
ISINTEGRALZ16 = ISINTEGRAL(DBLE(Z)) .AND. ISINTEGRAL(DIMAG(Z)) !Separate the parts.
END FUNCTION ISINTEGRALZ16 !No INTEGER COMPLEX type is offered.
END MODULE ZERMELO !Much more mathematics lie elsewhere.
PROGRAM TEST
USE ZERMELO
DOUBLE COMPLEX Z
DOUBLE PRECISION X
REAL U
Cast forth some pearls.
WRITE (6,1) 4,DIGITS(U),RADIX(U)
WRITE (6,1) 8,DIGITS(X),RADIX(X)
1 FORMAT ("REAL*",I1,":",I3," digits, in base",I2)
WRITE (6,*) "See if some numbers are integral..."
WRITE (6,*) ISINTEGRAL(666D0),666D0
WRITE (6,*) ISINTEGRAL(665.9),665.9
Z = DCMPLX(-3D0,4*ATAN(1D0))
WRITE (6,*) ISINTEGRAL(Z),Z
END
You may also check:How to resolve the algorithm Find common directory path step by step in the Nim programming language
You may also check:How to resolve the algorithm File extension is in extensions list step by step in the Go programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the E programming language
You may also check:How to resolve the algorithm Assertions step by step in the Oz programming language
You may also check:How to resolve the algorithm Add a variable to a class instance at runtime step by step in the Ruby programming language