I'm kind of stuck with this, it SHOULD be very simple, but my brain can't wrap around it (it's Friday... lol)
I've got a thermometer, representing $1,000,000 max. it's 375px tall.
I'm running a DB Query to grab amounts from user submissions (between $1 and $200).
At that math, it's $2,666.66 per pixel to move it up 1 pixel ---
retrieve_amount(); is my DB function that grabs all the amounts - that's simple.
$fill_query = retrieve_amount();
$fill = 0;
$total = 0;
while($fill_query->is_valid() ) : $fill_query->amount();
$amount = get_valid_amount($input, 'amount');
$total = $total + $amount;
endwhile;
$finaltotal = $total; // THIS is the line that grabs the final total from above. Should work?
$fillheight = $SOMETHING +/-* $SOMETHING; // this is the line that i'm less sure of how to get my result
It may be that I'm just not great with math, but my questions are
$finaltotal = $total
should work to receive the total amount retrieved from the DB Query, correct?
And more importantly, how do I translate that to the pixels that I need?
$maxPixels = 375;
$maxAmount = 1000000;
$currentAmount = 1234567;
$currentPixels = round(($currentAmount / $maxAmount) * $maxPixels);
It's basically just like calculating percentages. Except, instead of 100%, your max is now 375 pixels.
Related
First post, please be gentle.
I'm trying to create a simple market script where for example I have a number in my database ie 50.00 and I want to run a cron job php script to increase or decrease this randomly to a minimum of 10.00 and a maximum of 75.00.
I thought a random 0,1 follow by 2 if statements 1 rand(-0.01,0.05) if 2 rand(0.01,0.05) then $sql = "UPDATE price SET oil='RESULT'";
I've tried a few times at the above but I can't get it to run and the other crons in the file work.
<?php
//Get Oil Price from database
$oilchange = rand(1, 2);
if ($oilchange == '1') {
$oilnew = rand(0.01,0.05);
//Oil price from database times oil new.
} else {
$oilnew = rand(-0.01,-0.05);
//Oil price from database times oil new.
}
// Update Price
?>
Rand is for integers (whole numbers)
First up, your use of rand between two decimal values (called floats) won't work, as rand is for integers only. So, you'd first want to have a random function which does output floats, like this:
function randomFloat($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
Then we can safely use it between, say, 1% and 5%:
$percentSwing = randomFloat(0.01, 0.05);
Rand defaults to being 0 or 1. We can use that to randomly invert it, so we also cover -1% to -5%:
$percentSwing *= rand() ? 1 : -1;
The above could also be written like this:
if(rand() == 1){
// Do nothing:
$percentSwing *= 1;
}else{
// Invert it:
$percentSwing *= -1;
}
So, we now know how much we need to swing the number by. Let's say it was $oilPrice:
$oilPrice = 48;
We can just multiply the percent swing by that number to get the amount it's changing by, then add it back on:
$oilPrice += $percentSwing * $oilPrice;
So far so good! Now we need to make sure the price did not go out of our fixed range of 10 to 75. Assuming you want to 'clamp' the number - that means if it goes below 10, it's set at 10 and vice-versa, that's done like this:
if( $oilPrice < 10 ){
// It went below 10 - clamp it:
$oilPrice = 10;
}else if( $oilPrice > 75 ){
// It went above 75 - clamp it:
$oilPrice = 75;
}
The above can also be represented in one line, like this:
$oilPrice = max(10, min(75, $oilPrice));
So, that gives us the whole thing:
function randomFloat($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
// Define the oil price (e.g. pull from your database):
$oilPrice = 48;
// get a random 1% to 5% swing:
$percentSwing = randomFloat(0.01, 0.05);
// Invert it 50% of the time:
$percentSwing *= rand() ? 1 : -1;
// Swing the price now:
$oilPrice += $percentSwing * $oilPrice;
// Clamp it:
$oilPrice = max(10, min(75, $oilPrice));
// Output something!
echo $oilPrice;
As a side note here, money in real financial systems is never stored as a float, because rounding errors can cause major problems.
I believe this is a language agnostic question and more focused on math, however I prefer PHP. I know how to calculate percentages the normal (forward) way:
$percent = 45.85;
$x = 2000000;
$deduction = ($percent / 100) * $x; // 917,000
$result = $x - $deduction; // 1,083,000
What I would like to do, is be able to reverse the calculation (assuming I only know the $percent and $result), for example...
54.15% of x = 1,083,000
How do I calculate x? I know the answer is 2,000,000, but how do I program it to arrive at that answer?
I found a similar question & solution through Google but I just don't understand how to implement it...
You can do
1,083,000 * 100 / 54.15
In PHP, it will be
$x = $result * 100 / $percent
When you say 54.15% of x = 1083000, you mean 0.5415 * x = 1083000. To solve for x, divide 0.5415 from both sides: x = 1083000 / 0.5415. The PHP is:
$p = 54.15;
$r = 108300;
// First, make p a number, not a percent
$p = $p/100; // I would actually use $p/= 100;
// Now, solve for x
$x = $r/$p;
I'm fairly new to PHP and I'm trying to design a car's gear ratio calculator that takes certain vehicle's dimensions as input. One way to calculate them is trough using geometric progression method. Below are the codes I'm trying to run:
<?
$alowgearf = 14.37;
$ahigear = 3.293;
$noOfGear = 5;
$loopLimit = $noOfGear - 2;
$gears = array(0 => "$alowgearf", "$noOfGear - 1" => "$ahigear");
$nlnh = $alowgearf/$ahigear;
$cgp = pow($nlnh, 1/($noOfGear - 1));
for ( $n = 1; $n <= $loopLimit ; $n++ ) {
$m = $n - 1;
$gear = $gears[$m]/$cgp;
$gears[$n] = $gear;
}
print_r($gears);
?>
The formula for the GPM method of calculating gear ratio is as follows :
N[i+1] = N[i]/Cgp,
where N is gear ratio, i is the gear number and Cgp is the geometric progression constant.
By supplying the formula with the lowest and highest gear ratio and the desired number of gears, we can calculate the rest of the gear ratios.
The problem is, the codes above outputs nothing. I've been trying to fix the code but I can't detect any problem with them. I've been toying with other loop function such as do-while but the results are the same. What's the problem? What I've been doing wrong?
I have built a simple modular scale calculator, where I can enter a base number (say font size or line height) and an important number (maybe column width, page width, or another font size) and select a ratio (golden ratio for example) and the calculator will display a double stranded scale for use in page layout. see example below
I have been toying with the idea of allowing users to input points and picas and then displaying the scale in one or the other.
The problem is that picas are base 12 numbers (12 points to a pica), I figured if I could just convert the input (something like 16p6) to base 12 I could do the calculation and go from there.
I just can't work out how to do basic calculations in another base. I'm really just messing around to see what I can come up with, let me know if you think I'm barking up the wrong tree.
So my question is this how do I do calculations in base 12?
<?php
// basic modular scale calculation
$goldenRatio = 1.618;
$baseNumber = 16;
$i = 0;
while ($i <= 10) {
echo round($baseNumber,1). "<br>";
$baseNumber = $baseNumber * $goldenRatio;
$i++;
}
echo "<hr><br>";
// Attempt at base 12 calculation
$a=base_convert(16,10,12);
$b=base_convert(12,10,12);
$r = ($a*$b);
echo $a."*".$b."=";
echo $r;
I'm really just messing around to se what I can come up with, let me know if you think I'm barking up the wrong tree.
Update
To solve the problem of converting Picas to base points from a string like '12p6' I ended up using regex to first test if Picas and Points had been supplied the split the Picas and Points.
function isPica($data) {
if (preg_match('/^[0-9]+(?i)p([0-1]?[0-9])?$/i',$data)) {
return true;
}
return false;
}
function makePoints($data) {
$data = preg_replace('/^([0-9]+)((?i)p)(([0-1]?[0-9])?)$/i','$1.$3',$data);
$data = explode('.',$data);
$points = floor($data[0] * 12);
$points = $data[1] + $points;
return $points;
}
Modular Scale Calculator
Git Hub — Modular Scale Calculator
base_convert just converts the string representation. You can't do calculations using strings of numbers in base 12 in php. When dealing with imperial units, you usually have multiple "bases" to deal with. So it has to be done manually. When you're doing calculations, the base doesn't matter.
Convert all the different units to the smallest one (points). $a = 3*12 + 7;//3picas, 7points.
Do the calculations.
Convert back to original units.
$points = (int)$val % 12;
$picas = (int)($val / 12);
or
$picas = floor($val / 12);
$points = $val - 12*$picas;
in my app I get a var $curxp which holds an int, now I want to create a function that automatically returns $xplvlup (which holds an int how much total XP is needed for the next level and a function that returns the current level.
Now I could simply hardcode with switch statements and calculated numbers like:
switch($curxp){
case <10: $xplvlup = 10; break;
}
But it would be much nicer if I could use an algorithm so there is no max level.
I know I have to work with exponents to get a nice curve, but I just dont know how to start things up.
UPDATE
Thanks to Oltarus I came to the following solution:
$curxp = 20;
function level($lvl){
return $xp = pow($lvl,2) + 5 * $lvl;
}
$lvl = 0;
while (level($lvl) < $curxp) $lvl++;
$totxp = level($lvl);
$xplvlup = level($lvl) - $curxp;
echo 'Level: '.$lvl."<br />";
echo 'Total XP: '.$totxp."<br />";
echo 'XP needed for Levelup: '.$xplvlup;
If for instance you would have the first level-up require 500 xp, and then every level you will need 10% more xp you could do something like this:
function xp_needed($cur_lvl){
$start = 500;
return $start*pow(1.1,($cur_lvl-1));
}
For every level, the xp is calculated by 500 * 1.1^(level-1)
Edit
Woops, $cur_lvl should be substracted by 1.
I don't know how you calculate the levels, but let's say you have a function level($n) that returns how many XP are needed to have level $n.
$n = 0;
while (level($n) < $curxp) $n++;
$xplvlup = level($n) - $curxp;