Dealing with imprecise, jittery gps signal - c#

My goal is to use GPS to measure the distance that I moved with my phone. My problem is, that the results are imprecise. I've used the following code to calculate the distance:
public double getDistance(GeoCoordinate p1, GeoCoordinate p2)
{
double d = p1.Latitude * 0.017453292519943295;
double num3 = p1.Longitude * 0.017453292519943295;
double num4 = p2.Latitude * 0.017453292519943295;
double num5 = p2.Longitude * 0.017453292519943295;
double num6 = num5 - num3;
double num7 = num4 - d;
double num8 = Math.Pow(Math.Sin(num7 / 2.0), 2.0) + ((Math.Cos(d) * Math.Cos(num4)) * Math.Pow(Math.Sin(num6 / 2.0), 2.0));
double num9 = 2.0 * Math.Atan2(Math.Sqrt(num8), Math.Sqrt(1.0 - num8));
return (6376500.0 * num9);
}
This is my OnLocationChanged implementation:
bool begin = true;
public void OnLocationChanged(Location location)
{
_aktuellerOrt = location;
//aktuellerOrt.Speed is always 0, so I cannot use that.
if (_aktuellerOrt == null)
{
//message
}
else
{
if (_aktuellerOrt.Accuracy > 70) //I found values around 130 to be more or less good
{
_locationText.Text = String.Format("{0}, {1}", _aktuellerOrt.Latitude, _aktuellerOrt.Longitude);
GeoJetzt = new GeoCoordinate();
GeoJetzt.Latitude = _aktuellerOrt.Latitude;
GeoJetzt.Longitude = _aktuellerOrt.Longitude;
if (beginn)
{
GeoVorher = new GeoCoordinate();
GeoVorher.Latitude = _aktuellerOrt.Latitude;
GeoVorher.Longitude = _aktuellerOrt.Longitude;
beginn = false;
}
else
{
double abstand = getDistance(GeoVorher, GeoJetzt);
weg += abstand;
if (weg >= 1)
_distanzText.Text = weg + " kilometers";
else
_distanzText.Text = (weg * 1000) + " meters";
_distanzText.Text += " (" + abstand + ", " + _aktuellerOrt.Accuracy + ")";
GeoVorher = new GeoCoordinate();
GeoVorher.Latitude = _aktuellerOrt.Latitude;
GeoVorher.Longitude = _aktuellerOrt.Longitude;
}
}
else
_locationText.Text = "Too coarse, now " + _aktuellerOrt.Accuracy + ".";
}
}
The problem is, I get values for abstand within the kilometer range while not moving the phone.
What are best practices for imprecise signals? My goal is to measure the distance while jogging or running, so 10 - 16 km/h.

Related

Amortisation schedule, monthly principal decreasing instead of increasing

Hi I am trying to create a amortization schedule , which shows the EMI , Principal , Interest and the new Principal for next month. The problem is that the monthly principal instead of increasing keeps on decreasing. As far as from searching the net , I am doing the right calculations. What am I missing?
decimal principal = 312500;
decimal rate = 3.50M;
decimal EMI;
decimal monthlyInterest;
decimal monthlyPrincipal;
decimal newPrincipalBalance;
decimal downPayment = 62500;
decimal actualPrincipal = principal - downPayment;
for (int i = 0; i <= 24; i++)
{
Console.WriteLine("principal " + actualPrincipal);
EMI = Math.Round(monthlyPayments(actualPrincipal, rate, 30));
Console.WriteLine("EMI " + EMI);
monthlyInterest = actualPrincipal * rate / 12;
monthlyInterest = Math.Round((actualPrincipal * rate / 100) / 12);
Console.WriteLine("monthlyInterest " + monthlyInterest);
monthlyPrincipal = Math.Round(EMI - monthlyInterest);
Console.WriteLine("monthlyPrincipal " + monthlyPrincipal);
newPrincipalBalance = Math.Round(actualPrincipal - monthlyPrincipal);
Console.WriteLine("newPrincipalBalance " + newPrincipalBalance);
Console.WriteLine("===================================");
actualPrincipal = newPrincipalBalance;
}
}
public static decimal monthlyPayments(decimal actualPrincipal, decimal rate, int years)
{
rate = rate / 1200;
years = years * 12;
decimal F = (decimal)Math.Pow((double)(1 + rate), years);
return actualPrincipal * (rate * F) / (F - 1);
}
These are first few of my results where the monthly principal is decreasing
This is what the expected result is :
This is the formula for calculating the monthlyPayments
I found what I was doing wrong. As the EMI remains fixed for the entire period of the loan, it should have been kept outside the loop.
Console.WriteLine("principal " + actualPrincipal);
EMI = Math.Round(monthlyPayments(actualPrincipal, rate, 30));
Console.WriteLine("EMI " + EMI);
for (int i = 0; i <= 24; i++)
{
monthlyInterest = actualPrincipal * rate / 12;
monthlyInterest = Math.Round((actualPrincipal * rate / 100) / 12);
Console.WriteLine("monthlyInterest " + monthlyInterest);
monthlyPrincipal = Math.Round(EMI - monthlyInterest);
Console.WriteLine("monthlyPrincipal " + monthlyPrincipal);
newPrincipalBalance = Math.Round(actualPrincipal - monthlyPrincipal);
Console.WriteLine("newPrincipalBalance " + newPrincipalBalance);
Console.WriteLine("===================================");
actualPrincipal = newPrincipalBalance;
}

