Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C# does not require explicit casting for convert Long To Double?

Tags:

c#

casting

At first, sorry for my bad english. I have fragment of code:

long x = 9223372036854775807L;
double f = x;
Console.WriteLine(x);           
Console.WriteLine(f);

Output is:

9223372036854775807
9,22337203685478E+18

I'm not getting any errors while compiling and execution this code. We have a loss of precision while converting Long to Double. Why C# does not require explicit casting in that case ?

Thanks all.

like image 512
tvolf Avatar asked Feb 13 '13 18:02

tvolf


People also ask

Why learn C?

Why Learn C? There are an awful lot of programming languages available right now -- everything from the extremely high level (such as Visual Basic) to the low level power of assembly, and a good variety of specialized options in between ( Perl, Ruby, and Python are good choices for many tasks).

Why do so many programmers use C?

Plus, with C, you get lots of strong opinions mixed with insights that you can understand. As a result of its age and its use as the language of system programming for Unix, C has become something of the lingua franca of programming. C is a great language for expressing common ideas in programming in a way that most people are comfortable with.

What does %C mean in C programming?

While it's an integer, the %c interprets its numeric value as a character value for display. For instance for the character a: If you used %d you'd get an integer, e.g., 97, the internal representation of the character a using %c to display the character ' a ' itself (if using ASCII)

Why do we use %C instead of %s in D?

Also, when using %c you want a character printed, not a number. In the D language, you would always use %s and let the compiler worry about the types. @MatejNanut No, integer types smaller than int are promoted to int when being passed into a variadic function. @Pubby: thank you, I didn't know that.


1 Answers

The language has some implicit conversion built into it.

The following table is from the documentation, which is why you are allowed to assign the value without an explicit cast or conversion:

From        To
===============================================================================
sbyte       short , int, long, float, double, or decimal
byte        short , ushort, int, uint, long, ulong, float, double, or decimal
short       int , long, float, double, or decimal
ushort      int , uint, long, ulong, float, double, or decimal
int         long , float, double, or decimal
uint        long , ulong, float, double, or decimal
long        float , double, or decimal
char        ushort , int, uint, long, ulong, float, double, or decimal
float       double
ulong       float , double, or decimal

And in the documentation it states (emphasis mine):

Precision but not magnitude might be lost in the conversions from int, uint, long, or ulong to float and from long or ulong to double.

like image 105
John Koerner Avatar answered Oct 11 '22 22:10

John Koerner