SRL Archive - SRL Archives

Couple simple math functions...


n3ss3s

2007-11-16 09:38:56

I noticed that there aren't some math functions in SCAR
(yes I know they + these aren't all)

and I quickly made couple simple, but maybe useful for someone

1. Median - in Finnish the name is Mediaani, so I think its something like Median.
It returns the value in middle of the array after rearranging from small to large.
If the array size is even, it gets the two middlest and divides by two.


Function Median(Ints: TIntegerArray): Extended;
Var
I: Integer;
Begin
BubbleSort(Ints);
I := GetArrayLength(Ints);
If I Mod 2 <> 0 Then
Begin
Result := Ints[Round(High(Ints) / 2)];
End Else
Begin
Result := (Ints[Round(High(Ints) / 2)-1] + Ints[Round(High(Ints) /2)]) / 2;
End;
End;

Function Average(Ints: TIntegerArray): Extended;
Var
I: Integer;
Begin
For I := 0 To High(Ints) Do
Begin
Result := Result + Ints[i];
End;
Result := Result / GetArrayLength(Ints);
End;

Function InIntArrayArray(Ints: Array of TIntegerArray; Value: Integer): Boolean;
Var
I: Integer;
Begin
For I := 0 To High(Ints) Do
Begin
If InIntArray(Ints[i], Value) Then
Begin
Result := True;
Exit;
End;
End;
End;

Function InIntArrayArrayEx(Ints: Array of TIntegerArray; Var TheArray: Integer; Value: Integer): Boolean;
Var
I: Integer;
Begin
For I := 0 To High(Ints) Do
Begin
If InIntArray(Ints[i], Value) Then
Begin
Result := True;
TheArray := I;
Exit;
End;
End;
End;

I decided to post these here instead of my own function thread, because these could be called 'core functions' or like that, not my custom obj finders lol.


Hope they make someones life easier :)

ShowerThoughts

2007-11-16 09:42:43

looks confusing :S so its (h) :p

n3ss3s

2007-11-16 09:57:02

Actually its not at all, one thing that makes functions look uber is that there is no string to make any
''
there.

Take few minutes to read them through, then you'll find they're easy as any other math stuff you can do.

Dumpin

2007-11-16 15:12:59

man your the god ;O awsome

Santa_Clause

2007-11-16 16:43:29

Function Median(Ints: TIntegerArray): Extended;
Var
I: Integer;
Begin
BubbleSort(Ints);
If GetArrayLength(Ints) Mod 2 <> 0 Then
Result := Ints[Round(High(Ints) / 2)]
Else
Result := (Ints[Round(High(Ints) / 2)-1] + Ints[Round(High(Ints) /2)]) / 2;
End;
End;

Function Average(Ints: TIntegerArray): Extended;
Var
I: Integer;
Begin
For I := 0 To High(Ints) Do
Result := (Result + Ints[i]) / GetArrayLength(Ints);
End;

The others can be shortened but I can't be bothered :p

ShowerThoughts

2007-11-16 16:44:19

N3ss3s needs some attention to i know ^^

R0b0t1

2007-11-16 17:01:58

Looks like you just got out of math class n3ss3s :p.

n3ss3s

2007-11-17 02:04:51

@r0b0t: umm yeah :p

Well average wasn't anything new but median I hadnt heard of before…

@Santy: You don't have any functions to shorten - Bwuahahahahaaa
The reason I use Begin-End like everywhere is because it just doesn't fit my eye without them, except for If Not LoggedIn Then Exit


EDIT:
But he'd most likely divide you by zero and crash the universe.


Omg, he uses hacks too!! He needs a tuned compiler to bypass the divide by zero error!

EDIT EDIT: Chuck Norris has now been officially disqualified.

Santa_Clause

2007-11-17 02:56:29

From: n3ss3s
@r0b0t: umm yeah :p

Well average wasn't anything new but median I hadnt heard of before…

