How to solve unknown expected error in iphone app - objective-c

I am assigning a value to var with calculations but it shows syntax error i have seen all the brackets still it gives error here is my code .
appDelegate.Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee
- 5000) / appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) *
((appDelegate.Cost_Treatment_SevereEI *
appDelegate.Same_Vaccination_SevereEI_Annually) +
(appDelegate.Cost_Treatment_ModerateEI *
appDelegate.Same_Vaccination_ModerateEI_Annually) +
(appDelegate.Cost_Treatment_MildEI *
appDelegate.Same_Vaccination_MildEI_Annually)))) *
appDelegate.Same_Vaccination_Horses_Per_Premise;

As per Your code:-
float Same_Vaccination_Cost_Treatment_Annually_With_Guarantee;
float Cost_Treatment_SevereEI;
float Same_Vaccination_SevereEI_Annually;
float Cost_Treatment_MildEI;
float Same_Vaccination_MildEI_Annually;
float Same_Vaccination_Horses_Per_Premise;
float Same_Vaccination_Cost_Treatment_Annually_No_Guarantee;
float Cost_Treatment_ModerateEI;
float Same_Vaccination_ModerateEI_Annually;
Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=(((Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * (((Cost_Treatment_SevereEI * Same_Vaccination_SevereEI_Annually) + (Cost_Treatment_ModerateEI * Same_Vaccination_ModerateEI_Annually) + (Cost_Treatment_MildEI * Same_Vaccination_MildEI_Annually)) * Same_Vaccination_Horses_Per_Premise));
and you can also put Method like:-
Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * ((Cost_Treatment_SevereEI * Same_Vaccination_SevereEI_Annually) + (Cost_Treatment_ModerateEI * Same_Vaccination_ModerateEI_Annually) + (Cost_Treatment_MildEI * Same_Vaccination_MildEI_Annually)) * Same_Vaccination_Horses_Per_Premise;

If you are using exactly same
appDelegate.Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * ((appDelegate.Cost_Treatment_SevereEI * appDelegate.Same_Vaccination_SevereEI_Annually) + (appDelegate.Cost_Treatment_ModerateEI * appDelegate.Same_Vaccination_ModerateEI_Annually) + (appDelegate.Cost_Treatment_MildEI * appDelegate.Same_Vaccination_MildEI_Annually)))) * appDelegate.Same_Vaccination_Horses_Per_Premise;
then you have 2 braces extra
try
appDelegate.Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * ((appDelegate.Cost_Treatment_SevereEI * appDelegate.Same_Vaccination_SevereEI_Annually) + (appDelegate.Cost_Treatment_ModerateEI * appDelegate.Same_Vaccination_ModerateEI_Annually) + (appDelegate.Cost_Treatment_MildEI * appDelegate.Same_Vaccination_MildEI_Annually)) * appDelegate.Same_Vaccination_Horses_Per_Premise;

appDelegate.Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * ((appDelegate.Cost_Treatment_SevereEI * appDelegate.Same_Vaccination_SevereEI_Annually) + (appDelegate.Cost_Treatment_ModerateEI * appDelegate.Same_Vaccination_ModerateEI_Annually) + (appDelegate.Cost_Treatment_MildEI * appDelegate.Same_Vaccination_MildEI_Annually)) * appDelegate.Same_Vaccination_Horses_Per_Premise;
This will work. You were using too many brackets.

Related

Why do I get a value out of range: underflow and how do I work around it?

