I have to show the instantaneous value of a sensor output using a QProgressbar.
value can change from 0 to 0.3
how do I configure (set range and set value) the QProgressBar to display the above value.
I'm a bit confused because setRange method can only take int values, and how do I set range as 0 and 0.3?
There are different way. You can chose between these two options:
Transform your range in an integer range by multiplying by 100
[0 , 0.3] -> [0 , 300]
When you receive a new value, just multiply it by 100
0.12*100 = 120/300
You can also make setRange(0, 100) and for each value, make the conversion:
(value * maxValue) / 100
So (0.12 / 0.3) * 100 give you 40%
Related
I would like to write an operation that takes a number, that could take any value larger than -1, and only outputs a number between -1 and 0.5
Currently, I am able to ensure that the above happens and it always outputs a number between 0 and 1 by doing the following:
SupressedNumber = (Number)%1
And the following for values between -1 and 0:
SupressedNumber = (Number)%-1
And the following for values between -0.5 and 0:
SupressedNumber = (Number)%-0.5
However I would like to make it between -1 and <-0.5 (-0.49ish max). It doesn't have to use modulus but I feel like it's part of the solution. It just has to be doable in lua.
As far as I understand, you want to clamp a number between two values, but one of them is a supremum, not a maximum. Lets solve the first problem first:
To clamp a number between two values inclusivly (e.g. -1 <= number <= -0.5), you can use the standard lua functions math.min() and math.max() or even code it yourself, if you need pure lua:
local function clamp(min, value, max)
return math.max(min, math.min(max, value))
end
clamp() will return value, but if value is smaller than min, it returns min. If it is greater than max, it returns max, so the result never leaves [-1, -0.5].
Since your goal is: [-1, -0.5), you have to make a compromise. Computers store decimals with a finite amount of precision, so you can't get a number that is infinitly close to -0.5, but maybe a number, that is close enaugh. Let's create a variable epsilon which says, how close is close enough:
local epsilon = 0.00001
And now we can put both ideas together:
supressed_number = clamp(-1, number, -0.5 + epsilon)
This will clamp your number between -1 and a little bit less than -0.5:
-1 <= value <= −0,49999 < -0.5
I'm working on a project where I have a pane that goes from 0 to 500 on the x and z, but I need to convert that coordinate (from 0 to 500) to a float in the range of 0.0 to 1.0 in because it is required from some of the functions I'm using.
I'm stuck on how to take the axis position (lets say something like, 3475x3475) and change in into it's relative position on the plane from 0.0f to 1.0f
(I've tried to write this in a way that any programmer can understand, instead of using Unity terminology. If you're a Unity programmer and would better understand it with that terminology tell me and I'll re-write it)
You can take your number that ranges from 0 to 500 and simply divide it by 500, e.g. scaled_x = x / 500.0f. Depending on the language and the type of x you will need to divide by either 500 or 500.0f. If you are using a language that has integer division like C, and if x is an integer than x / 500 will be zero unless x is 500, but if you do x/500.0f than you will get a float between 0.0f and 1.0f (assuming x is between 0 and 500 inclusively). This is of course assuming you want a linear mapping, e.g. 0 maps to 0, 100 maps to 0.2, 250 maps to 0.5 and so on.
I have data that comes from different sources with different typical ranges, like so:
VALUE LOWERBAND UPPERBOUND
5 2 7
6 1 10
2 1 4
22 3 8
...
I would like to normalise VALUEs with respect to LOWERBAND and UPPERBOUND, but as I have no background in statistics I really can't see how it could be done. Any pointers?
To put it in other words, I guess I would like to rescale VALUES so they would all belong to the same LOWERBAND and UPPERBOUND (perhaps the global mean LOWERBANDs and UPPERBOUNDs?)
I guess what you are after is something like the following:
Move the lower bound to zero:
newValue = oldValue - LOWERBOUND
Calculate the value as a percentage of the upper bound (scale 0 - 100)
newValuePercent = (newValue / UPPERBOUND) * 100
In you example the last value is btw. outside the [LOWERBOUND, UPPERBOUND] range... so not sure whether you want to limit it at the end or not
How can I convert a length into a value in the range -1.0 to 1.0?
Example: my stage is 440px in length and accepts mouse events. I would like to click in the middle of the stage, and rather than an output of X = 220, I'd like it to be X = 0. Similarly, I'd like the real X = 0 to become X = -1.0 and the real X = 440 to become X = 1.0.
I don't have access to the stage, so i can't simply center-register it, which would make this process a lot easier. Also, it's not possible to dynamically change the actual size of my stage, so I'm looking for a formula that will translate the mouse's real X coordinate of the stage to evenly fit within a range from -1 to 1.
-1 + (2/440)*x
where x is the distance
So, to generalize it, if the minimum normalized value is a and the maximum normalized value is b (in your example a = -1.0, b = 1.0 and the maximum possible value is k (in your example k = 440):
a + x*(b-a)/k
where x is >= 0 and <= k
This is essentially two steps:
Center the range on 0, so for example a range from 400 to 800 moves so it's from -200 to 200. Do this by subtracting the center (average) of the min and max of the range
Divide by the absolute value of the range extremes to convert from a -n to n range to a -1 to 1 range. In the -200 to 200 example, you'd divide by 200
Doesn't answer your question, but for future googlers looking for a continuous monotone function that maps all real numbers to (-1, 1), any sigmoid curve will do, such as atan or a logistic curve:
f(x) = atan(x) / (pi/2)
f(x) = 2/(1+e-x) - 1
(x - 220) / 220 = new X
Is that what you're looking for?
You need to shift the origin and normalize the range. So the expression becomes
(XCoordinate - 220) / 220.0
handling arbitrary stage widths (no idea if you've got threads to consider, which might require mutexes or similar depending on your language?)
stageWidth = GetStageWidth(); // which may return 440 in your case
clickedX = MouseInput(); // should be 0 to 440
x = -1.0 + 2.0 * (clickedX / stageWidth); // scale to -1.0 to +1.0
you may also want to limit x to the range [-1,1] here?
if ( x < -1 ) x = -1.0;
if ( x > 1 ) x = 1.0;
or provide some kind of feedback/warning/error if its out of bounds (only if it really matters and simply clipping it to the range [-1,1] isn't good enough).
You have an interval [a,b] that you'd like to map to a new interval [c,d], and a value x in the original coordinates that you'd like to map to y in the new coordinates. Then:
y = c + (x-a)*(c-d)/(b-a)
And for your example with [a,b] = [0,440] and [c,d] = [-1,1], with x=220:
y = -1 + (220-0)*(1 - -1)/(440-0)
= 0
and so forth.
By the way, this works even if x is outside of [a,b]. So as long as you know any two values in both systems, you can convert any value in either direction.
I'm writing iPhone code that fuzzily recognizes whether a swiped line is straight-ish. I get the bearing of the two end points and compare it to 0, 90, 180 and 270 degrees with a tolerance of 10 degrees plus or minus. Right now I do it with a bunch of if blocks, which seems super clunky.
How to write a function that, given the bearing 0..360, the tolerance percentage (say 20% = (-10° to +10°)) and a straight angle like 90 degrees, returns whether the bearing is within the tolerance?
Update: I am, perhaps, being too specific. I think a nice, general function that determines whether a number is within a percentage of another number has utility in many areas.
For instance: Is the number swipeLength within 10% of maxSwipe? That would be useful.
BOOL isNumberWithinPercentOfNumber(float firstN, float percent, float secondN) {
// dunno how to calculate
}
BOOL result;
float swipeLength1 = 303;
float swipeLength2 = 310;
float tolerance = 10.0; // from -5% to 5%
float maxSwipe = 320.0;
result = isNumberWithinPercentOfNumber(swipeLength1, tolerance, maxSwipe);
// result = NO
result = isNumberWithinPercentOfNumber(swipeLength2, tolerance, maxSwipe);
// result = YES
Do you see what I'm getting at?
int AngularDistance (int angle, int targetAngle)
{
int diff = 0;
diff = abs(targetAngle - angle)
if (diff > 180) diff = 360 - diff;
return diff;
}
This should work for any two angles.
20% as a decimal is equal to 0.2. Just divide by 100.0 to get the decimal. Divide by 2.0 to get half of the acceptable range. (Combined into 200.0 divisor)
From there, add and subtract from 1.0 to get the 90%, and 110% values.
If the first number is between the ranges, then there you have it.
BOOL isNumberWithinPercentOfNumber(float firstN, float percent, float secondN) {
float decimalPercent = percent / 200.0;
float highRange = secondN * (1.0 + decimalPercent);
float lowRange = secondN * (1.0 - decimalPercent);
return lowRange <= firstN && firstN <= highRange;
}
Note: there's no error checking here for NaN or negative values. You'll want to add that for production code.
Update: to make percent to include both +/- range.
Answer to your refined/new question:
bool isNumberWithinPercentOfNumber (float n1, float percentage, float n2)
{
if (n2 == 0.0) //check for div by zero, may not be necessary for float
return false; //default for a target value of zero is false
else
return (percentage > abs(abs(n2 - n1)/n2)*100.0);
}
To explain, you take the absolute difference between your test and target value, and divide it by the target value (the two 'abs'olute calls make sure this works with negative target and test numbers too, but not with negative percentages/tolerances). This gives you the percentage of the difference expressed as decimal fraction, multiplies it with 100 to give the 'common' expression of percentage (10% = 0.10),