How do you make an equation in swift with multiple variables? [closed] - swift

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to make something and I am trying to use an equation in Swift. This is the equation:
((((((((((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) * ((((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) /1005.967 * (3.14159 * ((d / 100)/2) * ((d / 100)/2)))) - (((calculation.mempty/1000) + ((calculation.vol * 0.972)/1000))/2) * calculation.g) / (((calculation.mempty/1000) + ((calculation.vol * 0.972)/1000))/2)) * ((1005.967 * (calculation.vol / 1000000) / (((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) * 6894.76))))) * 2) sin 2(calculation.theta) / calculation.g) * (1 - calculation.dc))
This might seem complicated but if you understand it, it isn't (for a computer). All the variables have been defined and the error message says:
Missing argument for parameter 'verbatim' in call and another error that says: Unterminated string literal
I am unsure why and whenever I google it up, an actual result never comes up and when one does, it's always about Playgrounds, not the actual Swift that is used to make apps and stuff.
EDIT:
Here is the reproducible example:
'''
//
// ContentView.swift
// MRE File
//
// Created by Go Peter on 2021/06/18.
//
import SwiftUI
struct Calculation {
var vbottle:Int = 2
var g:Int = Int(9.807)
var vol:Int = 250
var d:Int = 2
var theta:Int = 90
var psi:Int = 40
var mempty:Int = 70
}
struct ContentView: View {
var body: some View {
Text("\(((((((((((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) * ((((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) /1005.967 * (3.14159 * ((d / 100)/2) * ((d / 100)/2)))) - (((calculation.mempty/1000) + ((calculation.vol * 0.972)/1000))/2) * calculation.g) / (((calculation.mempty/1000) + ((calculation.vol * 0.972)/1000))/2)) * ((1005.967 * (calculation.vol / 1000000) / (((calculation.d/100)/2) * Int(0.98) * (sqrt(calculation.vbottle * 1005.967 * (calculation.psi + (calculation.psi * (calculation.vbottle - (calculation.vol/1000))/2)/2) * 6894.76))))) * 2) sin 2(calculation.theta) / calculation.g) * (1 - calculation.dc)))m")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
'''

There are a few errors I see:
You need a space in /1005.967 after the / operator -- there are likely other spots like this as well.
Near the end of the expression, you have a floating sin that doesn't have an operator before it and isn't followed with parenthesis at all
There are many spots where you refer to just d, but probably mean calculation.d
You have a mismatched number of open and closed parenthesis.
You don't actually have a Calculation property defined on your view, so there's nothing to do the calculations on yet.
Because I don't know the intent of some of this stuff, I can't actually fix it for you. But, I'd recommend trying to clean up the code a little -- at the least, it'll make it easier to debug.
To start, move this out of the interpolated String and into a computed property:
var calc : Int { //Int? -- see comment about this
//your calculation here
}
var body: some View {
Text("\(calc)m")
}
Then, I'd break the calculation up into much smaller expressions that are more readable and would let you find your errors more easily than trying to sift through so many parenthesis, etc. Even a computer can theoretically handle a long, tough-to-read expression, it makes it a challenging debugging issue for us humans.
I'd also be really surprised if you truly want Int for these properties. You have a bunch of spots where you're doing things like Int(0.98), which doesn't make sense, because it'll get rounded to 1. Perhaps you instead want to use Double or Float for everything? You'll see as you start breaking the instructions up and the compiler starts to find more errors once it can parse everything correctly that you're going to end up with type mismatches between things like Int and Double in the existing expressions.

Related

Distance calculation in query with slick database

I am using slick with scala to find the distance of the user to the closes spot. But I'm not sure how to properly do this with slick. I've tried different ways, but I keep getting errors with symbols or not getting the appropriate answers. The SQL query that I'm looking to convert is:
SELECT *, 3956 * 2 * sin(sqrt( pow(sin((spot_lat -
abs(lat)) * Pi/180 / 2),2) + cos(spot_lat * Pi/180 ) * cos(abs(lat) * Pi/180) *
pow(sin((spot_lon – lon) * Pi/180 / 2), 2) )) as distance
FROM Spots
Where lat and lon have been given as inputs
First, define your database trig functions so that they can be used by slick:
val dbSin = SimpleFunction.unary[Double, Double]("sin")
val dbCos = SimpleFunction.unary[Double, Double]("cos")
val dbSqrt = SimpleFunction.unary[Double, Double]("sqrt")
val dbPow = SimpleFunction.binary[Double, Double, Double]("pow")
val dbAbs = SimpleFunction.unary[Double, Double]("abs")
When you use these functions in a slick query, it generates the SQL query with calls to the database function. Also, make sure that the trig functions are defined in the database you are using. Read more about it at: http://slick.typesafe.com/doc/2.1.0/userdefined.html
Now you can use these functions in your query:
import scala.math._
def distance(lat: Double, lon: Double) = globals.spots map { spot =>
3956 * 2 * dbSin(dbSqrt(dbPow(dbSin((spot.splot_lat - Pi / 180.0 / 2.0), 2) +
dbCos(spot.spot_lat * Pi / 180.0) * dbCos(dbAbs(lat) * Pi / 180.0) *
dbPow(dbSin((spot.spot_lon - lon) * Pi / 180.0 / 2.0), 2)))
}
Please double check that I copied the equation correctly.

Swift compound arithmetic operation ERROR

var ret = -100.0 + (2.0 * 1.3) + (3.0 * 4.0) + (0.2 * 2.0 * 2.0) + 0.1 * 2.0 * 3.0
//output: Cannot invoke '+' with an argument list of type '($T24, $T31)'
When I perform the operation above, error occurs, it's very strange! Is it too complex for swift to compute?
The full error message can be found in the Build log in the Report Navigator:
main.swift:15:66: error: cannot invoke '+' with an argument list of type '($T24, $T31)'
var ret = -100.0 + (2.0 * 1.3) + (3.0 * 4.0) + (0.2 * 2.0 * 2.0) + 0.1 * 2.0 * 3.0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
main.swift:15:66: note: expression was too complex to be solved in reasonable time;
consider breaking up the expression into distinct sub-expressions
var ret = -100.0 + (2.0 * 1.3) + (3.0 * 4.0) + (0.2 * 2.0 * 2.0) + 0.1 * 2.0 * 3.0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
So yes, this expression is too complex for the current (beta 6) Swift compiler.
I would suggest to file a bug report.
At present, the only workaround seems to be to split the expression in two parts,
e.g.
var ret = -100.0 + (2.0 * 1.3) + (3.0 * 4.0)
ret += (0.2 * 2.0 * 2.0) + 0.1 * 2.0 * 3.0
Of course the parentheses are not necessary here, but removing them does not solve
the problem with the original expression.

What causes the retired instructions to increase?

I have a 496*O(N^3) loop. I am performing a blocking optimization technique where I'm operating 2 images at a time instead of 1. In raw terms, I am unrolling the outer loop. (The non-unrolled version of the code is as shown below: ) b.t.w I'm using Intel Xeon X5365 machine that has 8 cores and it has 3GHz clock, 1333MHz bus frequency, Shared 8MB L2( 4 MB shared between every 2 core), L1-I 32KB,L1-D 32KB .
for(imageNo =0; imageNo<496;imageNo++){
for (unsigned int k=0; k<256; k++)
{
double z = O_L + (double)k * R_L;
for (unsigned int j=0; j<256; j++)
{
double y = O_L + (double)j * R_L;
for (unsigned int i=0; i<256; i++)
{
double x[1] = {O_L + (double)i * R_L} ;
double w_n = (A_n[2] * x[0] + A_n[5] * y + A_n[8] * z + A_n[11]) ;
double u_n = ((A_n[0] * x[0] + A_n[3] * y + A_n[6] * z + A_n[9] ) / w_n);
double v_n = ((A_n[1] * x[0] + A_n[4] * y + A_n[7] * z + A_n[10]) / w_n);
for(int loop=0; loop<1;loop++)
{
px_x[loop] = (int) floor(u_n);
px_y[loop] = (int) floor(v_n);
alpha[loop] = u_n - px_x[loop] ;
beta[loop] = v_n - px_y[loop] ;
}
if(px_y[0]>=0 && px_y[0]<(int)threadCopy[0].S_y)
{
if (px_x[0]>=0 && px_x[0]<(int)threadCopy[0].S_x )
///////////////////(i,j) pixels ///////////////////////////////
pixel_1[0] = threadCopy[0].I_n[px_y[0] * threadCopy[0].S_x + px_x[0]];
else
pixel_1[0] =0.0;
if (px_x[0]+1>=0 && px_x[0]+1<(int)threadCopy[0].S_x)
/////////////////// (i+1, j) pixels/////////////////////////
pixel_1[2] = threadCopy[0].I_n[px_y[0] * threadCopy[0].S_x + (px_x[0]+1)];
else
pixel_1[2] = 0.0;
}
else{
pixel_1[0] =0.0;
pixel_1[2] =0.0;
}
if( px_y[0]+1>=0 && px_y[0]+1<(int)threadCopy[0].S_y)
{
if (px_x[0]>=0 && px_x[0]<(int)threadCopy[0].S_x)
pixel_1[1] = threadCopy[0].I_n[(px_y[0]+1) * threadCopy[0].S_x + px_x[0]];
else
pixel_1[1] = 0.0;
if (px_x[0]+1>=0 && px_x[0]+1<(int)threadCopy[0].S_x)
pixel_1[3] = threadCopy[0].I_n[(px_y[0]+1) * threadCopy[0].S_x + (px_x[0]+1)];
else
pixel_1[3] = 0.0;
}
else{
pixel_1[1] = 0.0;
pixel_1[3] = 0.0;
}
pix_1 = (1.0 - alpha[0]) * (1.0 - beta[0]) * pixel_1[0] + (1.0 - alpha[0]) * beta[0] * pixel_1[1]
+ alpha[0] * (1.0 - beta[0]) * pixel_1[2] + alpha[0] * beta[0] * pixel_1[3];
f_L[k * L * L + j * L + i] += (float)(1.0 / (w_n * w_n) * pix_1);
}
}
}
I profiled the results using Intel Vtune-2013 (Using binary created from gcc-4.1) and I can see that there is 40% reduction in memory bandwidth usage which was expected because 2 images are being processed for every iteration.(f_L store operation causes 8 bytes of traffic for every voxel). This accounts to 11.7% reduction in bus cycles! Also, since the block size is increased in the inner loop, the resource stalls decrease by 25.5%. These 2 accounts for 18% reduction in response time.
The mystery question is, why are instruction retired increased by 7.9%? (Which accounts for increase in response time by 6.51%) - Possible reason I could this of is:
1. Since the number of branch instructions increase inside the block (and core architecture has 8 bit global history) retired branch instruction increased by 2.5%( Although, mis-prediction remained the same! I know, smells fishy right?!!). But I am still missing answer for the rest 5.4%! Could anyone please shed me light in any direction? I'm completely out of options and No way to think. Thanks a lot!!

Math calculation breaks in coffeescript

I have a long equation written in coffeescript, which turns in a function call when compiled to JavaScript:
CoffeeScript:
#u[idx] = #max(0, currU + t * ((#dU * ((#uu[right] + #uu[left] + #uu[bottom] + #uu[top]) -4 * currU) - d2) + currF * (1.0 - currU)))
JavaScript:
this.max(0, currU + t * ((this.dU * ((this.uu[right] + this.uu[left] + this.uu[bottom] + this.uu[top])(-4 * currU)) - d2) + currF * (1.0 - currU)));
The problem is this part:
((#uu[right] + #uu[left] + #uu[bottom] + #uu[top]) -4 * currU)
which turns into a function call:
((this.uu[right] + this.uu[left] + this.uu[bottom] + this.uu[top])(-4 * currU))
Can someone explain whats going on here.
You want this:
#u[idx] = #max(0, currU + t * ((#dU * ((#uu[right] + #uu[left] + #uu[bottom] + #uu[top]) - 4 * currU) - d2) + currF * (1.0 - currU)))
Which compiles to:
this.u[idx] = this.max(0, currU + t * ((this.dU * ((this.uu[right] + this.uu[left] + this.uu[bottom] + this.uu[top]) - 4 * currU) - d2) + currF * (1.0 - currU)));
The silly little issue is the -4, vs - 4.
Without the space, the compiler assumes the -4 * currU to be an argument to the 'function' , (#uu[right] + #uu[left] + #uu[bottom] + #uu[top]).

I'm developing iPhone web based app

So when I type something in the search bar of my app, it should send my latitude and longitude to the web server, so it can return me the nearest place where I can get the searched tag. I'm new to Titanium so can anyone help me?
You are basically sending a request via HTTP to your webservice and use the result (Take the function from #Muhammad Zeeshan to get longitude/latitude):
var xhr = Titanium.Network.createHTTPClient();
// write file on success
xhr.onload = function(){
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,this.apiFile);
f.write(this.responseData);
};
// error handling
xhr.onerror = function(){
Ti.API.error(this.status + ' - ' + this.statusText);
};
// open the client (and test HTTPS)
xhr.open('GET','http://example.com/api/?longitude=' + longitude + '&latitude=' + latitude);
// send the data
xhr.send();
// read file and return json
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, this.apiFile);
var contents = f.read();
var yourJson = JSON.parse(contents);
On the server Side you need some Webservice to talk to (you didn't specify a language you use on the server), but I presume you get the data via a MySQL database (others should be similar):
SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($latitude * PI() / 180) * COS(lat * PI() / 180) * COS(($longitude – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `locations` HAVING `distance`<=’10′ ORDER BY `distance` ASC
Add additional WHERE clauses to filter by tag as well.
If you are using TiStudio instead of TiDeveloper you can use the sample project GPS to start with. It's bundled in the download so just fire it up and use that code as a working copy to learn from.