radians to degreees ..

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
Bolador
New member
Posts: 9
Joined: Thu Jan 30, 2020 4:56 am

radians to degreees ..

Post by Bolador »

well I am relatively new to C, Z88dk and trigonometry, but as I may have some kind of pleasure in suffering I decided to develop something using all three of them.. just to keep my mind doing something besides the daily work..
I could make my first C codes and successfully compile them (not without some forum help).. but results were not the expected (as expected:D)..
then I discovered thad results of simple sin() and cos() functions are given in radians :|.. (I wonder why in first place someone invented that radian thing :rolleyes:).. that would be easy to convert them but adding a multiplication and a division to every function in the loop will kill the already slow performance..
is there any way to tell the thing to calculate in degrees? how will compiler take that in speed terms.. I am thinking of using a look up table for speed purposes in the future but need to develop it easy at first.. I am slow coding, taking it easy,, having fun.. (I am kind of stressing myself now :lol:
thanks!!
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

Post by Timmy »

Bolador wrote:then I discovered thad results of simple sin() and cos() functions are given in radians :|.. (I wonder why in first place someone invented that radian thing :rolleyes:).. that would be easy to convert them but adding a multiplication and a division to every function in the loop will kill the already slow performance..
Yes, it's clear to me you are new to trigonometry. The results of simple sin() and cos() are not in radians; it's the inputs for those functions that are in radians. The outputs of the functions are between -1 and 1.

Conversion between degrees and radians is as simple as miles to kms, or gallons to litres; it's just one multiplication with a fixed number (you'll have to figure it out which :) ) The problem of course is that your poor z80 machine doesn't have multiplications nor divisions built-in. And if you've had any maths at school you would know that divisions are harder (and slower) to do than multiplications, so don't do divisions in computers if possible.

And yes, all trigonometry is slow on 8 bit computers. That's why people never do them there.
is there any way to tell the thing to calculate in degrees? how will compiler take that in speed terms..
You can write your own functions in C too, but what your sin function would do is to multiply that degrees number with some other number so that it becomes a radian. What else would you have expect? It may make you feel better but the sine of 0.7372773 radians is just the same as the sine of 47.5 degrees. (Yes, degrees have fractions, too :) )
I am thinking of using a look up table for speed purposes in the future but need to develop it easy at first.. I am slow coding, taking it easy,, having fun.. (I am kind of stressing myself now :lol:
thanks!!
If you only use less than say, 361 different degrees, then you can probably just use a lookup table for speed purposes. Let's assume each result takes 2 bytes, then you'd just need at most, 360*2=720 bytes to store the sines.

Good luck, and that's why most people never really do sines and cosines on 8 bit machines. And don't worry about the speed problem, doing maths is hard on 8 bit processors. It's why sometimes later, that math co-processors were created (I think it's around the time of 80386/80486 machines).
stefano
Well known member
Posts: 2137
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

You can use lib3d, it has a lookup table already prepared.
See clock.c
Bolador
New member
Posts: 9
Joined: Thu Jan 30, 2020 4:56 am

Post by Bolador »

Thanks Timmy!! I usted to have an 80287... I see math are slow on a Z80 bug my zeddy only has one of these inside .. I dont understand when you said people don't do.. what do they do instead to guest the results?..
Bolador
New member
Posts: 9
Joined: Thu Jan 30, 2020 4:56 am

Post by Bolador »

stefano wrote:You can use lib3d, it has a lookup table already prepared.
See clock.c
I had a look at lib3d but documentation is so scarce.... I've found the demo too so I'll try to figure out. I think that not to reinvent the wheel is a good advice!!!..
I what to move certain vector by an angle, that is: vector x0,y0 x1,y1 to a new position where x1,y1 was moved a known angle from it's current position. Keeping x0,y0 in the original position need to know the new x1,y1. (all in 2D)

Regards
Post Reply