c# - outputting a value of infinity [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have part of the calculation that is modified to explain what issue I'm having. I'm converting a working Java code into a c# application.
A double variable is declared doing the following calculation and the following input values being passed along for example:
double input1 = 45
double input2 = 42
double input3 = 1
double a = (input1 - input2 * Math.Pow(input3, 2.0)) / (1 - Math.Pow(input3, 2.0));
This outputs as an Infinite in c# but Java shows the value. With infinite being outputted, the other calculations below are affected.
double b = input1 + input2 - 2 * a (becomes -Infinite)
double c = b + a (gives you NaN)
a,b, and c are outputted into individual text boxes and each either show as Infinite or NaN.
Textbox1.Text = a.ToString("F") (Infinite)
Textbox2.Text = b.ToString("F") (-Infinite)
Textbox3.Text = c.ToString("F") (NaN)
I know that this is due to IEEE standards, but is there actually a way that variable a shows the value instead of showing infinite so that it doesn't affect the calculations or output the results into a text box in 2 decimal places?

Your code is returning a value 0 for the divide part which results into infinity value. which is true here there is nothing wrong in the code. You need to check the calculation.
double input1 = 45;
double input2 = 42;
double input3 = 1;
double a = (input1 - input2 * Math.Pow(input3, 2.0))
/ (1 - Math.Pow(input3, 2.0));
Console.WriteLine((input1 - input2 * Math.Pow(input3, 2.0)));
Console.WriteLine((1 - Math.Pow(input3, 2.0)));
Console.WriteLine(a);
check the output on console.

I pasted into a Java test program the code you posted for calculating a, and made minimal changes to turn it into compilable Java. I added ; to the first three declarations, and changed Math.Pow to Math.pow:
public class Test {
public static void main(String[] args) {
double input1 = 45;
double input2 = 42;
double input3 = 1;
double a = (input1 - input2 * Math.pow(input3, 2.0))
/ (1 - Math.pow(input3, 2.0));
System.out.println(a);
}
}
The program prints, as expected, "Infinity".
If your Java program is getting something different, there is some error in how you are converting it to C#.

Related

Can't get the conditions values from method [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 6 years ago.
I want to calculate the average of two floating point numbers, but whatever the input, I am getting an integer returned.
What should I do to make this work?
public class Program
{
public static float Average(int a, int b)
{
return (a + b) / 2;
}
public static void Main(string[] args)
{
Console.WriteLine(Average(2, 1));
}
}
There're two problems with your code
Evident one - Integer division - e.g. 1 / 2 == 0 not 0.5 since result must be integer
Hidden one - Integer overflow - e.g. a + b can overflow int.MaxValue and you'll get negative result
The most accurate implementation is
public static float Average(int a, int b)
{
return 0.5f * a + 0.5f * b;
}
Tests:
Average(1, 2); // 1.5
Average(int.MaxValue, int.MaxValue); // some large positive value
The trick is to write the expression as 0.5 * a + 0.5 * b, which also obviates the potential for int overflow (acknowledge Dmitry Bychenko).
Currently your expression is evaluated in integer arithmetic, which means that any fractional part is discarded.
In setting one of the values in each term to a floating point literal, the entire expression is evaluated in floating point.
Finally, if you want the type of the expression to be a float, then use
0.5f * a + 0.5f * b
The f suffix is used to denote a float literal.
return (a + b) / 2F; tells the compiler to treat the number as a float, otherwise it will be treated as an int.
Use this:
public static float Average(int a, int b)
{
return (float)(a + b) / 2;
}
You can use:
(float)(a + b) / 2.0
This will return float
Sorry, if anyone has answered the same way (I did not read all answers)

