If we have migrations like:
V1_6__six.sql
V1_7__seven.sql
V1_8__eight.sql
V1_9__nine.sql
What should we use for the next version?
If we use V1_10 will that come after V1__9? Or would we need to prefix the single digit version numbers with a 0?
Really the question is: are version numbers sorted numerically or alphabetically?
In one word: numerically. As would be expected for a number.
For a definitive answer on how the sorting happens, you can refer to the code. This test is especially helpful.
#Test
public void testNumber() {
final MigrationVersion a1 = MigrationVersion.fromVersion("1.2.13.3");
final MigrationVersion a2 = MigrationVersion.fromVersion("1.2.3.3");
assertTrue(a1.compareTo(a2) > 0);
}
#Test
public void testLength1() {
final MigrationVersion a1 = MigrationVersion.fromVersion("1.2.1.3");
final MigrationVersion a2 = MigrationVersion.fromVersion("1.2.1");
assertTrue(a1.compareTo(a2) > 0);
}
#Test
public void testLength2() {
final MigrationVersion a1 = MigrationVersion.fromVersion("1.2.1");
final MigrationVersion a2 = MigrationVersion.fromVersion("1.2.1.1");
assertTrue(a1.compareTo(a2) < 0);
}
#Test
public void leadingZeroes() {
final MigrationVersion v1 = MigrationVersion.fromVersion("1.0");
final MigrationVersion v2 = MigrationVersion.fromVersion("001.0");
assertTrue(v1.compareTo(v2) == 0);
assertTrue(v1.equals(v2));
assertEquals(v1.hashCode(), v2.hashCode());
}
Related
I´ve tried looking up an answer but I was unable to deduct from any of those what exactly the issue here is. When I try to verify it says "Expected "," or "..." before numeric constant" and highlights "#define Odvesna"
#define A1 35
#define B1 15
#define Odvesna 10
#define Prepona 30
int O(int A1, int B1) {
int result;
result = 2*(A1+B1);
return result;
}
int S(int A1,int B1) {
int result;
result = A1*B1;
return result;
}
int St(int Odvesna,int Prepona) {
int result;
result = Prepona*Odvesna/2;
return result;
}
int Ot(int Odvesna,int Prepona) {
int result;
result = sqrt(pow(Odvesna)+pow(Prepona))+ Odvesna+Prepona;
return result;
}
void Vystup(O,S,St) {
Serial.print("O = ");Serial.println(O);
Serial.print("S = ");Serial.println(S);
Serial.print("St = ");Serial.println(St);
Serial.print("Ot = ");Serial.println(Ot);
/*******************SETUP**********************************/
void setup() {
Serial.begin(115200);
Vystup(O,S,St);
}
/**************MAIN PROGRAM********************************/
void loop() {
}
There is a } missing at the end of the Vystup() function.
You can't reuse #define names as variable names.
In this call: Vystup(O,S,St); you are passing function pointers as parameters.
int St(int Odvesna,int Prepona) {} is turned into int St(int 10,int 30) {} by the preprocessor, and the compiler doesn't understand what you mean by that.
You have to use other variable names when you define your functions.
Also, void Vystup(O,S,St) {} won't work; you are passing functions as parameters, not ints as you maybe think.
Run main
public class ThreadTest {
volatile int p = 0, q = 0;
public void test() throws InterruptedException {
Thread writeThread = new Thread(){
public void run(){
while (!isInterrupted()) {
p++;
q++;
}
}
};
Thread readThread = new Thread(){
public void run(){
while (!isInterrupted()) {
//p should not be less than q
if(p<q){
System.out.println("happen before violation p = " + p + ";q = " + q);
}
}
}
};
writeThread.start();
readThread.start();
Thread.currentThread().sleep(2);
writeThread.interrupt();
readThread.interrupt();
}
public static void main(String[] args) throws InterruptedException {
new ThreadTest().test();
}
}
output
happen before violation p = 736;q = 827
happen before violation p = 4635;q = 4657
happen before violation p = 6421;q = 6440
happen before violation p = 8719;q = 8803
You should read p AFTER q for being sure that their values are in "not less than" order:
int q_value = q; // Read 'q' once
int p_value = p; // Read 'p' AFTER q
if(p_value < q_value) // ... happens-before violation
In that case volatile modifier is required only for q, and it can be omitted for p without constraints' violation.
In you original code nothing prevents the compiler from reading p BEFORE q. So readThread can see p value from 736-th iteration of the writeThread, but see q value from 827-th iteration.
Or readThread can see p value from the beginning of the 100-th iteration(that is, p=100), but see q value from the end of that iteration(that is, q=101).
I have an assignment for my class that reads like this: Write a class called Stats. The constructor will take no input. There will be a method addData(double a) which will be used to add a value from the test program. Methods getCount(), getAverage() and getStandardDeviation() will return the appropriate values as doubles.
Here's what I have so far:
public class Stats
{
public Stats (double a)
{
a=0.0
}
public void addData(double a)
{
while (
sum=sum+a;
sumsq=sumsq+Math.pow(a,2)
count=count+1
}
public double getCount()
{
return count;
}
public double getAverage()
{
average=sum/count
return average;
}
public double getStandardDeviation()
{
private double sum=o;
private double count=0;
private double sumsq=0;
My problem is figuring out how to calculate the standard deviation using the variables I've defined.
Thanks guys!
You can't do this with the variables you defined. You need to keep the original data to be able to compute the formula
sigma = Math.sqrt( sum(Math.pow(x-mean, 2)) / count )
So,
(1) create private array or list into which you'll add your values in addData. That's all you need to do in addData.
(2) getCount = length of the list
(3) getAverage = sum of values in list / getCount()
(4) getStandardDeviation is something like
double avg = getAverage();
double cnt = getCount();
double sumsq = 0;
for (int i = 0; i < values.Count(); i++) {
sumsq += Math.pow(values[i] - avg, 2);
}
stdev = Math.sqrt(sumsq / cnt);
I'm working on a game which has tank battles on a tiled map. If a tank is on a cell, that cell is considered unpassable in the A* algorithm, therefore, whenever an unit needs to attack another, I need to plan a path which brings the attacker into range (if range=1, then next to the target).
Currently, I use an iterative approach with increasing radius to find a path to a nearby cell and choose a cell which minimizes the A-Cell-B distance. Unfortunately, this is slow for one unit, not to mention for 50 units.
Is there a way to extract a partial path from a regular A* search data structures?
Just for reference, here is the implementation I have.
Set<T> closedSet = U.newHashSet();
Map<T, T> cameFrom = U.newHashMap();
final Map<T, Integer> gScore = U.newHashMap();
final Map<T, Integer> hScore = U.newHashMap();
final Map<T, Integer> fScore = U.newHashMap();
final Comparator<T> smallestF = new Comparator<T>() {
#Override
public int compare(T o1, T o2) {
int g1 = fScore.get(o1);
int g2 = fScore.get(o2);
return g1 < g2 ? -1 : (g1 > g2 ? 1 : 0);
}
};
Set<T> openSet2 = U.newHashSet();
List<T> openSet = U.newArrayList();
gScore.put(initial, 0);
hScore.put(initial, estimation.invoke(initial, destination));
fScore.put(initial, gScore.get(initial) + hScore.get(initial));
openSet.add(initial);
openSet2.add(initial);
while (!openSet.isEmpty()) {
T current = openSet.get(0);
if (current.equals(destination)) {
return reconstructPath(cameFrom, destination);
}
openSet.remove(0);
openSet2.remove(current);
closedSet.add(current);
for (T loc : neighbors.invoke(current)) {
if (!closedSet.contains(loc)) {
int tentativeScore = gScore.get(current)
+ distance.invoke(current, loc);
if (!openSet2.contains(loc)) {
cameFrom.put(loc, current);
gScore.put(loc, tentativeScore);
hScore.put(loc, estimation.invoke(loc, destination));
fScore.put(loc, gScore.get(loc) + hScore.get(loc));
openSet.add(loc);
Collections.sort(openSet, smallestF);
openSet2.add(loc);
} else
if (tentativeScore < gScore.get(loc)) {
cameFrom.put(loc, current);
gScore.put(loc, tentativeScore);
hScore.put(loc, estimation.invoke(loc, destination));
fScore.put(loc, gScore.get(loc) + hScore.get(loc));
Collections.sort(openSet, smallestF);
}
}
}
}
return Collections.emptyList();
A solution that seems to work (replacing the last return Collections.emptyList();):
// if we get here, there was no direct path available
// find a target location which minimizes initial-L-destination
if (closedSet.isEmpty()) {
return Pair.of(false, Collections.<T>emptyList());
}
T nearest = Collections.min(closedSet, new Comparator<T>() {
#Override
public int compare(T o1, T o2) {
int d1 = trueDistance.invoke(destination, o1);
int d2 = trueDistance.invoke(destination, o2);
int c = U.compare(d1, d2);
if (c == 0) {
d1 = trueDistance.invoke(initial, o1);
d2 = trueDistance.invoke(initial, o2);
c = U.compare(d1, d2);
}
return c;
}
});
return Pair.of(true, reconstructPath(cameFrom, nearest));
Where the trueDistance gives the eucleidian distance of two points. (The base algorithm uses a simpler function yielding 1000 for X-X or YY neightbor, 1414 for XY neighbor).
I apologize if the title is not clear, but I couldn't explain it succinctly.
Given a vector of concentrations, I would like to round the maximum value to the next order of magnitude (i.e., 345 to 1000). Also, I would like to round the minimum value to the lower order of magnitude (i.e., 3.2 to 1). These concentrations may also be below 1 so for example 0.034 would need to be rounded to 0.01.
Any ideas?
I'm not sure about R, but this is a simple process to describe algorithmically.
Take the base 10 logarithm of the number, and apply a ceiling or floor to the result. Raise 10 to that power. Done.
You need a special case for 0 because you can't take a logarithm of 0.
Here's a simple function that does what you're after:
log10_ceiling <- function(x) {
10^(ceiling(log10(x)))
}
log10_ceiling(c(345, 3.2, 0.034))
# [1] 1000.0 10.0 0.1
Hadley's plyr package has an extremely flexible function called round_any which does this elegantly. Here is how you would call the function
round_any(x, accuracy, f = round)
In your case, x = 345, accuracy = 1000 and you want f = ceiling. So calling
round_any(x = 345, accuracy = 1000, f = ceiling)
would do the job
EDIT. Just saw that you wanted the maximum to be rounded up to ceiling and the minimum values rounded down to floor. change the f in the function call to achieve this.
The accepted answer by Mark Ransom is mostly correct.
Having implemented this in Java I have found some more areas that need to be addressed:
Negative numbers need to be handled specially if you want -375 to yield -1000
Ceiling for positive log values, floor + 1 for negative log values (the plus one is important if you want 0.456 to yield 1).
Here is my implementation in Java with passing unit tests
static double roundUpToNearestMagnitude(double n) {
if (n == 0d) return 1d;
boolean negative = n < 0;
double log = Math.log10(Math.abs(n));
double decimalPlaces = ((log > 0)) ? (Math.ceil(log)) : (Math.floor(log) + 1);
double rounded = Math.pow(10, decimalPlaces);
return negative ? -rounded : rounded;
}
#Test public void roundUpToNearestMagnitudeFifty() {
Assert.assertEquals(100d, roundUpToNearestMagnitude(50d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeFive() {
Assert.assertEquals(10d, roundUpToNearestMagnitude(5d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeZeroPointFive() {
Assert.assertEquals(1d, roundUpToNearestMagnitude(0.5d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeZeroPointZeroFive() {
Assert.assertEquals(.1d, roundUpToNearestMagnitude(0.05d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeZeroPointZeroZeroFive() {
Assert.assertEquals(.01d, roundUpToNearestMagnitude(0.005d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeNegativeFifty() {
Assert.assertEquals(-100d, roundUpToNearestMagnitude(-50d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeNegativeFive() {
Assert.assertEquals(-10d, roundUpToNearestMagnitude(-5d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeNegativeZeroPointFive() {
Assert.assertEquals(-1d, roundUpToNearestMagnitude(-0.5d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeNegativeZeroPointZeroFive() {
Assert.assertEquals(-.1d, roundUpToNearestMagnitude(-0.05d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeNegativeZeroPointZeroZeroFive() {
Assert.assertEquals(-.01d, roundUpToNearestMagnitude(-0.005d), 0.000001);
}
#Test public void roundUpToNearestMagnitudeZero() {
Assert.assertEquals(1, roundUpToNearestMagnitude(0d), 0.000001);
}
In case someone is interested, the following is Ostermiller's solution translated to Python:
def roundUpToNearestMagnitude(n):
if n == 0:
return 1
negative = n < 0
log = np.log10(abs(n))
if log > 0:
decimalPlaces = np.ceil(log)
else:
decimalPlaces = np.floor(log) + 1
rounded = np.power(10, decimalPlaces)
if negative:
return -rounded
else:
return rounded
def test_roundUpToNearestMagnitude():
assert(100 == roundUpToNearestMagnitude(50))
assert(10 == roundUpToNearestMagnitude(5))
assert(1 == roundUpToNearestMagnitude(0.5))
assert(.1 == roundUpToNearestMagnitude(0.05))
assert(.01 == roundUpToNearestMagnitude(0.005))
assert(-100 == roundUpToNearestMagnitude(-50))
assert(-10 == roundUpToNearestMagnitude(-5))
assert(-1 == roundUpToNearestMagnitude(-0.5))
assert(-.1 == roundUpToNearestMagnitude(-0.05))
assert(-.01 == roundUpToNearestMagnitude(-0.005))
assert(1 == roundUpToNearestMagnitude(0))