Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C#: Integer Types

Posted on 2014-03-03
3
Medium Priority
?
384 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 1000 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

783 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