Schrijver
| Calculate PI=3,1415.... in msx z80 assembly language.
|
nikodr msx addict Berichten: 491 | Geplaatst: 28 Juli 2007, 15:31   |
As i am getting more and more interested in machine code i would like to ask if anyone has a routine that can calculate PI (lets say some thousand digits of it) in pure machine code for use in msx.Do you have any routine?
Best regards
|
|
djh1697 msx professional Berichten: 551 | Geplaatst: 28 Juli 2007, 16:19   |
I would imagine that PI is stored as a variable within the ROM, but I dont know to be honest?
|
|
Prodatron msx master Berichten: 1110 | Geplaatst: 28 Juli 2007, 16:35   |
Maybe the floating point routines, which I used in the pocket calculator for SymbOS, can be useful for you. They are the original Amstrad CPC Basic routines (40bit and work very accurate), and I disassembled and commented them:
http://www.symbos.de/download/app-calculator.zip
PI is also stored here as a 40bit floating point constant, but maybe you can use the routines to re-calculate PI with your required amount of digits  |
|
ARTRAG msx master Berichten: 1737 | Geplaatst: 28 Juli 2007, 18:01   |
do you want to compute many digits for PI in z80 ASM ?
First you need an algorithm : many of them exist,
choose one that can work in fixed precision, i.e. that
can be compute using integers
IMHO the easiest way is using tyalor seriers or atan
atan(x) = x - x^3/3 + x^5/5 - ...+/- (x^n)/n ...
now we have that:
atan(1) = PI/4
so in order to compute PI/4
you need to compute a bunck of divisions like 1/n and add the results
atan(1) = 1 - 1/3 + 1/5 - ... +/- 1/n ...
or, even better:
PI = 4 - 4/3 + 4/5 - ... +/- 4/n ...
now you need a ASM routine able to compute divisions on a integers with a large number of bits
on line you can easily find 64bit routines for integer division, willing more digits imply to write an ad hoc routine
assume you find division code on n bits, convert the number above in fix precision in oder to fit in the
maximum range of digits in your routine.
e.g. if n = 32 bits, assuming signed ints, 4 should be 2147483648
this allows you to compute 2147483648 terms in the ATN expansion
getting non zero results.
think that each term you add in the sum does not correspond to one digit in PI
good luck !
|
|
ARTRAG msx master Berichten: 1737 | Geplaatst: 28 Juli 2007, 23:39   |
|
|
dvik msx master Berichten: 1339 | Geplaatst: 28 Juli 2007, 23:52   |
Way back I wrote a program that computed 1000 decimals of PI on my Atari ST. It was quite a lot of work, most of it to implement support for large numbers (1000 decimals is about 4000 bits) and math functions using these large numbers. There are open source libraries that you can look at but I'm quite sure there aren't any MSX libraries available that supports large numbers.
The routines to calculate PI ARTRAG mentioned won't give you many decimals on PI in reasonable time. You need to use more sophisticated algorithms. The one I used gives 1000 decimals in 5 or 6 iterations. If I can find it I'll post it here.
|
|
dvik msx master Berichten: 1339 | Geplaatst: 29 Juli 2007, 00:06   |
The algorithm I used is called something like Borweins & Ramanujan. Its quite simple and very efficient:
start: n=0; y=sqr(2)-1; a=6-4*sqr(2);
repeat:
n=n+1; y=(1-sqr(sqr(1-y^4)))/(1+sqr(sqr(1-y^4))); a=(a*(1+y)^4)-y*(1+y+y^2)*2^(2*n+3);
|
|
jr msx addict Berichten: 310 | Geplaatst: 29 Juli 2007, 08:14   |
|
|
ARTRAG msx master Berichten: 1737 | Geplaatst: 29 Juli 2007, 08:33   |
@dvik
I am aware that Tailor expansion of atan(x) has slow convergence,
BTW I suggested it mainly due to the fact that it needs only
n bits sum and n bits divisions, that are easier to be implemented
in asm also for a asm beginner.
On line 32 an 64 bits asm code for add/sub and duv should
be already available
|
|
|
|
|