# how can i define a variable type for 0..359.99?

[b]0..359 would be easy:[/b]

TYPE
degrees = 0..359

[b]i guess 0..359.99 is, too, but[/b]

TYPE
degrees = 0..359.99

[b]does not work

who knows an answer for my problem?
thanks
[italic]chobo[/italic][/b]

## Comments

• : [b]0..359 would be easy:[/b]
:
: TYPE
: degrees = 0..359
:
: [b]i guess 0..359.99 is, too, but[/b]
:
: TYPE
: degrees = 0..359.99
:
: [b]does not work
:
: who knows an answer for my problem?
: thanks
: [italic]chobo[/italic][/b]
:

Why don't you just read TP help? It says that [b]subrange types[/b]
can only use [b]ordinal[/b] types as host. Real type is not an
ordinal so value such as 359.99 cannot be used.
The simplest but not fastest solutuion is to use 0..35999 and
divide it by 100 every time you need it.

Iby

• : Why don't you just read TP help? It says that [b]subrange types[/b]
: can only use [b]ordinal[/b] types as host. Real type is not an
: ordinal so value such as 359.99 cannot be used.
: The simplest but not fastest solutuion is to use 0..35999 and
: divide it by 100 every time you need it.
:
:
: Iby

thanks iby,
i read the help before, but it appears i didn't understand all of it.

well then, what [b]is[/b] the fastest solution?

even if you don't know, thanks again
[italic][b]chobo[/b][/italic]

• : : Why don't you just read TP help? It says that [b]subrange types[/b]
: : can only use [b]ordinal[/b] types as host. Real type is not an
: : ordinal so value such as 359.99 cannot be used.
: : The simplest but not fastest solutuion is to use 0..35999 and
: : divide it by 100 every time you need it.
: :
: :
: : Iby
:
: thanks iby,
: i read the help before, but it appears i didn't understand all of it.
:
: well then, what [b]is[/b] the fastest solution?
:
: even if you don't know, thanks again
: [italic][b]chobo[/b][/italic]
:

The fastest solution for any calculation is (as always)
use look-up table(s) (it seams you want to do some vectors).
If this is not acceptable solution (space requirement for
tables, for example), then next best (and most elegant)
solution is use of fixed comma math.

Iby

• : The fastest solution for any calculation is (as always)
: use look-up table(s) (it seams you want to do some vectors).
: If this is not acceptable solution (space requirement for
: tables, for example), then next best (and most elegant)
: solution is use of fixed comma math.
:
: Iby
:

me so chobo, ya gosu style programmer, ya ownzzzz me all...
well, i guess i'll have to do the dividing by 100 thing, since i don't understand the other solutions...
or, if thei're short... could you write them down for me?
if they are [b]not[/b] short, i think i wouldn't understand them anyway...

thanks again, you were a big help for me already
[italic][b]chobo[/b][/italic]

• : : The fastest solution for any calculation is (as always)
: : use look-up table(s) (it seams you want to do some vectors).
: : If this is not acceptable solution (space requirement for
: : tables, for example), then next best (and most elegant)
: : solution is use of fixed comma math.
: :
: : Iby
: :
:
: me so chobo, ya gosu style programmer, ya ownzzzz me all...
: well, i guess i'll have to do the dividing by 100 thing, since i don't understand the other solutions...
: or, if thei're short... could you write them down for me?
: if they are [b]not[/b] short, i think i wouldn't understand them anyway...
:
: thanks again, you were a big help for me already
: [italic][b]chobo[/b][/italic]
:

lookup tables are simple. you only have to have lot's of memory.
for example:
if your program requires very fast calculation (any
calculation) what you probably want to do is calculate
ALL results right away and store tham into table
(specially efective on floating point or any more
complex than adding and substracting). This will
make slower loading of the program but this one or
two second lost in calculation will pay off. Later
in the main program when you really need fast performance,
all you do is get the values from the table.

code example:
let's say you want to do animation of a sprite (bouncing ball
for example). you don't need really great accuracy
(0.001 degree is an overkill!) so lets do it for every
degree. We could have only one section (0-90deg.) and derive
everything from it but it is simpler and faster to do the
full 360degree circle.

[code]
Program SIN_nice_and_slow;

uses graph,....;

var x,y:integer;

begin
{ no preparation }

{ set graph mode }

for x:=0 to 359 do { main loop }
begin
y:=round(200*sin(x));
putpixel(x,250-y);
end;

{ set text mode }
end.

program SIN_smart_and_fast;

var sinus:array[0..359] of integer;
x,y:integer;

begin

for x:=0 to 359 do { calculate tables }
sinus[x]:=250-round(200*(1-sin(x)));

{ set graph mode }

for x:=0 to 359 do { main program loop }
begin
putpixel(x,sinus[x]);
end;

{ set text mode }
end.

[/code]

Note that we did more than just calcualting sin. We also
did rounding, offseting etc. The result is table
full of data ready for use. I guess this explains
the principle. If you need SIN and COS, just make longer
table:

[code]
var sinus:array[0..449] of integer; { 360 + 90 deg }
[/code]

Later in the program, whenever you need COS, replace it
with
[code]
SIN[x+90]
[/code]

Now you see how tables can get huge! If use of tables is
not practical, it is still better to use fixed coma
math instead of slow calculation with floating point.
Nowdays when CPUs are running with hundreds of MHz and
come with integrated FPU, this has far lower importance
than it had not so long ago.

Anyway, idea is to use integers, regardless of register
size (16, 32 or even 64 bit). Let's assume 32-bit
(it's easy to use even in TP). Then we could divide
the 32-bit into two sections: BEFORE and AFTER decomal
point. This spliting is just imaginary. For example
if we use first bit for sign, 21-bit BEFORE and
10-bit AFTER decimal point (1+21+10=32) what range
do we get?

21 bit gives the size:
2 to power of 21 is just over 2 milion (2097152).

10 bit gives the accuracy:
2 to power of 10 is just over 1000 (1024).
this means accuracy of at least 0.001

So usefull range would be roughly +/-2000000.000

Not very impresive compared to REAL but far better
than INTEGER. Now we have to make own functions to
add, subtract, divide, multiply etc. such two numbers.
It can be lot's of headacke but if done wisely it can
boost performance. The good side there are libraries
which already have all this implemented, you just need
to use them (after you find them :-)).

Iby

• wow, impressive answer. i'll see what i can make out of it. i understood it, that's most important. except with the fixed comma thing... i'm not really sure if i understood that absolutely. could you give me a short example of what it would look like in pascal (just a short bit of code...)?

you are really a great help for me, if i can do sth for let me know. (in fact there's not much i [b]can[/b] do, but... just ask.
[italic][b]chobo[/b][/italic]

Sign In or Register to comment.

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!