C or Obj-c function to normalize range to x-y - objective-c

I have an output of ranges from 150-0. I want to map those to 0 to 1. Or perhaps 0 to (some value less than 1 such as 0.5) where 150 is 0 and 0 is 1 ( or some values less than..).
Is this considered interpolation? What is the formula to derive these values? But preferably, is there a built-in StdLib function I can call?

Divide your number by the (Max - min). This would make 150 be 1 and 0 will be 0, with everything else a number in between. Now, to make it the opposite just do 1 - result.

If you need to map 0-1 to any custom range, you need to multiply range with MAX-MIN and then add MIN to it to get the exact number in range.
Formula will be MIN + (MAX-MIN)*value
where value is range in between 0-1;
MIN is number mapped to 0;
MAX is number mapped to 1;

Related

Snowflake CEIL Function - round up to next 0.1 kilometer

I have a column containing measurement values in meters.
I want to round them up (ceil) them to the next 100m and return it as a km value.
Special thing is: if the original value is a "round" number (100m increment) it should be ceiled up to the next 100m increment (see line 3 in the example below).
Example:
meter_value kilometer_value
1111 1.2
111 0.2
1000 1.1
I think I can get the first two lines by doing:
ceil(meter_value/1000,1) as kilometer_value
The solution I thought of to fix the edge case in line three is to just add 1 meter always:
ceil((meter_value+1)/1000,1) as kilometer_value
It seems a bit clumsy, is there a better way/alternative function to archive this?
You can check to see if it's divisible by 100 and only add one if it is:
ceil(((meter_value + iff(meter_value % 100 = 0, 1, 0))/1000), 1)
This will prevent situations where (if decimal parts are allowed) adding 1 to a value of 999.5 would not be accurate if adding one all the time.
Greg's answer is good, simpler to read to me would be to
divide by 100
floor
add 1
ceil
divide by 10
select
column1 as meter_value
,ceil(((meter_value + iff(meter_value % 100 = 0, 1, 0))/1000), 1) as greg
,ceil(floor(meter_value/100)+1)/10 as simeon
from values
(1111)
,(111)
,(1000)
,(1)
,(0)
;
METER_VALUE
GREG
SIMEON
1,111
1.2
1.2
111
0.2
0.2
1,000
1.1
1.1
1
0.1
0.1
0
0.1
0.1
do we want to mention negative values? I mean it distance, so it's a directionless magnitude, right?
anyway with negative value, both our methods the +1 forces the boundary case to be wrong.
Actually:
Once you have floored adding the 1 or 0.1 if you divide by 1000 vs 100 first, you don't need to ceil at all
thus two short forms can be:
,ceil(floor(meter_value/100)+1)/10 as version_a
,(floor(meter_value/100)+1)/10 as version_b
,floor(meter_value/1000,1)+0.1 as version_c

Split a vector into parts separated by zeros and cumulatively sum the elements in each part

I want to split a vector into several parts separated by the numeric value 0. For each part, cumulatively calculate the sum of the elements encountered so far. Negative numbers do not participate in the calculation.
For example, with the input [0,1,1,1,0,1,-1,1,1], I expect the result to be [0,1,2,3,0,1,1,2,3].
How to implement this in DolphinDB?
Use the DolphinDB built-in function cumPositiveStreak(X). Treat the negative elements in X as NULL values.
Script:
a = 1 2 -1 0 1 2 3
cumPositiveStreak(iif(a<0,NULL,a))
Execution result:
1 3 3 0 1 3 6

SQL Aggregating Mixed Data

I have a table that has 3 columns
STA (which is equivalent to X)
BL (which is equivalent to y)
Ultimate_Load (which has positive and negative values)
I want to aggregate column 3 by the maximum of absolute values, but display the actual (negative or positive) value.
your help is appreciated
I think this would work. You get de max absolute value of ultimate_load and then change it's sign depending on the sign of MAX(ultimate_load)+MIN(ultimate_load).
SELECT STA,BL, MAX(ABS(ULTIMATE_LOAD)) * case sign(MAX(ULTIMATE_LOAD)+min(ULTIMATE_LOAD)) when 0 then 1 when -1 then -1 else 1 end as maxvalue
FROM TAB
GROUP BY STA, BL