Is it possible to avoid value out of range: underflow with casting the values to some types explicitly?
The function that causes the exception is:
create or replace function produciblepwforregulation(now date, timerange integer, devicename character varying) returns void
language plpgsql
as $$
BEGIN
FOR t IN 1..timeRange BY 1
LOOP
BEGIN
insert into values
(value, time, devicename)
values ( 2200 +
600 *
round(abs(sin(((t) * 0.042))) + 0.1) *
(sin(sin(((t) * 2 / 2.0 / 2.155172413793103 * PI()))) /
abs(sin(sin(((t) * 2 / 2.0 / 2.155172413793103 * PI()))))) *
(3 - exp(-12.5 + floor((t) * 2 / 2.0 / 2.155172413793103) * 2.155172413793103 * PI() * 2 * round(abs(sin(((t) * 0.042))) + 0.1) - ((t) * 2 * PI())))
+
600 *
abs(round(abs(sin(((t) * 0.042))) + 0.1) - 1) *
(sin(sin(((t) / 2.0 / 4.310344827586207 * PI()))) /
abs(sin(sin(((t) / 2.0 / 4.310344827586207 * PI()))))) *
(3 - exp(-25.5 + floor((t) / 2.0 / 4.310344827586207) * 4.310344827586207 * PI() * 2 * abs(round(abs(sin(((t) * 0.042))) + 0.1) - 1) - ((t) * PI())))
+ 50 * sin(3 + (t) * 0.092) + 350 * sin(3 + (t) * 0.012),
now + t * INTERVAL '1 second' - 1 * INTERVAL '1 milliseconds',
devicename);
END;
END LOOP;
END
$$;
What kind of data types produce the underflow during the calculation?

Problem using inprod() to summarise linear predictor

I am having a problem when trying to summarise my aditive predictor:
mu[j] <- b0 + weights1[1] * A[j] + weights1[2] * A[j+1] + weights1[3] * A[j+2] + weights1[4] * A[j+3] +
weights1[5] * A[j+4] + weights1[6] * A[j+5] + weights1[7] * A[j+6] + weights1[8] * A[j+7] +
weights1[9] * A[j+8] + weights1[10] * A[j+9] + weights1[11] * A[j+10] + weights1[12] * A[j+11] +
weights2[1] * B[j] + weights2[2] * B[j+1] + weights2[3] * B[j+2] + weights2[4] * B[j+3] +
weights2[5] * B[j+4] + weights2[6] * B[j+5] + weights2[7] * B[j+6] + weights2[8] * B[j+7] +
weights2[9] * B[j+8] + weights2[10] * B[j+9] + weights2[11] * B[j+10] + weights2[12] * B[j+11]
by using inprod(). This is what I thought should be the equivalent:
mu[j] <- b0 + inprod(weights1[],A[j:(j+11)]) + inprod(weights2[],B[j:(j+11)])
While the model compiles and seems to work, it stays updating forever. Its been running for hours and it does not end while the first approach ends in few minutes.
These are the priors, just in case:
weights1[1] ~ dnorm(0,1.0E-6)
weights2[1] ~ dnorm(0,1.0E-6)
for(t in 2:12) {
weights1[t]~dnorm(weights1[t-1],tauweight1)}
for(t in 2:12) {
weights2[t]~dnorm(weights2[t-1],tauweight2)}
b0 ~ dnorm(0,.001)
tau ~ dgamma(0.001, 0.001)
sigma <- 1/sqrt(tau)
tauweight1~dgamma(1.0E-3,1.0E-3)
tauweight2~dgamma(1.0E-3,1.0E-3)
I am calling OpenBUGS from R using R2OpenBUGS just in case.
Thanks very much for your time!

calculate angle with three CGpoint

I have three CGpoint and I would like calculate the angle.
I drawn a little schema :
I tried with this code :
CGPoint u ;
u.x = 0;
u.y = - middleRectY;
CGPoint v ;
v.x = x1 - middelRectX;
v.y = y1 - middleRectY;
// formule = u.v / ( ||u|| * ||v||)
double cosa = (double)((u.x * v.x + u.y * v.y)) / sqrt(u.x * u.x + u.y * u.y) * sqrt(v.x * v.x + v.y * v.y);
// angle en degré
double angle = (180.0 / M_PI) * acos(cosa);
// Signe de l'angle
int sign = (u.x * v.y - u.y * v.x) > 0 ? 1 : -1;
rectYellow.transform = CGAffineTransformMakeRotation(angle*sign);
But my function return "nan" :/
Thx :)
I found the problem !
It just a probleme of parenthesis :
double cosa = ((u.x * v.x) + (u.y * v.y)) / (sqrt((u.x * u.x) + (u.y * u.y)) * sqrt((v.x * v.x) + (v.y * v.y)));
I don't understand why ?
Because the parentheses aren't necessary for multiplication ...

