SRL Archive - FAQ

Simba maths?!


logical

2011-11-29 12:29:39

someone explain this please…


program new;
var i:extended;
begin
i:=(3/4);
writeln(tostr(i));
end.

why does that return 0 and not 0.75??

even this returns a 0… why is this? :(


program new;
var i:extended;
begin
i:=ceil(3/4);
writeln(tostr(i));
end.

senrath

2011-11-29 12:40:51

Because 3 and 4 are integers. Try it with 3.0 and 4.0.

logical

2011-11-29 13:42:36

tyvm senrath :D i'm learning :)
another problem i keep getting is

'Error: Out Of Range at line 11'

could you help me again? what am i doing wrong?

(it simply returns inv slot coords)

program learning;
{.include SRL/SRL.scar}
var ix,iy:extended;
i:integer;
r:tpointarray;

begin
i:=1;
repeat
begin
r[i].x:=round(563+(42*ix));
r[i].y:=round(213+(36*iy));

ix:=round(((i/4.0)-floor(i/4.0))*4.0);
iy:=ceil(i/4.0);

writeln(' r[i].x (x coord):' + inttostr(r[i].X) + ' r[i].Y (y coord):' + inttostr(r[i].Y)); //563, 213 ~~ 598,244
writeln(' i:' + tostr(i) + ' ix:' + tostr(ix) + ' iy:' + tostr(iy));
i:=i+1;
end;
until(i=29);

end.

i luffs yeww

2011-11-29 13:48:26

You have to set the length of the array before adding new elements. So right after you do i := 1; you can do SetLength(r, 28);, and that should fix it.

There's also plenty of functions in SRL relating to the inventory that should suffice everything you need (for example you can get the four corners of every inventory space with InvBox).

Tniffoc

2011-11-29 17:45:48

From: senrath
Because 3 and 4 are integers. Try it with 3.0 and 4.0.


To give you a little bit of background on why this works the way it does, I'll offer a little spiel.

When you tell Simba (3/4) you are essentially telling it that it is working with whole numbers, and doesn't need to waste space by making room in memory for a decimal. It is like giving it 3 blocks and telling it to split them in to 4 equal groups without altering the blocks. It's impossible. When you say 3.0 and 4.0, you are telling it to allocate enough space in memory to work with decimals.

This is a pretty universal concept across many computer languages, so its certainly a good thing to learn right off the bat.

logical

2011-11-29 17:56:22

^.^ ty guys!
where do you suggest i learn these more simple things on computer languages?

Tniffoc

2011-11-29 18:19:40

Well, the best answer to that question is by coming to SRL after having programmed in other languages. For you, it's going to be your first language, so aside from reading the tutorials, there's going to be a lot of learning by doing. At this point, I would attribute only about 5% of my programming knowledge to things that I learned in classes and tutorials. The rest I've learned through experience and talking to people. Just hanging out here, scripting, and stopping by the IRC will be enough to give you a solid base, even if just through osmosis.