Proximity between numbers in an Interval - php

I have a set of (floating point) numbers between x = 1 and y = 9:
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
How can I compute a metric of the proximity, in the given interval, between number A and number B?
What I've Tried
If the amplitude (max - min) of the above set is 9 - 1 = 8 I am able to compute the relative value of any number using the formula (n - min) / (max - min), computing this for all values yields:
(1 - 1) / (9 - 1) = 0
(2 - 1) / (9 - 1) = 0.125
(3 - 1) / (9 - 1) = 0.25
(4 - 1) / (9 - 1) = 0.375
(5 - 1) / (9 - 1) = 0.5
(6 - 1) / (9 - 1) = 0.625
(7 - 1) / (9 - 1) = 0.75
(8 - 1) / (9 - 1) = 0.875
(9 - 1) / (9 - 1) = 1
Dividing the minimum relative value (between A and B) with the maximum relative value (also between A and B), seems to produce the kind of metric I'm looking for. Here are a few examples:
var_dump(min(0.875, 0.25) / max(0.875, 0.25)); // 0.286 between 8 and 3
var_dump(min(0.875, 0.375) / max(0.875, 0.375)); // 0.429 between 8 and 4
var_dump(min(0.875, 0.75) / max(0.875, 0.75)); // 0.857 between 8 and 7
var_dump(min(0.875, 0.875) / max(0.875, 0.875)); // 1 between 8 and 8
var_dump(min(0.25, 0.25) / max(0.25, 0.25)); // 1 between 3 and 3
The Problem
Whenever the minimum value of the set comes into play, the result will always be 0:
var_dump(min(0.875, 0) / max(0.875, 0)); // 0 between 8 and 1
var_dump(min(0.125, 0) / max(0.125, 0)); // 0 between 2 and 1
var_dump(min(0, 0) / max(0, 0)); // 0 between 1 and 1 (ERR!)
Any ideas on how to solve this?

