Casting double by long gives wrong value.

V

Vaibhav

I have just made this demo code.

namespace testdatatype
{
class Program
{
static void Main(string[] args)
{
double Value = long.MaxValue;
Console.WriteLine("Max value of long : " + long.MaxValue);
Console.WriteLine("Value in long of double is : " + (long)Value);
Console.Read();
}
}
}


I am expecting that the value set to double i.e max of long should be
printed on console but following output comes.

Max value of long : 9223372036854775807
Value in long of double is : -9223372036854775808

& if i typecast it by ulong in Console.WriteLine() i get following output.

Max value of long : 9223372036854775807
Value in long of double is : -9223372036854775808

Actualluy i want the exact value of long.MaxValue ?

Is there any way i can do this.

Thanks in advance
 
G

Geoffrey Summerhayes

I have just made this demo code.

namespace testdatatype
{
    class Program
    {
        static void Main(string[] args)
        {
            double Value = long.MaxValue;
            Console.WriteLine("Max value of long : " + long.MaxValue);
            Console.WriteLine("Value in long of double is : "+ (long)Value);
            Console.Read();
        }
    }

}

I am expecting that the value set to double i.e max of long should be
printed on console but following output comes.

Max value of long : 9223372036854775807
Value in long of double is : -9223372036854775808

& if i typecast it by ulong in Console.WriteLine() i get following output..

Max value of long : 9223372036854775807
Value in long of double is : -9223372036854775808

Actualluy i want the exact value of long.MaxValue ?

If you want the exact value, don't use floating point.

http://docs.sun.com/source/806-3568/ncg_goldberg.html

If you really need casts when you are close to the limits of the
integers use the checked keyword.
 
J

Joe Greer

I have just made this demo code.

namespace testdatatype
{
class Program
{
static void Main(string[] args)
{
double Value = long.MaxValue;
Console.WriteLine("Max value of long : " + long.MaxValue);
Console.WriteLine("Value in long of double is : " +
(long)Value); Console.Read();
}
}
}


I am expecting that the value set to double i.e max of long should be
printed on console but following output comes.

Max value of long : 9223372036854775807
Value in long of double is : -9223372036854775808

& if i typecast it by ulong in Console.WriteLine() i get following
output.

Max value of long : 9223372036854775807
Value in long of double is : -9223372036854775808

Actualluy i want the exact value of long.MaxValue ?

Is there any way i can do this.

Thanks in advance

Well, let's see you have a 19 digit number according to the documentation I
have available doubles have the range:

Type Approximate range Precision .NET Framework type

double ±5.0 × 10-324 to ±1.7 × 10308 15-16 digits System..::.Double


You see the problem? You have 19 digits and are trying to store it in a
type which only has 16 digits precision. Something is going to have to
give and it will be the value stored in the double. That will be rounded.
If you had printed the value of the double without the cast to long, you
would have seen.

9.22337203685478E+18


Notice that it is now 15 digits of accuracy and the value is actually
larger than long.MaxValue. Because of that, you get an overflow and the
negative value observed above. Sadly, because you are reducing the
precision of the value you are storing, you are doomed to be disappointed
in your desire to get back exactly what you put into the double for values
with more precision than the double can handle.

joe
 
P

Pavel Minaev

Note that in C#, any code that needs to use long.MaxValue as part of a  
calculation is already suspect.  Even using integer arithmetic with "long"  
as the data type, all you can do is divide that number by something else. 
Otherwise, you're not going to get useful results.  If you have useful  
data that gets that is so close (or identical!) to the maximum value  
allowed, you're probably using the wrong data type.  (And of course, in 
C#, if you need more precision than 64 bits, you're looking at custom data  
types).

Or Decimal, if binary representation is not specifically required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top