Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > calculating of square roots ?
2006-06-29 00:59
Trifox
Account closed

Registered: Mar 2006
Posts: 108
calculating of square roots ?

hi all, for my newest project i am in urgent need to calculate the length of a 2d vector, reminding pythagorian math i remember that i have to calculate the roots of a fixed point (8bits.8bits) number, how can that be mastered in a convenient way ?!?!?!

thx
 
... 92 posts hidden. Click here to view all posts....
 
2006-07-03 15:28
Graham
Account closed

Registered: Dec 2002
Posts: 990
@JackAsser: Rounding is not correct here since the assembler routine also gives INT(SQR(X)) and not INT(SQR(X)+0.5).

@_V_: You can calculate it as long as you don't have intermediate numbers, for example: (SIN(45°)^2) = 0.5 :)

Well and the math teacher said that pocket calculators used tables for calculation. That's definitely wrong, imagine that he said that in the 80s when a few KB was not only expensive but also biiiig :)
2006-07-03 16:14
_V_
Account closed

Registered: Jan 2002
Posts: 124
Graham: You calculated sin^2(45°), not sin(45°), which is a different number. Sin(45°) is equal to sqr(2)/2. Since sqr(2) is an irrational number, sin(45°) cannot be equal to a decimal fraction or bit pattern of any kind, it can only be approached by one. The best we can do with irrational numbers is to represent them with a symbol (e, for example) or extract them from properties wherein they appear (for example, the ratio of the surface of a perfect disc and the squared radius of said disc is exactly equal to pi). In other words, we use them in an indirect way so that we don't have to use their mindboggling explicit form.

Yes, you can use a currently known algorithm to calculate sin(45°)... but only up to a certain degree of precision. If you want to determine the entire number, its real value, you'll need an infinite amount of time to do so. That's why I said that you can't *really* calculate them with such methods.

As for the 80s maths teacher and the table, he might have meant a table containing certain power series (Taylor, Lagrange, etc.) expansion coefficients which the calculator could use to calculate f(x) for a given x and f, using for example a formula like f(x) = a0+a1*x+a2*x^2+... Such a table would be small enough to fit into the calculators of that era, I reckon. If he was talking about storing the huge books filled with log-tables and stuff inside them, then no indeed :).
2006-07-03 16:32
Graham
Account closed

Registered: Dec 2002
Posts: 990
No I was talking about keeping "SIN(45°)" as representation of SIN(45°), you don't always need decimals :)
2006-07-03 22:44
_V_
Account closed

Registered: Jan 2002
Posts: 124
Yep, but at some point, in some formula, you'll need the numerical value of sin(45°), because it's explicitly stated in the result. And then, you'll find that you cannot calculate it exactly with the current methods, unless you're willing to wait forever :).
2006-07-04 06:59
JackAsser

Registered: Jun 2002
Posts: 1989
Question is, why do you WANT the numerical exact representation. To apply it in the real world? I don't think so, at some point you'll get down to the quant angle, quant time, quant length, quant ***. So, one could argue that more decimals in PI than f.e. 1000 is irrelevant in the numerical form since they're not applicable on anything. :D And if you need that many decimals to avoid summing up rounding errors I suggest using another method than the numerical representation in the first place.
2006-07-04 10:44
enthusi

Registered: May 2004
Posts: 675
this is somewhat off topic here :) but euler's 'e' is for example very much needed with greater precision than 1000 decimals since there are massive iterative computations 'using' it. Actually there is the legend about an error in 1000th place that caused the first examination of 'chaotic' behavior.
Since sin is periodic and almost always associated with geometry, I would know of no use for superprecision (well that is not the reason for my lack of knowledge but you get what I mean).
@_v_: waiting for ever wont give you the result either

in general: don't defend math teachers - they tend to know shit :) But I too thought they use small coefficent tables for some calcs - like the kernal does :)

2006-07-04 11:15
Graham
Account closed

Registered: Dec 2002
Posts: 990
Here's an extended incarnation of the integer sqrt routine. This one is extended so it does proper rounding. It is achieved by adding another iteration and calculate bit -1 which then can be used to determine if the fractional part is >= .5 or not.