Having trouble calculating prices

I'm trying to create a booking form in C#, where a user can select multiple tickets for one or more attraction site. They can also add a promotion code for a 10% discount on the total price if the code contains an "X". I'm having trouble with calculating the total price before the promotion discount can be taken away. I thought I could make the default value of each attraction "0" so when the individual prices are added for dblPrice, but that doesn't work.
Here's what I have so far -
//declared variables
double dblPrice;
double dblDiscount;
double dblRollprice, dblWaterprice, dblKidsprice, dblLiveprice, dblActionprice, dblThrillprice, dbl4dprice;
double dblTotalprice;
string strRollquantity, strWaterquantity, strKidsquantity, strLivequantity, strActionquantity, strThrillquantity, str4dquantity;
int intWaterquantity, intRollquantity, intKidsquantity, intLivequantity, intActionquantity, intThrillquantity, int4dquantity;
string strPromocode;
strPromocode = txtPromocode.Text;
strRollquantity = txtRollquantity.Text;
strWaterquantity = txtWaterquantity.Text;
strKidsquantity = txtKidsquantity.Text;
strLivequantity = txtLivequantity.Text;
strActionquantity = txtActionquantity.Text;
strThrillquantity = txtThrillquantity.Text;
str4dquantity = txt4dquantity.Text;
int.TryParse(strRollquantity, out intRollquantity);
int.TryParse(strWaterquantity, out intWaterquantity);
int.TryParse(strKidsquantity, out intKidsquantity);
int.TryParse(strLivequantity, out intLivequantity);
int.TryParse(strActionquantity, out intActionquantity);
int.TryParse(strThrillquantity, out intThrillquantity);
int.TryParse(str4dquantity, out int4dquantity);
dblRollprice = 15.00;
dblWaterprice = 15.00;
dblActionprice = 15.00;
dblKidsprice = 15.00;
dblLiveprice = 15.00;
dbl4dprice = 15.00;
dblThrillprice = 15.00;
if (chkRoll.Checked)
{
dblRollprice = dblRollprice * intRollquantity;
}
if (chkWater.Checked)
{
dblWaterprice = dblWaterprice * intWaterquantity;
}
if (chkKids.Checked)
{
dblKidsprice = 15.00 * intKidsquantity;
}
if (chkLive.Checked)
{
dblLiveprice = 15.00 * intLivequantity;
}
if (chkAction.Checked)
{
dblActionprice = 15.00 * intActionquantity;
}
if (chkThrill.Checked)
{
dblThrillprice = 15.00 * intThrillquantity;
}
if (chk4d.Checked)
{
dbl4dprice = 15.00 * int4dquantity;
}
dblPrice = dblRollprice + dblWaterprice + dblThrillprice + dblLiveprice + dblActionprice + dbl4dprice + dblKidsprice;
if (strPromocode == "X")
{
dblDiscount = dblPrice / 100 * 10;
dblTotalprice = dblPrice - dblDiscount;
Response.Write("Price is: " + dblTotalprice + "<br />");
}

Excel RATE function in .NET CORE

