SRL Archive - FAQ

Simba random(#) to Extended/Float


Cstrike

2010-10-17 11:56:19

I've been trying this math function out of curiosity:

program new;

function GF (a, b, c, x : Extended) : Extended;
begin
result := a*Pow(2.71828, -(((x-b)*(x-b))/c));
end;

var x, xposi, xpose : Extended;
xposi2 : Integer;
begin
xposi2 := random(100);
writeln (xposi2);
xposi := xposi / 100.5; // are we extended now...?
writeln (xposi);
xpose := xposi/100; // Redundant, ignore me... this was in place before the above function to see if going to ext. was possible
writeln (xpose)
x := GF (1, 0, 0.2, xpose);
writeln (x);
end.

No matter what I do, I can't seem to turn the random integer into an extended, therefore whenever I apply a gaussian function my results always return as 1 or 0 (this was designed to be around 1 and 0… though the only fix I have is to go back and make it between 100 and 0).

Is there a way to use extended from a LongInt? or am I SOL?

EDIT: Should I put a decimal after each number? It seems that if I do y = x/2000, I will always get an integer back.

nielsie95

2010-10-17 12:03:11

Don't you mean xposi := xposi2 / 100.5?

Cstrike

2010-10-18 12:56:48

From: nielsie95
Don't you mean xposi := xposi2 / 100.5?

Yeah that was a bad typo (i fixed it) ;)



Is there any way to convert from an integer to a decimal without making a new equation?

For example


C := somethingA / somethingB;

if C = extended and somethingA and B are integers, C will end up being an integer unless I do something like this


C := somethingA / somethingB / 1.0;
Is there any way to avoid having to throw * or / by 1.0 everywhere?

Nava2

2010-10-18 13:45:21

No. Not with integer division issues. I don't think you can type cast in PS with primitive types.

You are stuck with doing `/' or `*' by 1.0. Or you can just use floats instead of integers.. :)

Wizzup?

2010-10-18 15:10:35

Not sure if PS can do it. You could always make a function for it I guess. Perhaps Extended() works.

ZephyrsFury

2010-10-18 19:58:19



C := somethingA * 1.0 / somethingB


should work.

You have to remember that division of integers by / or div gives you an integer result. And since the order of operations with multiplication and division is performed left to right, you either need to order your equation so that your ints are converted to floats before you do the division.

Dgby714

2010-10-18 21:11:17

I was bored =/

function ToExtended(Data: variant): extended;
begin
case VarType(Data) of
varInteger, varInt64, varSmallInt, varShortInt, varWord, varLongWord, varByte: Result := Data * 1.0;
varString, varOleStr: Result := StrToIntDef(Data, 0) * 1.0;
varNull: Result := 0.0;
varDouble: Result := Data;
varBoolean: if (Data) then
Result := 1.0
else
Result := 0.0;
end;
end;