Solved

C#: Integer Types

Posted on 2014-03-03
3
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

732 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