I tried to use RATE function in .NET CORE project.
There is a Visual Basic library I wanted to use but it does not work with .NET CORE.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.financial.rate?view=netframework-4.7.2
Are there any other ways to use it or should I calculate it explicitly? How? I can't find any explanation of this function.
According to #omajid comment I transform official VB code to C#.
This is all you need to use Rate method without dependency on Microsoft.VisualBasic.dll which is lacking this method in .NET CORE.
private double Rate(double NPer, double Pmt, double PV, double FV = 0, DueDate Due = DueDate.EndOfPeriod, double Guess = 0.1)
{
double dTemp;
double dRate0;
double dRate1;
double dY0;
double dY1;
int I;
// Check for error condition
if (NPer <= 0.0)
throw new ArgumentException("NPer must by greater than zero");
dRate0 = Guess;
dY0 = LEvalRate(dRate0, NPer, Pmt, PV, FV, Due);
if (dY0 > 0)
dRate1 = (dRate0 / 2);
else
dRate1 = (dRate0 * 2);
dY1 = LEvalRate(dRate1, NPer, Pmt, PV, FV, Due);
for (I = 0; I <= 39; I++)
{
if (dY1 == dY0)
{
if (dRate1 > dRate0)
dRate0 = dRate0 - cnL_IT_STEP;
else
dRate0 = dRate0 - cnL_IT_STEP * (-1);
dY0 = LEvalRate(dRate0, NPer, Pmt, PV, FV, Due);
if (dY1 == dY0)
throw new ArgumentException("Divide by zero");
}
dRate0 = dRate1 - (dRate1 - dRate0) * dY1 / (dY1 - dY0);
// Secant method of generating next approximation
dY0 = LEvalRate(dRate0, NPer, Pmt, PV, FV, Due);
if (Math.Abs(dY0) < cnL_IT_EPSILON)
return dRate0;
dTemp = dY0;
dY0 = dY1;
dY1 = dTemp;
dTemp = dRate0;
dRate0 = dRate1;
dRate1 = dTemp;
}
throw new ArgumentException("Can not calculate rate");
}
private double LEvalRate(double Rate, double NPer, double Pmt, double PV, double dFv, DueDate Due)
{
double dTemp1;
double dTemp2;
double dTemp3;
if (Rate == 0.0)
return (PV + Pmt * NPer + dFv);
else
{
dTemp3 = Rate + 1.0;
// WARSI Using the exponent operator for pow(..) in C code of LEvalRate. Still got
// to make sure that they (pow and ^) are same for all conditions
dTemp1 = Math.Pow(dTemp3, NPer);
if (Due != 0)
dTemp2 = 1 + Rate;
else
dTemp2 = 1.0;
return (PV * dTemp1 + Pmt * dTemp2 * (dTemp1 - 1) / Rate + dFv);
}
}
private const double cnL_IT_STEP = 0.00001;
private const double cnL_IT_EPSILON = 0.0000001;
enum DueDate
{
EndOfPeriod = 0,
BegOfPeriod = 1
}

Restart program from a certain line with an if statement?

