FORTRAN: Problem with converting reals to integers

In summary, the conversation is about a person trying to convert real number data into integer data for use in a do loop. The problem they are facing is that the converted numbers are being recognized as non-scalar integers. They also discuss issues with formatting and converting data types in their program.
  • #1
mattmac.nuke
22
0

Homework Statement


I'm trying to convert data that's entered as a real number into integer data to be used in a do loop. The problem is that it keeps telling me that the numbers I've just converted are not scalar integers...


The Attempt at a Solution



program interest
IMPLICIT NONE
REAL :: r !Monthly interest rate
REAL :: pay !Montyly Payment Amount
REAL :: p !Initial loan amount (Money borrowed)
INTEGER :: n, i, j !Total number of payments to be made
REAL :: i_max !Maximum Interest rate
REAL :: i_min !Minimum Interest rate
REAL :: t_max !Maximum amount of Time for Loan payback
CHARACTER :: x



WRITE (*,*) 'COP 2271- Project 2'
WRITE (*,*) ' Interest rates '
WRITE (*,*) '-------------------------'
WRITE (*,*) 'Main Menu'
WRITE (*,*) ' A) Enter new Data'
WRITE (*,*) ' Q) Quit'
READ (*,*) x

DO
SELECT CASE (x)
CASE ('A','a')
DO
WRITE (*,*) 'Enter principle loan amount($)'
READ (*,*) p
WRITE (*,*) 'Enter maximum length of the loan(Years):'
READ (*,*) t_max
WRITE (*,*) 'Enter minimum interest rate(%):'
READ (*,*) i_min
WRITE (*,*) 'Enter maximum interest rate(%):'
READ (*,*) i_max
END DO
CASE ('Q','q')
EXIT
CASE DEFAULT
WRITE (*,*) 'Invalid Selection'
END SELECT

IF (p < 0) WRITE (*,*) 'ERROR- Principle amount must be positive!'
IF (t_max < 3) WRITE (*,*) 'ERROR- Maximum loan length must be greater than or equal to 3 (years)!'
IF (i_min < 0 .OR. i_min > 100 .OR. i_min > i_max) WRITE (*,*) 'Error- Interest rate must be between 0 and 100!'
IF (i_max < 0 .OR. i_max > 100) WRITE (*,*) 'Error- Interest rate must be between 0 and 100!'

END DO

i_max = CEILING(i_max)
i_min = INT(i_min)
t_max = CEILING(t_max)

DO i = 2, t_max
n = 12*i
pay = (r*p)/(1-((1 + r)**(-n)))

DO j = i_min, i_max

WRITE (*,'(3X,I3,8X,I3,5X,A,F10.2, 5X,A, F10.2)')


END DO
END DO


end program interest


-This is the program as I've written it so far, it's not finished... I'm also having problems with the formatting they want us to use for the tables.
 
Physics news on Phys.org
  • #2
I don't think you can declare i_min as a REAL and then expect the type to convert to INTEGER after executing the statement: i_min = INT (i_min). You should assign the converted value of i_min to another variable of TYPE INTEGER.
 

Related to FORTRAN: Problem with converting reals to integers

1. What is FORTRAN?

FORTRAN (short for Formula Translation) is a programming language commonly used in scientific and engineering applications. It was first developed in the 1950s and has been used extensively for numerical calculations and scientific computing.

2. What is the problem with converting reals to integers in FORTRAN?

The problem with converting reals (floating-point numbers) to integers in FORTRAN is that the precision of the real number may be lost. This is because integers can only represent whole numbers, while real numbers can have decimal values. Thus, some rounding may occur during the conversion process, resulting in potential loss of accuracy.

3. How does FORTRAN handle real to integer conversions?

In FORTRAN, real to integer conversions are typically handled by truncation. This means that the decimal portion of the real number is simply dropped, and the resulting integer is the whole number part of the original real number. This can lead to potential loss of precision, as mentioned before.

4. Are there any alternative methods for converting reals to integers in FORTRAN?

Yes, there are alternative methods for converting reals to integers in FORTRAN. One method is to use the INT function, which rounds the real number to the nearest integer. Another method is to use the NINT function, which rounds the real number to the nearest integer, with ties being rounded to the nearest even integer.

5. How can I avoid problems with converting reals to integers in FORTRAN?

To avoid potential loss of accuracy when converting reals to integers in FORTRAN, it is recommended to use the NINT function or to manually round the real number using the ROUND function before converting it to an integer. It is also important to be aware of the limitations of integers and reals and to carefully consider the precision needed for your calculations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Programming and Computer Science
Replies
4
Views
967
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
6K
Back
Top