@Santy: You don't have any functions to shorten - Bwuahahahahaaa
The reason I use Begin-End like everywhere is because it just doesn't fit my eye without them, except for If Not LoggedIn Then Exit


EDIT:

Omg, he uses hacks too!! He needs a tuned compiler to bypass the divide by zero error!

EDIT EDIT: Chuck Norris has now been officially disqualified.


Nothing to shorten? Just remove the begin and end :p

n3ss3s

2007-11-17 03:20:39

Noooo!!

YOU CANT TAKE MY BABY!!!!

Noooooooooooooooo!!!!!

*starts swinging arms like a craizee*

Santa_Clause

2007-11-17 03:32:59

Fine, fine :p

n3ss3s

2007-11-17 03:38:23

man your the god ;O awsome

:p Nah

Im currently tuning FindObjOre (well it kinda isn't the same anymore)

and there I have couple places that don't have begin-end :)

Wizzup?

2007-11-17 05:11:06

From: Santa_Clause
Function Median(Ints: TIntegerArray): Extended;
Var
I: Integer;
Begin
BubbleSort(Ints);
If GetArrayLength(Ints) Mod 2 <> 0 Then
Result := Ints[Round(High(Ints) / 2)]
Else
Result := (Ints[Round(High(Ints) / 2)-1] + Ints[Round(High(Ints) /2)]) / 2;
End;
End;

Function Average(Ints: TIntegerArray): Extended;
Var
I: Integer;
Begin
For I := 0 To High(Ints) Do
Result := (Result + Ints[i]) / GetArrayLength(Ints);
End;

The others can be shortened but I can't be bothered :p


Your Average won't work. You can do: Result := (Result + Ints) Div 1;
But not divide it by the entire array.
The only way n3ss3s can shorten down the Average of his is to remove the begin and end; in the for loop.

n3ss3s

2007-11-17 05:13:02

Wizzup are you sure? :S

( 3 + 1 + 5) / 3 is the average. Or am I using wrong word…

EDIT: Oh, you were speaking to santy! :p


Function Average(Ints: TIntegerArray): Extended;
Var
I: Integer;
Begin
For I := 0 To High(Ints) Do
Begin
Result := Result + Ints[i];
End;
Result := Result / GetArrayLength(Ints);
End;
yes, this one without or with begins and ends..

Paul23

2007-11-17 18:37:45

I have a maths degree and did a fair bit of stats so if anybody is interested comes across more esoteric statistical definitions and wishes to know more about them feel feel to drop me a pm.

Paul

ZephyrsFury

2007-11-21 04:18:38

This may be a bit of a bump but just so you know a median is an average as well. There are three types of averages: mean, median and mode. The 'average' that your talking about is mean. Median is the one in the middle. Mode is the one that occurs the most.

Santa_Clause

2007-11-21 07:24:01

Are you sure that mode and median are considered averages too…? I thought average meant all the numbers added and divided by the number of numbers, which is the same as the mean…

n3ss3s

2007-11-21 11:18:16

Hehe, didn't make one for Mode because didn't find the english word :p

Mode = Tyyppiarvo (= "Type value" = like "typical value")

R0b0t1

2007-11-21 13:41:07

He's a word root dictionary too!


What I find is strange though, is you can look at any compound word in any language, and it kinda makes sense.



EDIT: The only word I can think of that has two vowels in a row is "aardvark".

n3ss3s

2007-11-21 14:17:37

Omg? In Finnish there is lot words with two vowels in a row, especially names.


EDIT: The only word I can think of that has two vowels in a row is "aardvark".
what about "cheese"?

"mouse"
"keyboard"

lots of stuff

ZephyrsFury

2007-11-21 18:54:26

From: Santa_Clause
Are you sure that mode and median are considered averages too…? I thought average meant all the numbers added and divided by the number of numbers, which is the same as the mean…


They're all types of averages.