The disadvantage of rounding is that the output has to be 9 bits now, because inputs $FF01 to $FFFF result in 256 now (and not 255). Alternatively you also might modify this routine and simply use bit -1 for further calculations. This gives you more accuracy than rounding.

        LDY #$00    ; R = 0
        LDX #$07
        CLC         ; clear bit 16 of M
.loop
        TYA
        ORA stab-1,X
        STA THI     ; (R ASL 8) | (D ASL 7)
        LDA MHI
        BCS .skip0  ; M >= 65536? then T <= M is always true
        CMP THI
        BCC .skip1  ; T <= M
.skip0
        SBC THI
        STA MHI     ; M = M - T
        TYA
        ORA stab,x
        TAY         ; R = R OR D
.skip1
        ASL MLO
        ROL MHI     ; M = M ASL 1
        DEX
        BNE .loop

        ; bit 0 iteration

        STY THI
        LDX MLO
        LDA MHI
        BCS .skip2
        CPX #$80
        SBC THI
        BCC .skip3
.skip2
        TXA
        SBC #$80
        TAX
        LDA MHI
        SBC THI
        STA MHI
        INY         ; R = R OR D (D is 1 here)
.skip3
        CPX #$80
        ROL MHI

        ; bit -1 iteration and rounding ( + 0.5)

        LDX #$00
        BCS .skip4
        CPY MHI
        BCS .skip5
.skip4
        INY         ; R = R + 0.5
        BNE .skip5
        INX
.skip5
        ; R in X and Y (Y is low-byte)

        RTS
stab:   .BYTE $01,$02,$04,$08,$10,$20,$40,$80

If you don't wanna have the rounding and prefer having bit -1, then use this final iteration:

        ; bit -1 iteration

        BCS .skip4
        LDX #$00
        CPY MHI
        BCS .skip5
.skip4
        LDX #$80
.skip5
        ; R in Y, X contains bit -1

        RTS
2006-07-05 19:59
_V_
Account closed

Registered: Jan 2002
Posts: 124
@Jackasser: I was speaking theoretically, at the moment, there is no apparent need for super-precision. Hence my prior statement, "So we settle for rational approximations which usually are sufficient for all intents and purposes". However, this may very well change in the future - there may also be procedures which only have superslow convergence, or systems of differential equations which require superprecision in order to not blow up after 2 iterations.

Quantum theory might not be the end-all of everything. Therefore, I would wait before declaring that the universe is just a collection of discrete spaces of quantum dimensions in which elementary particles hop and do their thing, and that only a limited amount of precision is all you need.

@enthusi: No, when you wait infinitely long, i.e. when you take the limit t->+Inf, you will have the exact decimal representation of the number (as decimals are countable). Remember, "infinity" is a weird thing. And, I have to defend maths teachers because I am one (actually physics, but I've given maths for a couple of years and I did have the impression that I knew what I was talking about).
2006-07-05 20:06
Tch
Account closed

Registered: Sep 2004
Posts: 512
"Don´t think too much about things,it will influence the outcome.
Thus there is no absolute truth."

Sorry,read this somewhere and I couldn´t resist. ;)
2006-07-05 21:49
Graham
Account closed

Registered: Dec 2002
Posts: 990
Quantum theory stays assumed right until somebody proves it wrong. That's the way of science.

Anyway, talking about "infinitely long" or "unlimited decimals" makes me oppose. There is no such timepoint called "infinity". Every point-of-time you can come up with is still a pretty finite point-of-time and the same applies to decimals. Every decimal number you can write down is a decimal with limited length, or else it is no decimal anymore. (Infinity might be weird, but unboundedness is not. And only the 2nd one exists.)
Previous - 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Rhythm/G★P
Icon/TRIAD
iceout/Avatar/HF
Bacchus/FairLight
Iapetus/Algar../Cosine
Oswald/Resource
E$G/hOKUtO fOrcE
Unlock/Padua/Albion
Guests online: 102
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 Memento Mori  (9.6)
10 Bromance  (9.5)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Dawnfall V1.1  (9.5)
8 Quadrants  (9.5)
9 Daah, Those Acid Pil..  (9.5)
10 Birth of a Flower  (9.5)
Top Groups
1 Nostalgia  (9.3)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Fullscreen Graphicians
1 Carrion  (9.8)
2 Joe  (9.8)
3 Duce  (9.8)
4 Mirage  (9.7)
5 Facet  (9.7)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.059 sec.