Solved

C#: Integer Types

Posted on 2014-03-03
3
294 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
Comment Utility
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
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now