Math Operation in Windows Form Application [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 1 year ago.
I am trying to use a function in windows form application that convert a given point to another coordinate system. However, I encountered a strange problem. The input are correct but output is always 0. First, I thought it caused because of the local variables and then instead of variables I used only integers but it did not solve. I have no idea about it. Here the code and output basically:
string[] newPoint1 = convertPoints(X1, Y1);
string[] convertPoints(int oldX, int oldY)
{
//int newX = ((oldX - oldLeft) / (oldRight - oldLeft)) * (newRight - newLeft);
MessageBox.Show(oldX.ToString()); // output is 296
int newX = (oldX / 500) * 4096; // ????????????????????? (296/500) * 4096 = 0 ?????????????
MessageBox.Show(newX.ToString()); // here output is 0
int newY = newTop + ((oldY - oldTop) / (oldBottom - oldTop)) * (newBottom - newTop);
//MessageBox.Show(newY.ToString());
string[] newPoints = {newX.ToString(), newY.ToString()};
//MessageBox.Show(newPoints[0], newPoints[1]);
return newPoints;
}
This is working as it should. Because oldX is an Integer, when you divide it, it rounds (drops anything after the decimal). I would convert it to float and back into an integer, like so
int newX = (int)(((float)oldX / 500) * 4096);
This will preserve the whole number until you're done at the end. You'll also need to do the same for the Y values
An integer division cuts off the decimal places. So in your case, 296/500 you would expect 0.592. As integer has no decimal places, it cuts off them off resulting in 0.
Change the oldX to double and divide by 500.0
You are getting 0 because oldX/500 is a fraction usually and since you are using the int datatypes there can only be whole numbers. What I would recommend doing is changing the data type then rounding yourself.
//Old code
int newX = (1 / 500);
Console.WriteLine(newX);
// writes 0 to console
//New code
double newXD = (1 / 500.0) * 4096;
Console.WriteLine(newXD);
//Writes 8.192
The 1 and the 500 are considered ints try
Console.WriteLine(1/500);
It writes 0 to the console.
Console.WriteLine(1/500.0);
Console.WriteLine((float)1/500);
Console.WriteLine((double)1/500);
All these write 8.192 to the console.
Then after you have the double or other more accurate data type consider rounding if you really want an int.

Dividing one smaller number with the bigger one doesn't work without special decleration c# [duplicate]

This question already has answers here:
Why do these division equations result in zero?
(10 answers)
Closed 2 years ago.
This:
float a = 2 / 4;
return a;
Will output as 0, while this:
float a = 2;
float b = 4;
float c = a / b;
return c;
Will give a correct output 0.5.
Why am I getting this result?
C# interprets 2 and 4 as plain integers, 2/4 is 0 using integer math (floor of 0.5).
Try using 2f and 4f instead to have floating point numbers. Refer to this documentation to see all possible number literals https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types#real-literals.
Code below works as expected:
public static void Main()
{
float a = 2f / 4f;
Console.WriteLine(a);
}
Because 2 and 4 are the type of integer.
Type type1 = 2.GetType();
Type type2 = 4.GetType();

The result of calculation deviates after some digits [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Result of calculation shown below should be equal to 0,007306897 (I referred from 2 books) . But when i check result from Watch 1, i see that result is equal to 0,007306882. I split the process into some parts. And the problem is occurring when c is calculating.
///Declarations
double sigma = 1.00000000;
double a,b,e,c;
a = (1 / Math.Sqrt(2 * Math.PI)); //calculated properly
c = -(i * i + j * j) / 2.00000000 * (sigma * sigma); //i and j are equal to -2
e = Math.E; //calculated properly
b = Math.Pow(e, c);
result=a * b;
Unfortunately the double type is not highly accurate. The link below shows that some numbers like 1.05 can not be stored accurately by the double type.
http://www.binaryconvert.com/convert_double.html
For normal usage it is usually accurate enough, if you need accuracy to the level you describe you probably need to use something like binary coded decimal. That will mean the normal math library won't work. You could try asking on some physics sites to see what they use when modelling complex systems to maintain accuracy.

Getting Different Values for the same formula in C++ Than in C#

I have a formula from a C++ application that I have implemented in C# but I am producing vastly different results.
The Sample Latitude is:
Here is the C++ Implementation:
double lat = 0.959931088596881 //In Radians
double lon = -3.14159265358979 //In Radians
double alt = altitude;
double a = 6378137;
double e = 8.1819190842622e-2;
double N = a / sqrt(1.0 - (e * e) * (sin(lat) * sin(lat)));
x = (N+alt) * cos(lat) * cos(lon);
Final result for X: -3.66659e+006
Here is the C# Implementation:
double lat = this.DegreesToRadians(latitude);
double lon = this.DegreesToRadians(longitude);
double alt = altitude;
double a = 6378137;
double e = 8.1819190842622e-2;
double N = a / Math.Sqrt(1.0 - (e * e) * (Math.Sin(lat) * Math.Sin(lat)));
x = (N + alt) * Math.Cos(lat) * Math.Cos(lon);
Final result for X: -3666593.52237417
I am sure there must be something simple I am missing. I have tried pulling out different pieces and parts but can't quite figure out where the difference is occurring.
I have a formula from a C++ application that I have implemented in C# but I am producing vastly different results.
Nope, you're getting very, very similar results - with different representations.
C++: -3.66659e+006
C#: -3666593.52237417
Those are the same number (to 6 significant digits) - the C++ representation is just using scientific representation.
Here's a piece of C# to demonstrate the same value shown in two ways:
using System;
class Test
{
static void Main(string[] args)
{
// Source code using scientific representation
double value = -3.66659e+006;
Console.WriteLine(value.ToString("G")); // General representation
Console.WriteLine(value.ToString("E")); // Scientific representation
}
}
Output:
-3666590
-3.666590E+006
Now we can't tell what the C++ result is beyond the first 6 significant digits, based just on your output, although you can probably use printf to show you more details. But the values are at least very similar.

Categories