1

I want to calculate wind_chill by getting temp and wind_speed from user. All variables are declared as double.

wind_chill = 35.74 + 0.6215 * temp + (0.4275 * temp - 35.75) * System.Math.Pow(wind_speed, 0.16);

I am getting this O/P:-

Enter temperature and wind Speed: 20 7 wind_chill is:11.03490062551

But I want to print all the decimal number till last without round up.

Expected O/P:-

wind_chill = 11.034900625509998

By declaring variables as decimal and converting all the values in decimal: I am getting this output:

wind_chill = 11.034900625510096

Still not matching with the expected one. I searched But I didn't get my answer.How to get expected output?

Surabhi Pandey
  • 4,058
  • 4
  • 18
  • 25

2 Answers2

4

Calculate the value with doubles. It is just that .NET will format the value to 15 characters when printing. Use R format to get all digits.

var wind_chill = WindChillDbl(20.0, 7.0);
Console.WriteLine(String.Format("{0:R}", wind_chill));

public static double WindChillDbl(double temp, double wind_speed)
{
    return 35.74 + 0.6215 * temp + (0.4275 * temp - 35.75) * System.Math.Pow(wind_speed, 0.16);
}

"By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision." MSDN

Janne Matikainen
  • 5,061
  • 15
  • 21
  • @ Janne: Now O/P is coming: wind_chill = 11.034900625509991 Expected o/p:wind_chill = 11.034900625509998 why last 8 in expected o/p is converted to 1 in my o/p. – Surabhi Pandey Jan 03 '17 at 06:54
  • @ Janne: I need to match all the decimal points with the expected one – Surabhi Pandey Jan 03 '17 at 06:57
  • 1
    Where do you get your expected result value? – Janne Matikainen Jan 03 '17 at 07:04
  • Some times they need 15 decimal points sometimes they need 14. Their system is using different test cases and showing me wrong output because now with "R" format my all the outputs having 15 decimal points. Can we switch between 14 and 15 Or their system is only wrong – Surabhi Pandey Jan 03 '17 at 07:17
  • 1
    I dont know what system you are talking about. Seems they are rounding things up in if the get the last digit as 8 instead 1. As @jrbeverly stated, it really is about the power calculation, if you do that in scientific calculator you will get far more precision than the 17 digits (1,3652610064150737737613938051401 in the windows default calculator) – Janne Matikainen Jan 03 '17 at 07:28
3

You can inspect your calculation to see that the values returned for most of the expression are accurate. The problem is with the accuracy of System.Math.Pow(wind_speed, 0.16);. If you look at wolframalpha for that input, there are signifcantly more digits provided than the 1.36526100641507 returned by Math.Pow.

The reason for this is because Math.pow uses float point types which are inaccurate by design. You may also be able to use BigInteger and figure out a way to make your equation work with that.

You can resolve this in a couple of ways:

  1. Use BigRational
  2. Rework the equation to somehow use BigInteger
  3. See this question: What is the equivalent of the Java BigDecimal class in C#?, specifically this answer: https://stackoverflow.com/a/13813535/2127492

If you do go with the BigDecimal class provided in that answer, you will be able to make use of the method BigDecimal Pow(double basis, double exponent) to improve the accuracy your calculation.

You can see your calculation with the above class here.

Community
  • 1
  • 1
jrbeverly
  • 1,611
  • 14
  • 20