-5

I want to run this program, but it won't let me because I get an error saying I'm trying to convert 'decimal' to 'double'. I don't understand why. Nor do I know how I can fix this issue.

class Program
{
    static void Main(string[] args)
    {
        string inputString = Console.ReadLine();
        decimal[] hvValues = inputString.Split().Select(decimal.Parse).ToArray();

        decimal sinTest = hvValues[1];
        decimal sinV = Math.Sin(sinTest);
        decimal hypotenusan = hvValues[0] / sinV;
        Console.WriteLine(Math.Ceiling(hypotenusan));
    }
}
fredso
  • 17
  • 3
  • 1
    May I guess at the line causing the error? Is it, `decimal hypotenusan = hvValues[0] / sinV;` ? – Ňɏssa Pøngjǣrdenlarp Mar 28 '19 at 20:04
  • 2
    `Math.Sin` takes a `double` and returns a `double`. Yet you are using `decimal` throughout your code. –  Mar 28 '19 at 20:05
  • `Math.Sin` is only defined for `double` (see https://learn.microsoft.com/en-us/dotnet/api/system.math.sin?view=netframework-4.7.2) and therefore accepts and returns only a `double`. – ckuri Mar 28 '19 at 20:05
  • You can't cast double to decimal implicitly because the conversion can't be secured to be lossless. Check [this SO post](https://stackoverflow.com/questions/7817866/why-cant-double-be-implicitly-cast-to-decimal) – Vulpex Mar 28 '19 at 20:06
  • `Math.Sin` returns double, thats why. – CSharpie Mar 28 '19 at 20:07
  • @Vulpex OP is doing the reverse. – itsme86 Mar 28 '19 at 20:07
  • @itsme86 it doesn't matter, it isn't allowed either way as far I know. – Vulpex Mar 28 '19 at 20:08
  • Well for clarity, you should speak to what the OP is trying to do, not something that is seemingly irrelevant. – itsme86 Mar 28 '19 at 20:08
  • Your code does not say that you are trying... Only a single line of code is saying that, not your whole code. There are even little colored wiggly lines in Visual Studio that highlight the line of code that makes problems. From there, it is just a matter of looking up the documentation of what type of arguments the offending method/function accepts and what type of results it returns (if any). Just use your eyes and follow the bread crumbs ;-) –  Mar 28 '19 at 20:09
  • Read the third paragraph under "Conversion Considerations": https://learn.microsoft.com/en-us/dotnet/api/system.decimal?view=netframework-4.7.2#conversion-considerations –  Mar 28 '19 at 20:10
  • Just turn *everything* to double, then your code will work. But you should try to understand what the difference between `double` and `decimal` is, at some point. – CSharpie Mar 28 '19 at 20:10

1 Answers1

0

For simplicity, as you seem to be new to programming, just use double instead of decimal:

class Program
{
    static void Main(string[] args)
    {
        string inputString = Console.ReadLine();
        double[] hvValues = inputString.Split().Select(double.Parse).ToArray();

        double sinTest = hvValues[1];
        double sinV = Math.Sin(sinTest);
        double hypotenusan = hvValues[0] / sinV;
        Console.WriteLine(Math.Ceiling(hypotenusan));
    }
}

This will make it work, however at some point you should try to understand what the difference between double and decimal is. Also it will help that you learn to understand compiler errors and why your IDE puts red underline under certain parts of your code.

CSharpie
  • 9,195
  • 4
  • 44
  • 71