3

Why does converting back gives a -ve expression ?

float f = long.MaxValue;

Console.WriteLine("long to float: {0}",f);
Console.WriteLine("long Max: {0}",long.MaxValue);
Console.WriteLine("float to long casting: {0}",(long)f);
//Console.WriteLine("converting float to long: {0}",Convert.ToInt64(f));
//Above statement gives OverflowExection as expected.

OUTPUT:

long to float: 9.223372E+18
long Max: 9223372036854775807
float to long casting: -9223372036854775808

Again, long.MinValue gives the correct result.

Marshal
  • 6,551
  • 13
  • 55
  • 91
  • Have a look here: http://stackoverflow.com/questions/4352213/long-in-float-why .. and here: http://stackoverflow.com/questions/22935564/error-in-float-to-long-conversion .. – txtechhelp Jun 27 '15 at 07:37
  • @txtechhelp: The questioner in first link didn't know how integral and floating points are represented in memory and the questioner in the second link wanted to have exact value from a floating point type. But context of my question is different. Its ok if float doesn't give exact value, but negative value ...? – Marshal Jun 27 '15 at 07:47
  • You are not Converting to long. you are Casting to long. and well that's kind of different! – M.kazem Akhgary Jun 27 '15 at 08:07
  • yup that was explicit casting, I tried converting too..same result. Added to the question. – Marshal Jun 27 '15 at 08:10
  • float f = long.MaxValue; Console.WriteLine(Convert.ToInt64(f)); System.OverflowException: Arithmetic operation resulted in an overflow. – Ganesh R. Jun 27 '15 at 08:22
  • @GaneshR. yes you are right !. I was using LinqPad 4 which gave the above result. I should remove the convert version. – Marshal Jun 27 '15 at 08:25
  • i give answer as comment! because im not sure. i dont have reference. i thinks its because float is floating-point and not accurate. it will become out of range of long (since it was already long.max) and casting just gives unexpected resault (i dont know why). but if you try `Convert.ToInt64` you will get Overflow exception. – M.kazem Akhgary Jun 27 '15 at 08:25
  • Yes that's the answer. I just checked the value of float result, its 1 more than long.MaxValue (you can see the output). Please write it as an answer and I will accept it. – Marshal Jun 27 '15 at 08:28

1 Answers1

0

As M.kazem suggested in the comment, it was resulting in an overflow and as per C# specification compiler was silently ignoring it

Console.WriteLine("float to long casting: {0}",(long)f);

But if we convert above under checked then an overflow expression is thrown.

checked 
{
   Console.WriteLine("float to long casting: {0}",(long)f);
}
Marshal
  • 6,551
  • 13
  • 55
  • 91
  • yes. if i find why casting gives negative value ill let you know. – M.kazem Akhgary Jun 27 '15 at 08:32
  • For C# projects, by default check for arithmetic overflow is disabled. – Ganesh R. Jun 27 '15 at 10:01
  • for the negative value i think the problem is top bit that specifies the sign of `long`. the same thing when you convert uint to int. if uint is more than `int.max` it gives negative because top bit specifies sign. http://stackoverflow.com/a/1131851/4767498 – M.kazem Akhgary Aug 12 '15 at 19:26