Valid equivalent partitions in a range from -100 to 100?

I can't figure out this question.
A program which accepts an integer in the range -100 to +100:
1) How many valid equivalent partitions are there?
2) For which range what are minimum and maximum values?
3) Using BVA, what values need to be checked for the partitions?
So..., according to the equivalence testing, you can have a valid and invalid value. I supposed the invalid values would be anything less than -100 and greater than 100. However I can't find information about how to get equivalent partitions.
I mean, I can chose and say that it has 20 equivalent partitions, for example: -100 to -90 | -89 to 70 etc..., but: Is there a way to get this?
For the other questions: Is it possible get the previous partition so the minimum value would be -100 and the maximum -90?
Here's an example of how basic EPA & BVA applies to your data
So, practically in your case you'll have 3 values from equivalence partitioning and 4 values from boundary values analysis.
Don't forget to pay attention to 0. It's always a tricky point..
Good luck!
For Boundary value analysis, the main focus should be for the corner cases.
So for the above range, values needed to be checked for BVA are:
-101 -100 100 101 (This is as per ISTQB)
I assume that this is <-100;100> range so -100 and 100 are valid.
1) How many valid equivalent partitions are there?
Only one with any number from given range
2) For which range what are minimum and maximum values?
The minimum is -100 and the maximum is 100
3) Using BVA, what values need to be checked for the partitions?
Using BVA you have 6 values to be checked -101, - 100, -99, 99, 100 and 101 (the minumum/ the maximum and the next valid one just to find bugs in x>-100 except x>=-100, i.e. when programmer wrote x>-100 and you will check only -100 you will not find a bug, if you check also -99 you will find a bug).
1) How many valid equivalent partitions are there?
Theoretically for your range of -100 and 100 there would be three equivalent class partitions:
1) one partition having values below -100 i.e. -101,-102 etc. These are invalid values class.
2) Second partition having values between -100 and 100(including -100 and 100). These are valid values class.
3) Third partition having values greater than 100 i.e. 101,102 etc. These are invalid values class.
Now you can choose one value from each partition. For example,
1) You can choose -118 from first class(invalid class partition).
2) You can choose 70 from second class(valid class partition).
3) You can choose 170 from third class(invalid class partition).
But in my view if you want to check with more values you can do more partitions within class -100 to 100. For example you can divide it into -100 to -51, -50 to 0, 1 to 50, 51 to 100. Then you can choose one value from each of these partitions.
The main purpose of ECP is to reduce the number of test cases(test values) so if you have enough time then you can choose more than one value from each class or you can do make more classes and choose values from them.
2) For which range what are minimum and maximum values?
1) For first class minimum value cannot be described, maximum value is -101.
2) For second class minimum value is -100 and maximum value is 100.
3) For Third class minimum value is 101 and maximum value cannot be described.
3) Using BVA, what values need to be checked for the partitions?
For BVA following values need to be checked:
1) Value immediately below minimum value i.e. -101.
2) Minimum value i.e. -100.
3) Value immediately above minimum value of range i.e. -99.
4) value immediately below than maximum value of range i.e. 99.
5) Maximum value of range i.e. 100.
6) Value immediately above maximum value of range i.e. 101.
A program which accepts an integer in the range -100 to +100:
In Equivalance:
Test Scenario # Test Scenario Description Expected Outcome
Class I: values < -100 => invalid class
Class II: -100 to +100 => valid class
Class III: values > +100 => invalid class
In BVA,
1) Test cases with test data exactly as the input boundaries of input domain i.e. values -100 and +100 in our case.
2) Test data with values just below the extreme edges of input domains i.e. values -101 and +99.
3) Test data with values just above the extreme edges of input domain i.e. values -99 and +101.

Generating Even Random Numbers

I need a code to generate only random EVEN numbers 2-100
There is tutorials on the web that generate random numbers but they're odd and even.
Please understand i just need even numbers to generate.
1, generate numbers 1-50
2, multiply all the numbers by 2
all numbers multiplied by 2 are even
This will work:
NSInteger evenNumber = (arc4random_uniform(50) + 1) * 2;
arc4random_uniform(50) will give results in the range 0 - 49. Adding 1 gives a value in the range 1 - 50. Multiplying by two gives you even numbers in the range 2 - 100.