What's Wrong with My Monte Carlo Calculation of π Using Fortran?

AI Thread Summary
The discussion centers on a coding issue related to estimating the value of Pi using a Monte Carlo method in Fortran. The user is experiencing unexpected results, specifically an approximation of 12, which is too high. Key points include the incorrect calculation of the variable A, which should be defined as A=4*(COUNTER_CIRCULE / N) instead of A=4*(COUNTER_CIRCULE / COUNTER_SQUARE). This correction addresses the issue of integer division versus floating-point division, which can lead to erroneous results if not handled properly. The conversation also highlights the importance of understanding type conversion in programming languages like Fortran. The user ultimately resolves the confusion by revisiting the mathematical concepts involved.
Taylor_1989
Messages
400
Reaction score
14
Hi guys, I am having trouble seeing where I have actually gone wrong with my code. If I run the code I am getting approximation of 12 as my lowest is way to big. But I am really struggling to find where I have gone wrong. Any advice would be appreciated.

Fortran:
PROGRAM assign_10_1

IMPLICIT NONE

INTEGER, DIMENSION (1:12) :: SEED

REAL:: A,X,Y

INTEGER:: I, N, COUNTER_CIRCULE, COUNTER_SQUARE

COUNTER_CIRCULE=0

COUNTER_SQUARE=0

N=100000

WRITE(*,*)'please seclect 12 numbers for your seed'

READ(*,*) SEED

WRITE(*,*) SEED

 CALL RANDOM_SEED(PUT=SEED)

DO I=1,N
   CALL RANDOM_NUMBER(X)
   CALL RANDOM_NUMBER(Y)
   WRITE(*,*) X,Y

   IF (X**2+Y**2 < 1) THEN
   COUNTER_CIRCULE=COUNTER_CIRCULE+1
   ELSE
   COUNTER_SQUARE=COUNTER_SQUARE+1
   END IF

END DO

A=4*(COUNTER_CIRCULE/COUNTER_SQUARE)

WRITE(*,*)'ESTIMATION FOR VALUE OF PI', A
END PROGRAM
 
Technology news on Phys.org
Your counter_square is not what you think it is...
 
Taylor_1989 said:
A=4*(COUNTER_CIRCULE/COUNTER_SQUARE)
A=4*(COUNTER_CIRCULE / N)
 
Ok thanks guys. I got it. I just went back of the math with a bit of paper and it clicked in what you are saying. Thanks once again.
 
Baluncore said:
A=4*(COUNTER_CIRCULE / N)
Not mentioned is the fact that if COUNTER_CIRCULE is 1000, then A will be assigned the value 0. In Fortran, C, C++, and many other programming languages, there are two types of division: integer division and floating point division. So for example, 2/5 is 0 while all three of the following expressions evaluate to 0.4 or something close to it.
  • 2.0 / 5.0
  • 2 / 5.0
  • 2.0 / 5
See http://www.oc.nps.edu/~bird/oc3030_online/fortran/basics/basics.html, in Type Conversion and Mixed-Mode Arithmetic.

BTW there is no such word as "circule" in English.
 
  • Like
Likes Taylor_1989 and BvU
Mark44 said:
Not mentioned is the fact that if COUNTER_CIRCULE is 1000, then A will be assigned the value 0. In Fortran, C, C++, and many other programming languages, there are two types of division: integer division and floating point division. So for example, 2/5 is 0 while all three of the following expressions evaluate to 0.4 or something close to it.
  • 2.0 / 5.0
  • 2 / 5.0
  • 2.0 / 5
See http://www.oc.nps.edu/~bird/oc3030_online/fortran/basics/basics.html, in Type Conversion and Mixed-Mode Arithmetic.

BTW there is no such word as "circule" in English.
Thanks for the advice. much appreciated
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
1
Views
4K
Replies
3
Views
2K
Replies
2
Views
1K
Back
Top