could anyone help me restart my program from line 46 if the user enters 1 (just after the comment where it states that the next code is going to ask the user for 2 inputs) and if the user enters -1 end it. I cannot think how to do it. I'm new to C# any help you could give would be great!
class Program
{
static void Main(string[] args)
{
//Displays data in correct Format
List<float> inputList = new List<float>();
TextReader tr = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
String input = Convert.ToString(tr.ReadToEnd());
String[] items = input.Split(',');
Console.WriteLine("Point Latitude Longtitude Elevation");
for (int i = 0; i < items.Length; i++)
{
if (i % 3 == 0)
{
Console.Write((i / 3) + "\t\t");
}
Console.Write(items[i]);
Console.Write("\t\t");
if (((i - 2) % 3) == 0)
{
Console.WriteLine();
}
}
Console.WriteLine();
Console.WriteLine();
// Ask for two inputs from the user which is then converted into 6 floats and transfered in class Coordinates
Console.WriteLine("Please enter the two points that you wish to know the distance between:");
string point = Console.ReadLine();
string[] pointInput = point.Split(' ');
int pointNumber = Convert.ToInt16(pointInput[0]);
int pointNumber2 = Convert.ToInt16(pointInput[1]);
Coordinates distance = new Coordinates();
distance.latitude = (Convert.ToDouble(items[pointNumber * 3]));
distance.longtitude = (Convert.ToDouble(items[(pointNumber * 3) + 1]));
distance.elevation = (Convert.ToDouble(items[(pointNumber * 3) + 2]));
distance.latitude2 = (Convert.ToDouble(items[pointNumber2 * 3]));
distance.longtitude2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 1]));
distance.elevation2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 2]));
//Calculate the distance between two points
const double PIx = 3.141592653589793;
const double RADIO = 6371;
double dlat = ((distance.latitude2) * (PIx / 180)) - ((distance.latitude) * (PIx / 180));
double dlon = ((distance.longtitude2) * (PIx / 180)) - ((distance.longtitude) * (PIx / 180));
double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos((distance.latitude) * (PIx / 180)) * Math.Cos((distance.latitude2) * (PIx / 180)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double ultimateDistance = (angle * RADIO);
Console.WriteLine("The distance between your two points is...");
Console.WriteLine(ultimateDistance);
//Repeat the program if the user enters 1, end the program if the user enters -1
Console.WriteLine("If you wish to calculate another distance type 1 and return, if you wish to end the program, type -1.");
Console.ReadLine();
if (Convert.ToInt16(Console.ReadLine()) == 1);
{
//here is where I need it to repeat
}
bool exit = false;
do
{
Console.WriteLine("Please enter the two points that you wish to know the distance between:");
...
Console.WriteLine("If you wish to calculate another distance type 1 and return, if you wish to end the program, type -1.");
string input;
do
{
input = Console.ReadLine().Trim();
}
while (input != "1" && input != "-1");
if (input == -1) exit = true;
}
while (!exit);
But you would do much better to think about pushing logic into methods and functions such that you program is built up of much smaller building blocks.
You should be aiming towards something like this:
bool exit = false;
do
{
Point[] points = ReadCoordinates();
Whatever result = CalculateWhatever();
DisplayResults(results);
exit = ShouldExit();
}
while (!exit);
This makes the outer loop of your program self documenting and the methods self explanatory.
if (Convert.ToInt16(Console.ReadLine()) == 1);
{
Main(args)
}
That's quite an odd thing to do.
Have you looked into the while loop?
You could make a method of the above structure and loop it, while userinput equals 1
if it equals -1, call the following statement
Application.Exit()
Can use Goto to make the program loop on user input.
public static void Main(string[] args)
{
RestartApplication:
//// Displays data in correct Format
TextReader textReader = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
var input = Convert.ToString(textReader.ReadToEnd());
var items = input.Split(',');
Console.WriteLine("Point Latitude Longtitude Elevation");
for (var i = 0; i < items.Length; i++)
{
if (i % 3 == 0)
{
Console.Write((i / 3) + "\t\t");
}
Console.Write(items[i]);
Console.Write("\t\t");
if (((i - 2) % 3) == 0)
{
Console.WriteLine();
}
}
Console.WriteLine();
Console.WriteLine();
//// Ask for two inputs from the user which is then converted into 6 floats and transferred in class Coordinates
Console.WriteLine("Please enter the two points that you wish to know the distance between:");
var point = Console.ReadLine();
string[] pointInput;
if (point != null)
{
pointInput = point.Split(' ');
}
else
{
goto RestartApplication;
}
var pointNumber = Convert.ToInt16(pointInput[0]);
var pointNumber2 = Convert.ToInt16(pointInput[1]);
var distance = new Coordinates
{
Latitude = Convert.ToDouble(items[pointNumber * 3]),
Longtitude = Convert.ToDouble(items[(pointNumber * 3) + 1]),
Elevation = Convert.ToDouble(items[(pointNumber * 3) + 2]),
Latitude2 = Convert.ToDouble(items[pointNumber2 * 3]),
Longtitude2 = Convert.ToDouble(items[(pointNumber2 * 3) + 1]),
Elevation2 = Convert.ToDouble(items[(pointNumber2 * 3) + 2])
};
//// Calculate the distance between two points
const double PIx = 3.141592653589793;
const double Radio = 6371;
var dlat = (distance.Latitude2 * (PIx / 180)) - (distance.Latitude * (PIx / 180));
var dlon = (distance.Longtitude2 * (PIx / 180)) - (distance.Longtitude * (PIx / 180));
var a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(distance.Latitude * (PIx / 180)) * Math.Cos(distance.Latitude2 * (PIx / 180)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
var angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var ultimateDistance = angle * Radio;
Console.WriteLine("The distance between your two points is...");
Console.WriteLine(ultimateDistance);
//// Repeat the program if the user enters 1, end the program if the user enters -1
Console.WriteLine("If you wish to calculate another distance type 1 and return, if you wish to end the program, type -1.");
var userInput = Console.ReadLine();
if (Convert.ToInt16(userInput) == 1)
{
//// Here is where the application will repeat.
goto RestartApplication;
}
}
also did a bit of code formatting. Hope it helps.
You could do that with goto, be aware however that this is considered bad practice.
static void Main(string[] args)
{
...
MyLabel:
...
if (Convert.ToInt16(Console.ReadLine()) == 1);
{
//here is where I need it to repeat
goto MyLabel;
}

Getting runtime error, C# Converting Array to Int

for some reason this will compile but it comes out with an error at the end and I can't figure out why. The first part of the code is to display a table from a text file which works correctly, the second part doesn't.
I don't think it even gets to the Console.WriteLine bit, which was a way of checking whether it did. Can anyone see why?
Thanks for any help you can give!
class Program
{
static void Main(string[] args)
{
List<float> inputList = new List<float>();
TextReader tr = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
String input = Convert.ToString(tr.ReadToEnd());
String[] items = input.Split(',');
Console.WriteLine("Point Latitude Longtitude Elevation");
for (int i = 0; i < items.Length; i++)
{
if (i % 3 == 0)
{
Console.Write((i / 3) + "\t\t");
}
Console.Write(items[i]);
Console.Write("\t\t");
if (((i - 2) % 3) == 0)
{
Console.WriteLine();
}
}
Console.WriteLine();
Console.WriteLine();
// Ask for two bits of data which are then stored in Longtitude, Latitude and Elevation
Console.WriteLine("Please enter the two points that you wish to know the distance between:");
string point = Console.ReadLine();
string[] pointInput = point.Split(' ');
int pointNumber = Convert.ToInt16 (pointInput[0]);
int pointNumber2 = Convert.ToInt16 (pointInput[1]);
int Latitude = (Convert.ToInt16(items[pointNumber*3]));
int Longtitude = (Convert.ToInt16(items[(pointNumber*3)+1]));
int Elevation = (Convert.ToInt16(items[(pointNumber*3)+2]));
int Latitude2 = (Convert.ToInt16(items[pointNumber2 * 3]));
int Longtitude2 = (Convert.ToInt16(items[(pointNumber2 * 3) + 1]));
int Elevation2 = (Convert.ToInt16(items[(pointNumber2 * 3) + 2]));
Console.WriteLine("Latitude");
Console.WriteLine("Latitude2");
You are using decimal values, which cannot be converted into Int16. So use float.
Also, Outputting "Latitude" will write the variable's name, not its value.
I modified your code:
float Latitude = (float.Parse(items[pointNumber*3]));
float Longtitude = (float.Parse(items[(pointNumber*3)+1]));
float Elevation = (float.Parse(items[(pointNumber*3)+2]));
float Latitude2 = (float.Parse(items[pointNumber2 * 3]));
float Longtitude2 = (float.Parse(items[(pointNumber2 * 3) + 1]));
float Elevation2 = (float.Parse(items[(pointNumber2 * 3) + 2]));
Console.WriteLine(Latitude);
Console.WriteLine(Latitude2);
Your input strings are of floating values, not integers. You can parse to floats like this:
float Latitude = (Convert.ToSingle(items[pointNumber * 3]));
float Longtitude = (Convert.ToSingle(items[(pointNumber * 3) + 1]));
float Elevation = (Convert.ToSingle(items[(pointNumber * 3) + 2]));
float Latitude2 = (Convert.ToSingle(items[pointNumber2 * 3]));
float Longtitude2 = (Convert.ToSingle(items[(pointNumber2 * 3) + 1]));
float Elevation2 = (Convert.ToSingle(items[(pointNumber2 * 3) + 2]));

Categories