# Need help in using Tan Inverse of X/Y & convert the result into percentage

### Question

• Hi Guys,

This the code, i'l using to convert the given values into an angle using tan inverse x/y.

For the value, tan inverse -100/-300, i'm getting 0. What am i doing wrong here?? Thank you.

```int PT1BB;
int.TryParse(t_PT1BB.Text, NumberStyles.Any,
CultureInfo.InvariantCulture.NumberFormat, out PT1BB);

int PT2EB;
int.TryParse(t_PT2EB.Text, NumberStyles.Any,
CultureInfo.InvariantCulture.NumberFormat, out PT2EB);

var testforX = (from GridViewRowInfo rows in PrimaryGridView.Rows
where rows.Index != -1 && rows.Cells[0].Value.ToString().Contains("PT1")
select rows.Cells[2].Value.ToString()).ToList();

testforX.Sort((x, y) => Double.Parse(x).CompareTo(Double.Parse(y)));
var testforXinvalues = testforX.First();
var numValue = Convert.ToInt32(testforXinvalues);

var testforLastx = PrimaryGridView.Rows[PrimaryGridView.RowCount - 1].Cells[3].Value.ToString();
var numValue2 = Convert.ToInt32(testforLastx);

var finalcalcul = (PT1BB - PT2EB);
var resetborders =  (numValue - numValue2);
var finalcalculll = Math.Atan(finalcalcul / resetborders);
MessageBox.Show(finalcalculll.ToString());```

• Edited by Wednesday, December 12, 2012 8:51 PM edited
Wednesday, December 12, 2012 8:50 PM

• Perhaps you need something like this?

```using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ElevationLookupTable.MakeTable();
double valueofPY = -7.0;
ShowResult(valueofPY);
ShowResult(7.0);
ShowResult(-6.5);
}

private static void ShowResult(double valueofPY)
{
var dtuP = ElevationLookupTable.FindElevation(valueofPY);
if (dtuP != null)
{
var dtuPValue = dtuP.Dtu;
Console.WriteLine("X = {0}, Dtu = {1}", valueofPY, dtuPValue);
}
else
{
Console.WriteLine("No such value: {0}", valueofPY);
}
}
}
public static class ElevationLookupTable
{
static Dictionary<double, Elevation> theTable = new Dictionary<double, Elevation>();

public static Dictionary<double, Elevation> ElevationLookup
{
get { return theTable; }
}

public static void MakeTable()
{
//DUMMY: This needs to be called by app to populate table.
theTable.Add(-7, new Elevation(-7, -260, -270, -300, -180, -350));
theTable.Add(-6.5, new Elevation(-6.5, -265, -275, -300, -185, -350));
}

public static Elevation FindElevation(double x)
{
if (theTable.ContainsKey(x)) return theTable[x];
return null;
}
}

public class Elevation
{
public double InclinaisonPente { get; set; }
public double Anpr { get; set; }
public double Dtu { get; set; }
public double Mcu { get; set; }
public double Overview { get; set; }
public double DsrcBeacon { get; set; }

public Elevation(
double inclinaisonPente,
double anpr,
double dtu,
double mcu,
double overview,
double beacon)
{
this.InclinaisonPente = inclinaisonPente;
this.Anpr = anpr;
this.Dtu = dtu;
this.Mcu = mcu;
this.Overview = overview;
this.DsrcBeacon = beacon;
}
}
}
```

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

• Marked as answer by Thursday, December 13, 2012 2:12 PM
Thursday, December 13, 2012 1:26 PM

### All replies

• Integer division gives the quotient so 100/300 gives 0. And Atan(0) = 0.

Try Atan((double)100/300). Mind it's not clear what type the variables are. Why not declare them as double to make sure.

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

Wednesday, December 12, 2012 9:31 PM
• Integer division gives the quotient so 100/300 gives 0. And Atan(0) = 0.

Try Atan((double)100/300). Mind it's not clear what type the variables are. Why not declare them as double to make sure.

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

Changed everything into double like this:

```	    double finalcalcul = (PT1BB - PT2EB);
double resetborders = (numValue - numValue2);
double tanvalue = finalcalcul/resetborders;
double finalcalculll = Math.Atan(tanvalue);
double normalizedAngle = (finalcalculll + Math.PI / 2) % Math.PI - Math.PI / 2;```
For the value of 100/3000, i should get the value 1.9 but i'm getting 0.3. What is the mistake here???

Wednesday, December 12, 2012 9:35 PM
• By this way, it works perfectly, but would like to know, how can i convert into percentage from angle??

```double finalcalcul = (PT1BB - PT2EB);
double resetborders = (numValue - numValue2);
double tanvalue = finalcalcul/resetborders;

double RadianToDegree(double angle) { return angle * (180.0 / Math.PI); }```

Wednesday, December 12, 2012 10:01 PM
• Divide by 360  if in degrees? Or by 2*PI if in radians?

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

Wednesday, December 12, 2012 10:09 PM
• Divide by 360  if in degrees? Or by 2*PI if in radians?

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

