1

I am bit confused as to why calling Math.Round method would raise, "Ambiguous Call" compiler error.

Here is my offending code:

Math.Round((2000-((Splots[x].RawMin/4095)*2000))+200);

RawMin is Int32 data type.

I thought, Round method should return with Int32 value type.

Any hints or clues will be greatly appreciated. Thanks,

Ken White
  • 123,280
  • 14
  • 225
  • 444
ThN
  • 3,235
  • 3
  • 57
  • 115

1 Answers1

2

Look at the overload list for Math.Round. There are two methods taking a single parameter:

double Round(double d)
decimal Round(decimal d)

Both are applicable when called with an int argument - but it's pointless to call the method when you've only got an int to start with, as it's already rounded.

I suspect you actually want to change how you're doing arithmetic, e.g. by performing the division operation in double arithmetic, which will then propagate to the other operations:

// Note the 4095.0 to make it a double
Math.Round((2000-((Splots[x].RawMin/4095.0)*2000))+200);

Without that, all the operations use integer arithmetic, which almost certainly wasn't what you wanted.

You'll still need to cast the result to int though. The range of double exceeds that of both int and long, which is why the return type of Math.Round(double) is double. In this case you "know" that the result will be in an appropriate range though, given the calculation.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Jon Skeet, I guess I assumed that since I am doing division, the result will alway come out as decimal. Thank you for your answer. – ThN Jul 17 '12 at 13:54
  • @digitalanalog: Nope - if you're using `int` operands, the division is performed in integers; if you're using `decimal` operands, it's performed in `decimal`; if you're using `double` operands, it's performed in `double`. If you have `int` and `double`, the `int` is promoted to `double` first, which is the situation in my suggested code. – Jon Skeet Jul 17 '12 at 13:56