I was suggesting something like this:
<?php
function prox($a,$b)
{
return(abs($a-$b) / abs(1-9));
}
printf("%f\n", prox(1,2)); // 0.125000
printf("%f\n", prox(2,3)); // 0.125000
printf("%f\n", prox(1,1)); // 0.000000
printf("%f\n", prox(1,9)); // 1.000000
The proximity between 1 and 2 is the same as 2 and 3. This seems to make sense.
The largest proximity you'll get is when the numbers you specify are the bounds of your predefined range.
The smallest proximity you'll get is when the numbers you specify are equal.
If you want the opposite to be true (which I suppose better reflects proximity, you could do:
<?php
function prox($a,$b)
{
return(1 - (abs($a-$b) / abs(1-9)));
}
printf("%f\n", prox(1,2));
printf("%f\n", prox(2,3));
printf("%f\n", prox(1,1));
printf("%f\n", prox(1,9));
Which would output:
0.875000
0.875000
1.000000
0.000000
Now, the same number produces 1 as you specified and the bounds produce 0 as they're the least-proximate pair of values. Combinations like (1,2), (2,3), (3,4), etc. all produce the same value, as do combinations like (2,4), (3,5), (4,6), etc, etc.

Related

How do Generate a random number between 1 and 2 in PHP [duplicate]

This question already has answers here:
Random Float between 0 and 1 in PHP
(13 answers)
Closed 4 years ago.
How do Generate a random number between 1 and 2 with a possible result of both integer or float in PHP
I try to use rand(1,2) or mt_rand(1,2) but i keep getting between 1 and 2.
I want my result so be either 1.3 , 1.1, 1.3 ,2
What about this with mt_rand() and mt_getrandmax()?
<?php
function randomFloat($min = 0, $max = 1) {
return number_format($min + mt_rand() / mt_getrandmax() * ($max - $min), 2, '.', ''); // 2 decimal places
}
echo randomFloat(1,2);
DEMO: https://3v4l.org/5a5U1

How to determine next level using a formula?

Thank you for any help you can provide on this.
If we have:
$level = floor((-1 + sqrt(1 + 4 * ($c_xp + 500) / 100 * 2)) / 2) - 1; //GET LEVEL
For instance if:
$c_xp = 1200
Then:
$level = 4
Then I am asking if:
$level = 4
How much xp to get to level 5?
How can I get the amount of xp required to achieve the next level?
The formula to get the minimum XP required to reach a level $level is given by:
$minXP = 12.5 * (pow((2 * $level + 3), 2) - 41);
So you can set $level = 5 and $level = 4 and find the difference between them.
Analyse:
I ran a script to view the XP needed for each level:
$current_level = 0;
for($c_xp = 1;$c_xp < 7000;$c_xp++){
$level = floor((-1 + sqrt(1 + 4 * ($c_xp + 500) / 100 * 2)) / 2) - 1; //GET LEVEL
if($level > $current_level){
$current_level++;
echo "c_xp $c_xp level $level<br>";
}
}
The output is:
c_xp 1 level 1
c_xp 100 level 2
c_xp 500 level 3
c_xp 1000 level 4
c_xp 1600 level 5
c_xp 2300 level 6
c_xp 3100 level 7
c_xp 4000 level 8
c_xp 5000 level 9
c_xp 6100 level 10
We can see that level 1 is an exception, then the difference of XP from level 2 to level 3 is 400, from level 3 to level 4: 500 and level 4 to level 5: 600 and so on...
The pattern is now clear,
Level 1 -> 1 XP
Level 2 -> Start a 100 XP
Level 3 -> Increase by 400 (500 XP)
Level 4 -> Increase by (400 + 100) 1000XP
Level 5 to Level X -> Keep increasing by 100
The code:
With the above analyse, I could come up with a loop in a function:
$c_xp = 1200;
$level = floor((-1 + sqrt(1 + 4 * ($c_xp + 500) / 100 * 2)) / 2) - 1;
echo 'Next level: '.current_level_xp($level+1).'<br>';
echo 'XP needed for next Level: '. (current_level_xp($level+1) - $c_xp);
function current_level_xp($current_level){
if($current_level <= 1){
return(1);
}else{
$increase = 400;
$needed = 100;
for($i=0;$i<$current_level-2;$i++){
$needed += $increase;
$increase += 100;
}
return $needed;
}
}
Output:
Next level: 1600
XP needed for next Level: 400
Use a math / formular plotting tool of your choosing to get an idea of the results of the used equation (which presumably isn't yours):
Now, there's probably even a way to reverse your formula for translating y into x. But I'm unsure if there's a floor reverse.

php function calling itself

Why is the answer, 13 as given. I just cannot get my head around it.
What does the following function return, if the given input is 7:
function foo($bar) {
if ($bar == 1) return 1;
elseif ($bar == 0) return 0;
else return foo($bar - 1) + foo($bar - 2);
}
Correct Answer: D. 13
nneonneo should have just posted his comment as the answer, but, this is is how the concept of recursion works:
foo(7)
= foo(6) + foo(5)
But wait, what're those equal to?
foo(6) = foo(5) + foo(4)
Sonofagun!
foo(5) = foo(4) + foo(3)
Hmm .. a pattern emerging ..
foo(4) = foo(3) + foo(2)
foo(3) = foo(2) + foo(1)
foo(2) = foo(1) + foo(0)
foo(1) = 1
and
foo(0) = 0.
So now you can figure out backwards back to the values, but (and this the more important question) what's really happening when you increase $bar by 1 again?
How does foo(8) compare to foo(7)?
And the answer is that foo (8) equals foo(7) + foo(6). In other words, it is equal to 13 + 8 - the sum of the two previous outputs of foo .. hey, that sounds familiar ... is there some famous sequence that is equal to the sum of the previous two numbers?
1, 2, 3, 5, 8, 13 ...
That's right, this is how you can calculate the Fibonacci sequence recursively. And if you think about how you build up the Fibonacci sequence, it's really
1, 1 + 1, 2 + 1, 3 + 2, 5 + 3, 8 +5
Which is just
1, 1 + 1, (1 + 1) + 1, (2 + 1) + (1 + 1), etc.
By "seeding" the initial values (position "0" is 0, position 1 is 1) and then adding them together, you are able to derive each number in the sequence using just the original seeds and a lot of addition.
So in this case, bar represents the bar position in the Fibonacci sequence. So the 7th number in the sequence is 13.
It is very simple, just trace the sequence by hand. When using this type of recursion it helps to think of it more as a mathematical function than a programming procedure. If you want to get to know it more naturally, playing with a functional language *ML or some LISP would help you a lot and very quickly.
When you have a recursion on a data structure (Stack/Queue), then it is a bit different, but functional programming experience helps for that too.
foo(0) = 0
foo(1) = 1
foo(2) = foo(1) + foo(0) = 1 + 0 = 1
foo(3) = foo(2) + foo(1) = 1 + 1 = 2
foo(4) = 2 + 1 = 3
foo(5) = 3 + 2 = 5
foo(6) = 5 + 3 = 8
foo(7) = 8 + 5 = 13

Divide integer and get integer value

In languages like C or Python 2, if I divide an integer by an integer, I get an integer:
>>> 8/3
2
But in PHP, if I divide an integer by another integer with /, sometimes I get a float:
php > var_dump(6/3);
int(2)
php > var_dump(8/3);
float(2.6666666666667)
I'd like to do division like in Python or C, so that 8/3 is 2. How can I do that in PHP?
As of PHP 7, we can use the intdiv built-in function to get an integer value:
intdiv(8,3); // 2
For earlier versions of PHP, we can use the round() function to get an integer rounded value:
round(8 / 3); // 3
Or we can use the floor() function to get an integer value:
floor(8 / 3); // 2
In PHP 7, there is intdiv function doing exactly what you want.
Usage:
intdiv(8, 3);
Returns 2.
There is no integer division operator in PHP. 1/2 yields the float 0.5. The value can be casted to an integer to round it downwards, or the round() function provides finer control over rounding.
var_dump(25/7); // float(3.5714285714286)
var_dump((int) (25/7)); // int(3)
var_dump(round(25/7)); // float(4)
PhP manual
use this....
intval(1700000 / 300000 )...
this returns the integer value.
FOR PHP 7 try intdiv() Function:
Syntax: int intdiv($dividend, $divisor)
<?php
$dividend = 19;
$divisor = 3;
echo intdiv($dividend, $divisor);
?>
For Older versions of PHP:
<?php
// Convert $total_minutes to hours and minutes.
$total_minutes = 640;
$minutes = $total_minutes % 60;
$hours = ($total_minutes - $minutes) / 60;
echo "Time taken was $hours hours $minutes minutes";
?>
You can use the shortform by adding |0 to the end
8/3|0
There are several ways to perform integer division in PHP. The language doesn't have an operator for integer division, but there are several options for rounding the floating point quotient to an integer:
<?php
$pos = 1;
$neg = -1;
$divisor = 2;
// No rounding (float division)
var_dump($pos / $divisor); // 0.5 (float)
var_dump($neg / $divisor); // -0.5 (float)
// Round toward zero (like C integer division)
var_dump((int)($pos / $divisor)); // 0 (int)
var_dump((int)($neg / $divisor)); // 0 (int)
// Round half away from zero
var_dump(round($pos / $divisor)); // 1 (float)
var_dump(round($neg / $divisor)); // -1 (float)
// Round down
var_dump(floor($pos / $divisor)); // 0 (float)
var_dump(floor($neg / $divisor)); // -1 (float)
# And on PHP 7 you can round toward zero with intdiv():
var_dump(intdiv($pos, $divisor)); // 0 (int)
var_dump(intdiv($neg, $divisor)); // 0 (int) Rounded toward zero
On PHP 7 you can use intdiv($p, $q) to directly perform integer division. This is equivalent to (int)($p / $q) on PHP 5.
(int)(1700000 / 300000);
use type casting.
For PHP version 7 => intdiv(a,b)
And for versions less than 7 (like 5.6) => (int)floor(abs(a/b))

What does the percent sign mean in PHP?

What exactly does this mean?
$number = ( 3 - 2 + 7 ) % 7;
It's the modulus operator, as mentioned, which returns the remainder of a division operation.
Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3.
5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.
10 % 5 returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.
In the example you posted, (3 - 2 + 7) works out to 8, giving you 8 % 7, so $number will be 1, which is the remainder of 8/7.
It is the modulus operator:
$a % $b = Remainder of $a
divided by $b.
It is often used to get "one element every N elements". For instance, to only get one element each three elements:
for ($i=0 ; $i<10 ; $i++) {
if ($i % 3 === 0) {
echo $i . '<br />';
}
}
Which gets this output:
0
3
6
9
(Yeah, OK, $i+=3 would have done the trick; but this was just a demo.)
It is the modulus operator. In the statement $a % $b the result is the remainder when $a is divided by $b
Using this operator one can easily calculate odd or even days in month for example, if needed for schedule or something:
<?php echo (date(j) % 2 == 0) ? 'Today is even date' : 'Today is odd date'; ?>
% means modulus.
Modulus is the fancy name for "remainder after divide" in mathematics.
(numerator) mod (denominator) = (remainder)
In PHP
<?php
$n = 13;
$d = 7
$r = "$n % $d";
echo "$r is ($n mod $d).";
?>
In this case, this script will echo
6 is (13 mod 7).
Where $r is for the remainder (answer), $n for the numerator and $d for the denominator. The modulus operator is commonly used in public-key cryptography due to its special characteristic as a one-way function.
Since so many people say "modulus finds the remainder of the divisor", let's start by defining exactly what a remainder is.
In mathematics, the remainder is the amount "left over" after
performing some computation. In arithmetic, the remainder is the
integer "left over" after dividing one integer by another to produce
an integer quotient (integer division).
See: http://en.wikipedia.org/wiki/Remainder
So % (integer modulus) is a simple way of asking, "How much of the divisor is left over after dividing?"
To use the OP's computation of (3 - 2 + 7) = 8 % 7 = 1:
It can be broken down into:
(3 - 2 + 7) = 8
8 / 7 = 1.143 #Rounded up
.143 * 7 = 1.001 #Which results in an integer of 1
7 can go into 8 1 time with .14 of 7 leftover
That's all there is to it. I hope this helps to simplify how exactly modulus works.
Additional examples using different divisors with 21.
Breakdown of 21 % 3 = 0:
21 / 3 = 7.0
3 * 0 = 0
(3 can go into 21 7 times with 0 of 3 leftover)
Breakdown of 21 % 6 = 3:
21 / 6 = 3.5
.5 * 6 = 3
(6 can go into 21 3 times with .5 of 6 leftover)
Breakdown of 21 % 8 = 5:
21 / 8 = 2.625
.625 * 8 = 5
(8 can go into 21 2 times with .625 of 8 leftover)

Categories