Solved

C#: Integer Types

Posted on 2014-03-03
3
320 Views
Last Modified: 2014-03-03
I'm reading the Wrox book: C# 2012 and .NET 4.5. It says:
If there is any ambiguity about whether an intege ris int, uint, long, or ulong, it will default to an int. To specify which of the other integer types the value should take, you can append a character such as "L" to the end of the number. For example:
long l = 10L;
I'm confused.

What if I did something like this:

long l = 20;

Although 20 looks like small int or maybe a regular int, wouldn't there be some kind of implicit type conversion so that the variable "l" would store a long? Why is there a need to append the "L" character?
0
Comment
Question by:pzozulka
3 Comments
 
LVL 8

Author Comment

by:pzozulka
ID: 39901160
Similarly with decimals vs. floats.

If you hard-code a non-int number (such as 12.3), the compiler will normally assume that you want the number interpreted as a double. If you want to specify the number as a float, you should append an "F".

float myFloat = 12.3;

Again same question, fine 12.3 is interpreted as a double, but when it is assigned to myFloat, should there be some kind of implicit type conversion happening to convert that decimal number into a float? If not, what will be the result of the above line of code?
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 250 total points
ID: 39901201
Your declaration of a float contains two parts:

    It declares that the variable timeRemaining is of type float.
    It assigns the value 0.58 to this variable.

The problem occurs in part 2.

The right-hand side is evaluated on its own. According to the C# specification, a number containing a decimal point that doesn't have a suffix is interpreted as a double.

So we now have a double value that we want to assign to a variable of type float. In order to do this, there must be an implicit conversion from double to float. There is no such conversion, because you may (and in this case do) lose information in the conversion.

The reason is that the value used by the compiler isn't really 0.58, but the floating-point value closest to 0.58, which is 0.57999999999999978655962351581366... for double and exactly 0.579999946057796478271484375 for float.

Strictly speaking, the f is not required. You can avoid having to use the f suffix by casting the value to a float:

float timeRemaining = (float)0.58;

similar explanation is there for long:

http://msdn.microsoft.com/en-us/library/ctetwysk.aspx
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39901202
For your first question, there is no need to append the literal identifier because an int will fit inside the memory space allocated for a long.

For your second question, the problem is that a double won't fit into the memory allocated for a float. Since the compiler defaults to typing a floating-point number as double, you have to tell it that you really meant to store a float, hence the literal.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question