Maths

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Maths

Post by Grim... »

Okay, I'm not really sure where to put this, so I guess I can't go wrong in here.

Here is the deal.

I have a group of numbers, say 1, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 6, 7, 7, 8.
I want the smallest number to equal 10, and the highest to equal 20, and I want all the numbers inbetween to get the relevent number between 10 and 20 (in this case, 4 would euqal 15 and 6 would equal 17.5).

The highest and lowest numbers could change, so I could have a group of numbers starting with 10000 and ending with 909443 but they (and all the numbers inbetween) would still have to go into the 10 - 20 scale.

So how the hell do I do that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not sure what you are asking.. so lemme try this: you want to pass an array of numbers, that you want scaled into the range you specify?
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

small number always = to 10, biggest number always equals to 20..
BUT what he wants is that every number between will be relevent
meanin if i have 2 as the smallest number 4 as a biggest number andi have 3 in between (meaning i got 2, 3, 4)
2 will become 10
4 will become 20
3 will become 15

Why 3 is 15..
well be simple calculation, for 2 tobecom 10, and for 4 to becom 20 we have to multiply everything by 5
so if we have to scale 3 with the same frmula, (x5) it'll become 15.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

That's fine Draco, but what if the numbers are 1, 6, 250, 500?

There must be a formula for it - where are the Math phd boys?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Feyd - yes.

What I want it for is a site that has pictures that you can tag with keywords (called tags).
This page takes the keywords and adds them up, then displays them in alphabetical order, the rarer ones with a size of 10px and the most common ones with the size of 20px.

Linkage
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Although I might want to make the max size 35px or something - but once the formula is worked out I'm sure it won't be that hard.

At the moment it looks like it's working, but that's because the max number is 25ish. If it gets to 250:
a) The letters will be huge
b) There will be so many pictures I'll have run out of webspace ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

k.. so here's the basics: (untested)

Code: Select all

function distribute($array, $min, $max)
{
  $array = sort($array);
  $low = $array[0];
  $high = $array[sizeof($array) - 1];
  $scale = ((float)($max - $min))/($high - $low);
  $return = array();
  foreach($array as $key) $return[$key] = ($key - $low) * $scale + $min;
  return $return;
}
I believe the math is in order.. :)
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

My previous post just explained to feyd your first post
:)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

as I said, untested.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

feyd may have it right. To to it the long way:

0. Find the Min value.

1. Make the range zero based by subtracting the Min from every number.

2. Find the Max value.

3. Then calculate your Scale, which in your case is Max / 10.

5. Then normalize all the numbers my dividing them by Scale. That give you numbers in the range >0 and <10.

5. Finally add an Offset to every number to put them in the range you want. In your case add 10 to get them in the range >10 and <20.

There are definitely faster ways to do it, but it is so weird that I would do it the long way and document the code well.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

and realise that $max can equal $min.. (found this bug in panachart few days ago ;))

else dy = $value / $max - $min = $value / 0 -> error :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$max = $min isn't a problem. Only $high = $low can be a problem. Course if computers would return infinite results instead of exploding when dividing by zero, things would be slightly more fun... :?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Thanks guys!
Post Reply