i need a bit of help with some maths.
I have a range 0 - 127 and i want to convert it into percentage.
So 0% = 0 and 100% = 127 and every number inbetween.
How would i do this?
Edit:
Thanks to what jon posted, i came up with:
$percent * 127 / 100
While Jon's answer was not incorrect, the answer given by belisarius was more complete, in that it allowed for a range of numbers beginning and ending with any number, and not necessarily starting with 0.
Here's a little better way to represent the formula:
percentage = (value - min) / (max - min)
If you want to represent the percentage as a whole number instead of a decimal, simply multiply the result by 100.
And here's the reverse (going from a percentage to a value):
value = ((max - min) * percentage) + min
The percentage here is a decimal. If your percentage is a whole number, simply divide it by 100 before inserting in this formula.
Generally, if you have numbers in the interval [a,b], to get the percentage inside your interval, the formula is:
percentage = 100 * (x-a) / (b-a)
Where x is your value
If you want to go from a value to a percentage, eg from 63.5 to 50%, divide your value by 127 & multiply by 100.
If you want to go the other way, eg from 50% to 63.5, it's the reverse: divide your percentage by 100 & multiply by 127.
Related
So in my text book there is this example of a recursive function using f#
let rec gcd = function
| (0,n) -> n
| (m,n) -> gcd(n % m,m);;
with this function my text book gives the example by executing:
gcd(36,116);;
and since the m = 36 and not 0 then it ofcourse goes for the second clause like this:
gcd(116 % 36,36)
gcd(8,36)
gcd(36 % 8,8)
gcd(4,8)
gcd(8 % 4,4)
gcd(0,4)
and now hits the first clause stating this entire thing is = 4.
What i don't get is this (%)percentage sign/operator or whatever it is called in this connection. for an instance i don't get how
116 % 36 = 8
I have turned this so many times in my head now and I can't figure how this can turn into 8?
I know this is probably a silly question for those of you who knows this but I would very much appreciate your help the same.
% is a questionable version of modulo, which is the remainder of an integer division.
In the positive, you can think of % as the remainder of the division. See for example Wikipedia on Euclidean Divison. Consider 9 % 4: 4 fits into 9 twice. But two times four is only eight. Thus, there is a remainder of one.
If there are negative operands, % effectively ignores the signs to calculate the remainder and then uses the sign of the dividend as the sign of the result. This corresponds to the remainder of an integer division that rounds to zero, i.e. -2 / 3 = 0.
This is a mathematically unusual definition of division and remainder that has some bad properties. Normally, when calculating modulo n, adding or subtracting n on the input has no effect. Not so for this operator: 2 % 3 is not equal to (2 - 3) % 3.
I usually have the following defined to get useful remainders when there are negative operands:
/// Euclidean remainder, the proper modulo operation
let inline (%!) a b = (a % b + b) % b
So far, this operator was valid for all cases I have encountered where a modulo was needed, while the raw % repeatedly wasn't. For example:
When filling rows and columns from a single index, you could calculate rowNumber = index / nCols and colNumber = index % nCols. But if index and colNumber can be negative, this mapping becomes invalid, while Euclidean division and remainder remain valid.
If you want to normalize an angle to (0, 2pi), angle %! (2. * System.Math.PI) does the job, while the "normal" % might give you a headache.
Because
116 / 36 = 3
116 - (3*36) = 8
Basically, the % operator, known as the modulo operator will divide a number by other and give the rest if it can't divide any longer. Usually, the first time you would use it to understand it would be if you want to see if a number is even or odd by doing something like this in f#
let firstUsageModulo = 55 %2 =0 // false because leaves 1 not 0
When it leaves 8 the first time means that it divided you 116 with 36 and the closest integer was 8 to give.
Just to help you in future with similar problems: in IDEs such as Xamarin Studio and Visual Studio, if you hover the mouse cursor over an operator such as % you should get a tooltip, thus:
Module operator tool tip
Even if you don't understand the tool tip directly, it'll give you something to google.
The problem is (-1.100 x 2^5) + (1.1001 x 2^7).
After shifting to get them both to the same magnitude you would get
1.10010 x 2^7
-0.01100 x 2^7
My problem is with carrying. I'm not sure if I am doing it right.
The answer I got was 0.01110 x 2^7, is this correct? Also, when subtracting how do I know if I would end up with a negative value? If the answer I have above is correct, would the correct representation in single precision IEEE be
0 10000110 011100000000000000000000
Check your work by adding the difference (the result) to the subtrahend (the number after the minus sign). If you get the minuend (the number before the minus sign), you did it correctly.
11 // the carries from the addition
0.01100 // the difference you computed
+ 0.01110 // the subtrahend
---------
0.11010 // should be the minuend, if you computed the difference correctly
That is not the minuend (1.10010), so you subtracted incorrectly.
Example range 1 (minimum and maximum):
[40 ... 480]
Example numbers from range 1:
[42, 59.4, 78.18, 120.43, 416]
Example range 2:
[10 .. 140]
How can I translate the values of the numbers from range 1 to values within the second range? 42 should be equivalent to something between 10 and 11 in the new range.
I'm using PHP but this is more like a math problem.
I know how to align them to the second range:
$diff = $range[0] - $numbers[0];
foreach($numbers as $i => $number){
$numbers[$i] = $number + $diff;
}
But that's it :(
Do you mean something like this, which scales the values linearly to fit within the new range?
var transformRange = (value, r1, r2) => {
var scale = (r2.max - r2.min) / (r1.max - r1.min)
return (value - r1.min) * scale;
}
Sample usage:
transformRange(100, {max: 150, min: 50}, {max: 1, min: 0}); => 0.5
You can transform one of the ranges into the other with this function (python)
def transfrom(x):
return (x - 40)*(130/440.0) + 10
In general the idea is that you want to rebase the ranges (make sure that they both start at zero) and then find how you need to stretch the first range to obtain the second range. So the steps would be
Convert the first range to [0-440] by subtracting 40 and the second range to [0-130] by subtracting 10. This is done to get intervals which start at zero which are easy to scale.
To convert any value from [0-440] to a corresponding value from [0-130] you need to multiply it with 130/440. You can imagine this as shrinking the first interval to a [0-1] interval by dividing with 440 and then stretching it to a [0-130] interval by multiplying.
Now you know how to go from [0-440] to [0-130] and that means that to go from [40-440] to [10-140] you first need to rebase by subtracting 40 multiply by 130/440 and then add 10
Example:
>>> transform(40)
10.0
>>> transform(42)
10.590909090909092
>>> transform(220)
63.181818181818187
>>> transform(480)
140.0
I have a database of 817 items, each given a "rank" of 1 to 817 (the smaller the number, the "better" the item). This rank is based off of many factors that indicate quality.
Now, I need to assign a "value" to these items, with the item at rank 1 being valued the most, and the value decreasing with rank (non-linear).
The easiest first attempt was to simply choose an arbitrary base (100,000) and divide by the rank:
$value = 100000 / $rank;
/**
* Rank : Value
* 1 : 100,000
* 2 : 50,000
* 3 : 33,333
* etc.
*/
This produces exponential decay, as shown in the red line in this image:
However, I wish to value these items in a manner that looks more like the blue line above. How can I change my formula to achieve this?
Try 1/sqrt(x) (i.e, pow(x, -1/2)) for starters. If that's still not slow enough, try a smaller fractional power.
Why don't you go with linear?
value = n - rank
where n is the count of your items, i.e. 817.
I haven't tried but use exponent instead of dividing by 1000 of a base 2.
UPDATES
value = 2 pow (n-rank)
If I have a series of pixels, which range from say -500 to +1000, how would I normalize all the pixels on the same gradient so that they fall between a specific range, say 0 and 255?
Some pseudocode like this would scale values linearly from one range to another
oldmin=-500
oldmax=1000
oldrange=oldmax-oldmin;
newmin=0
newmax=255;
newrange=newmax-newmin;
foreach(oldvalue)
{
//where in the old scale is this value (0...1)
scale=(oldvalue-oldmin)/oldrange;
//place this scale in the new range
newvalue=(newrange*scale)+newmin
}
Your question isn't very clear so I'm going to assume that you're doing some kind of image processing and the results you get are values from -500 to 1000 and now you need to save the color to a file where every value needs to be between 0 and 255.
How you do this is really very dependent in the application, what is really the meaning of the results and what exactly you want to do. The two main options are:
clamp the values - anything under 0 you replace by 0 and anything above 255 you replace by 255. You'll want to do this, for instance, if your image processing is some kind of interpolation which really shouldn't reach these values
Linear normalization - linearly may your minimal value to 0 and your maximal value to 255. Of course you'll first need to find the minimum and maximum. You do:
v = (origv - min)/(max - min) * 255.0
What this does is first map the values to [0,1] and then stretch them back to [0,255].
A third option is to mix and match between these two options. Your application might demand that you treat negative values as unneeded values and clamp them to 0 and positive values to linearly map to [0,255].
First make it all positive. If the minimum is -500 then add 500 to all values. Then the minimum would be 0, and the maximum would be 1500.
Then it is just a rule of three and you have it:
[value in 0,255] = 255*(Pixel/1500)
Some pseudo code may help:
foreach( pixel_value in pixel_values): # between -500 and 1000
position = (pixel_value + 500) / 1500 # gives you a 0 to 1 decimal
new_value = int(postion * 255) # or instead of casting, you could round it off
That's python code by the way.
Create two variables, MinInputValue and MaxInputValue. Initialize MinInputValue to a very large positive number (higher than the largest pixel value you ever expect to see) and MaxInputValue to a very large negative number (lower than the lowest pixel value you ever expect to see).
Loop over every pixel in the image. For each pixel, if the pixel value PixelValue is lower than MinInputValue, set MinInputValue to PixelValue. If the pixel value is higher than MaxInputValue, set MaxInputValue to PixelValue.
Create a new variable, InputValueRange, and set it to MaxInputValue - MinInputValue.
Once this is done, loop over every pixel in the image again. For each pixel PixelValue, calculate the output pixel value as 255.0 * (PixelValue - MinInputValue) / InputValueRange. You can assign this new value back to the original PixelValue, or you can set the corresponding pixel in an output image of the same size.