Distance using WGS84-ellipsoid

Consider points P1 (60°N, 20°E, 0) and P2 (60°N, 22°E, 0) on the
surface of the Earth
What is the shortest distance between the points P1 and P2, when the shape of the
Earth is modeled using WGS-84 ellipsoid?
Unfortunately, Vincenty's algorithm fails to converge for some inputs.
GeographicLib provides an alternative which always converges (and
is also more accurate). Implementations in C++, C, Fortran, Javascript, Python, Java, and Matlab are provided. E.g., using the
Matlab package:
format long;
geoddistance(60,20,60,22)
->
111595.753650629
As pointed out in a comment to your question, you should use Vincenty's formula for inverse problem.
Answer to your question is: 111595.75 metres (or 60.257 nautical miles).
Javascript implementation of Vincenty's inverse formula, as copied from http://jsperf.com/vincenty-vs-haversine-distance-calculations:
/**
* Calculates geodetic distance between two points specified by latitude/longitude using
* Vincenty inverse formula for ellipsoids
*
* #param {Number} lat1, lon1: first point in decimal degrees
* #param {Number} lat2, lon2: second point in decimal degrees
* #returns (Number} distance in metres between points
*/
function distVincenty(lat1, lon1, lat2, lon2) {
var a = 6378137,
b = 6356752.314245,
f = 1 / 298.257223563; // WGS-84 ellipsoid params
var L = (lon2 - lon1).toRad();
var U1 = Math.atan((1 - f) * Math.tan(lat1.toRad()));
var U2 = Math.atan((1 - f) * Math.tan(lat2.toRad()));
var sinU1 = Math.sin(U1),
cosU1 = Math.cos(U1);
var sinU2 = Math.sin(U2),
cosU2 = Math.cos(U2);
var lambda = L,
lambdaP, iterLimit = 100;
do {
var sinLambda = Math.sin(lambda),
cosLambda = Math.cos(lambda);
var sinSigma = Math.sqrt((cosU2 * sinLambda) * (cosU2 * sinLambda) + (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda) * (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda));
if (sinSigma == 0) return 0; // co-incident points
var cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
var sigma = Math.atan2(sinSigma, cosSigma);
var sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
var cosSqAlpha = 1 - sinAlpha * sinAlpha;
var cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
if (isNaN(cos2SigmaM)) cos2SigmaM = 0; // equatorial line: cosSqAlpha=0 (§6)
var C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
lambdaP = lambda;
lambda = L + (1 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
} while (Math.abs(lambda - lambdaP) > 1e-12 && --iterLimit > 0);
if (iterLimit == 0) return NaN // formula failed to converge
var uSq = cosSqAlpha * (a * a - b * b) / (b * b);
var A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
var B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
var deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
var s = b * A * (sigma - deltaSigma);
s = s.toFixed(3); // round to 1mm precision
return s;
}
The Haversine Formula is commonly used (error < 0,5%)

Query to get records based on Radius in SQLite?

I have this query which does work fine in MySQL
SELECT ((ACOS(SIN(12.345 * PI() / 180) * SIN(lat * PI() / 180) +
COS(12.345 * PI() / 180) * COS(lat * PI() / 180) * COS((67.89 - lon) *
PI() / 180)) * 180 / PI()) * 60 * 1.1515 * 1.609344) AS distance, poi.*
FROM poi
WHERE lang='eng'
HAVING distance<='30'
distance is in Kilometers, the input is lat=12.345 and lon=67.89
The SQLite is 3, and I can't run custom functions with it as it's on Android. I also don't have acos() etc... as that is not part of the standard SQLite.
How would be the above query in SQLite?
Here is an implementation in Java for building a location based query on an Android device.
The idea comes from KennyTM (see accepted response) and implies the addition of 4 columns in your table to store values of sinus and cosinus of latitude and longitudes.
Here is the code preparing the data for a "Shop" table at insert time:
public static void injectLocationValues(ContentValues values, double latitude, double longitude) {
values.put(LocationColumns.LATITUDE, latitude);
values.put(LocationColumns.LONGITUDE, longitude);
values.put(LocationColumns.COSLAT, Math.cos(MathUtil.deg2rad(latitude)));
values.put(LocationColumns.SINLAT, Math.sin(MathUtil.deg2rad(latitude)));
values.put(LocationColumns.COSLNG, Math.cos(MathUtil.deg2rad(longitude)));
values.put(LocationColumns.SINLNG, Math.sin(MathUtil.deg2rad(longitude)));
}
public static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
You can then build your projection using the following function:
/**
* Build query based on distance using spherical law of cosinus
*
* d = acos(sin(lat1).sin(lat2)+cos(lat1).cos(lat2).cos(long2−long1)).R
* where R=6371 and latitudes and longitudes expressed in radians
*
* In Sqlite we do not have access to acos() sin() and lat() functions.
* Knowing that cos(A-B) = cos(A).cos(B) + sin(A).sin(B)
* We can determine a distance stub as:
* d = sin(lat1).sin(lat2)+cos(lat1).cos(lat2).(cos(long2).cos(long1)+sin(long2).sin(long1))
*
* First comparison point being fixed, sin(lat1) cos(lat1) sin(long1) and cos(long1)
* can be replaced by constants.
*
* Location aware table must therefore have the following columns to build the equation:
* sinlat => sin(radians(lat))
* coslat => cos(radians(lat))
* coslng => cos(radians(lng))
* sinlng => sin(radians(lng))
*
* Function will return a real between -1 and 1 which can be used to order the query.
* Distance in km is after expressed from R.acos(result)
*
* #param latitude, latitude of search
* #param longitude, longitude of search
* #return selection query to compute the distance
*/
public static String buildDistanceQuery(double latitude, double longitude) {
final double coslat = Math.cos(MathUtil.deg2rad(latitude));
final double sinlat = Math.sin(MathUtil.deg2rad(latitude));
final double coslng = Math.cos(MathUtil.deg2rad(longitude));
final double sinlng = Math.sin(MathUtil.deg2rad(longitude));
//#formatter:off
return "(" + coslat + "*" + LocationColumns.COSLAT
+ "*(" + LocationColumns.COSLNG + "*" + coslng
+ "+" + LocationColumns.SINLNG + "*" + sinlng
+ ")+" + sinlat + "*" + LocationColumns.SINLAT
+ ")";
//#formatter:on
}
It will inject a response column with the distance on which you need to apply the following formula to convert in kilometers:
public static double convertPartialDistanceToKm(double result) {
return Math.acos(result) * 6371;
}
If you want to order your query using the partial distance, you need to order DESC and not ASC.
Had the same issue while working on sqlite3 for ios, after playing a little with the formula here is a way to do it without using function from the sql side (pseudo-code):
Pre-calculate these value for each item you store in the database (and store them):
cos_lat = cos(lat * PI / 180)
sin_lat = sin(lat * PI / 180)
cos_lng = cos(lng * PI / 180)
sin_lng = sin(lng * PI / 180)
Pre-calculate these value at the search time (for a given position cur_lat,cur_lng)
CUR_cos_lat = cos(cur_lat * PI / 180)
CUR_sin_lat = sin(cur_lat * PI / 180)
CUR_cos_lng = cos(cur_lng * PI / 180)
CUR_sin_lng = sin(cur_lng * PI / 180)
cos_allowed_distance = cos(2.0 / 6371) # This is 2km
Your SQL query will look like this (replace CUR_* by the values you just calculated)
SELECT * FROM position WHERE CUR_sin_lat * sin_lat + CUR_cos_lat * cos_lat * (cos_lng* CUR_cos_lng + sin_lng * CUR_sin_lng) > cos_allowed_distance;
You can create 4 new columns, being sin and cos of lat and lon. Since cos(a+b) = cos a cos b - sin a sin b, and other appearances of sin and cos like SIN(12.345 * PI() / 180) can be calculated in the program before running the query, the big "distance" expression reduces to something of the form P * SIN_LAT + Q * COS_LAT + ... that can be handled by SQLite3.
BTW, see also Sqlite on Android: How to create a sqlite dist db function - to be used in the app for distance calculation using lat, long.