Wa_english_title: "Calculating the Value of Pi using a Monte Carlo Method", Wa_curated: "curated:donotuseinexternalfilters/productdocumentation", Wa_emtoperatingsystem: "emtoperatingsystem:linux,emtoperatingsystem:microsoftwindows,emtoperatingsystem:macos", Wa_rsoftware: "rsoftware:componentsproducts/intelfortrancompiler", Wa_emttechnology: "emttechnology:inteltechnologies/oneapi", Wa_emtcontenttype: "emtcontenttype:training/instructionalmaterials/tutorial", Wa_emtprogramminglanguage: "emtprogramminglanguage:fortran", For a more detailed explanation of this technique, see Count the number of random points that are within the circle and divide that by the total number of points the result is π/4. If X 2+Y 2 (distance between the point and the origin) is less than or equal to 1, the point is within the circle. To make this easier, we’ll use the center of the square as the origin and generate random values for X and Y coordinates between zero and 1, so we’re looking at a quarter of the circle/square. But how do we get the area of the circle? What we can do is pick random points on the paper and count the points that are within the circle. The area of the circle is πr 2, but since the radius (r) is 1, and we know the square’s area is 4 (2x2), the ratio of the circle’s area to that of the square’s area is π/4. On this paper draw a circle whose diameter is two units (radius one unit). Imagine a square piece of paper two units across (the actual unit doesn’t matter). ![]() what took 60 hours on an Apple II took just under 4 seconds now on my seven-years-old laptop = 54000 times faster = 1.45x speedup every year which seems a pretty good match with Moore's Law.We will be using a program that calculates the value of the mathematical constant π (Pi) using a Monte Carlo method, so named because it uses random numbers. I just did a search now and found this readable page (many pages aren't :-/ ) which I translated in to BASIC and. I ran a program from a magazine to calculate pi, and over a weekend I got two full pages of digits (~8000), of which at least the first 100 were correct when compared to the encyclopaedia. Way back when I was a teenager, my school had an Apple II. Obviously there is a speed hit, though, eg, if you multiply two 10,000 digit numbers, then that is 100 million digit-times-digit multiplications, although any decent library would be doing it in chunks of digits eg 32 bit operations mean you can add 9 decimal digits at a time. ![]() I haven't seen any native ones for B4X, but presumably a Java one could be used. There are math packages around for doing big-number calculations, usually effectively limited only by the available memory. I remember TurboPascal had them, and more recently, that Microsoft BASICs have a CURRENCY type, which were 64 bit integers divided by 10000 to give 4 decimal places (2 for cents, and 2 for just-in-case )īig-number applications like your 100-digit pi calculation, are not going to work with double-precision (~15 decimal digits) or even quadruple-precision (~33 decimal digits) numbers, regardless of whether they are in binary or decimal. Some languages have fixed-point or decimal floating-point (aka BCD = binary coded decimal) numbers, specifically for handling financial values. Double-precision gives 52 binary digits of precision = approximately 15 decimal digits.Īccounting applications can usually get away with using double-precision numbers - 15 decimal digits - say 2 guard digits for rounding leaves 13 usable digits = 11 digits for dollars and 2 digits for cents = good for values up to $100 billion accurate to the last cent. Single-precision floats give 24 binary digits of precision which translates to approximately 7 decimal digits of precision. The problem is that 0.1 (ie 1/10th) cannot be exactly represented in binary, just the same as 1/3rd cannot be exactly represented in decimal. Click to expand.You're not alone, except that mostly it is people doing financial accounting that discover it when using single-precision floating-point to adding small numbers to large totals.
0 Comments
Leave a Reply. |