Hi, tried the way you have suggested to me but seems getting a wrong value. I have tried another way but same problem persists. 1.9 in degress to percentage should be 3.33 but i'm getting a wrong value. What should i do??

```	double finalcalcul = (PT1BB - PT2EB);
double resetborders = (numValue - numValue2);
double tanvalue = finalcalcul/resetborders;
//double finalinpercentage = finalcalculll / 360;
double finalinpercentage = Math.Tan(finalcalculll) * 100;

Thursday, December 13, 2012 8:58 AM
• I've no idea what you are trying to do. An angle of 1.9 degrees as a percentage of the angles in a circle is 1.9/360 and that's about 0.5%. So where does 3.33 come from? If 1.9 is 3.33% then the total must be about 57.06 (if that is in degrees it's almost 1 radian, coincidence or meaningful?)

The calculations you show look a bit odd, but I don't know what you are trying to do.

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

Thursday, December 13, 2012 11:23 AM
• Finally finished with the calcualtion,

`double finalinpercentage = 100 * Math.Tan(finalcalculll / 57.296);`

Sorry for the mis-understanding. I have a question, oiut of the topic:

This is the dictiinary i'm using, if the values is doesn't exist how can i cpature the exception?? Thank you.

```	public static class ElevationLookupTable
{
public static Dictionary<double, Elevation> ElevationLookup
{
get { return getElevationLookup(); }
}
private static Dictionary<double, Elevation> getElevationLookup()
{    //Anpr,dtuc,mcu,ov, beacon
Dictionary<double, Elevation> elevationDictionary = new Dictionary<double, Elevation>();
elevationDictionary.Add(-7,		new Elevation(-7,	-260, -270, -300, -180, -350));

//Distance as Key Value and look for inclination Pente
return elevationDictionary;
}
}```

This is how i'm trying to access the dictionary from my interface:

```var dtuP = ElevationLookupTable.ElevationLookup[valueofPY];
var dtuPValue = dtuP.Dtu;```

Thursday, December 13, 2012 12:02 PM
• That does not make sense. The ElevationLookup property is not an array so why the [valuePy]? Also it only ever returns the same single entry dictionary (with key -7). And there is not property Dtu in the class.

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

Thursday, December 13, 2012 12:29 PM
• That does not make sense. The ElevationLookup property is not an array so why the [valuePy]? Also it only ever returns the same single entry dictionary (with key -7). And there is not property Dtu in the class.

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

This is the entire class:

```	public static class ElevationLookupTable
{
public static Dictionary<double, Elevation> ElevationLookup
{
get { return getElevationLookup(); }
}
private static Dictionary<double, Elevation> getElevationLookup()
{    //Anpr,dtuc,mcu,ov, beacon
Dictionary<double, Elevation> elevationDictionary = new Dictionary<double, Elevation>();
elevationDictionary.Add(-7,		new Elevation(-7,	-260, -270, -300, -180, -350));
elevationDictionary.Add(-6.5,   new Elevation(-6.5, -265, -275, -300, -185, -350));

//Distance as Key Value and look for inclination Pente
return elevationDictionary;
}
}

```

• Edited by Thursday, December 13, 2012 2:12 PM edited
Thursday, December 13, 2012 12:46 PM
• Perhaps you need something like this?

```using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ElevationLookupTable.MakeTable();
double valueofPY = -7.0;
ShowResult(valueofPY);
ShowResult(7.0);
ShowResult(-6.5);
}

private static void ShowResult(double valueofPY)
{
var dtuP = ElevationLookupTable.FindElevation(valueofPY);
if (dtuP != null)
{
var dtuPValue = dtuP.Dtu;
Console.WriteLine("X = {0}, Dtu = {1}", valueofPY, dtuPValue);
}
else
{
Console.WriteLine("No such value: {0}", valueofPY);
}
}
}
public static class ElevationLookupTable
{
static Dictionary<double, Elevation> theTable = new Dictionary<double, Elevation>();

public static Dictionary<double, Elevation> ElevationLookup
{
get { return theTable; }
}

public static void MakeTable()
{
//DUMMY: This needs to be called by app to populate table.
theTable.Add(-7, new Elevation(-7, -260, -270, -300, -180, -350));
theTable.Add(-6.5, new Elevation(-6.5, -265, -275, -300, -185, -350));
}

public static Elevation FindElevation(double x)
{
if (theTable.ContainsKey(x)) return theTable[x];
return null;
}
}

public class Elevation
{
public double InclinaisonPente { get; set; }
public double Anpr { get; set; }
public double Dtu { get; set; }
public double Mcu { get; set; }
public double Overview { get; set; }
public double DsrcBeacon { get; set; }

public Elevation(
double inclinaisonPente,
double anpr,
double dtu,
double mcu,
double overview,
double beacon)
{
this.InclinaisonPente = inclinaisonPente;
this.Anpr = anpr;
this.Dtu = dtu;
this.Mcu = mcu;
this.Overview = overview;
this.DsrcBeacon = beacon;
}
}
}
```

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

• Marked as answer by Thursday, December 13, 2012 2:12 PM
Thursday, December 13, 2012 1:26 PM
• Thank you. It works.
Thursday, December 13, 2012 2:12 PM