473,669 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about type-conversion in property

Hi,

Using VS.NET 2003, the following code fails compilation (as expected):

-------------------------------------------
using System;
class App
{
static public int I { get { return 1.0f; } } // Cannot implicitly
convert type 'float' to 'int'
static void Main() { Console.WriteLi ne(App.I); }
}
-------------------------------------------

But the following code compiles without any error:

-------------------------------------------
using System;
class App
{
static public float F { get { return 1; } } // <----- Shouldn't this be
caught as an error instead?
static void Main() { Console.WriteLi ne(App.F); }
}
-------------------------------------------

What am I missing?

Thanks,
Simon
Nov 15 '05 #1
4 1191
Simon Cheng wrote:
What am I missing?


There's no threat of data loss when casting an int to a float. The reverse
is not true. Surely you're not actually using the posted example in
production code.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
Frank Oquendo <fr****@acadxpi n.com> wrote:
What am I missing?


There's no threat of data loss when casting an int to a float.


Actually, that's not strictly true. Not every int is exactly
representable as a float. (They can't be, as both types are 32 bit
types and there are floats which aren't representable as ints.)

For instance:

using System;

public class Test
{
static void Main()
{
int i = int.MaxValue/2;
float f = i;

Console.WriteLi ne (i);
Console.WriteLi ne ("{0:f}", f);
}
}

However, there is still an implicit conversion from int to float,
regardless of this potential loss of data.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Jon Skeet [C# MVP] wrote:
Actually, that's not strictly true. Not every int is exactly
representable as a float. (They can't be, as both types are 32 bit
types and there are floats which aren't representable as ints.)
You lost me with that last line. Of course there are floats which are not
representable as ints. What were you trying to say?
int i = int.MaxValue/2;
float f = i;


The data loss here ocurred in the first line, not the second.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #4
Frank Oquendo <fr****@acadxpi n.com> wrote:
Jon Skeet [C# MVP] wrote:
Actually, that's not strictly true. Not every int is exactly
representable as a float. (They can't be, as both types are 32 bit
types and there are floats which aren't representable as ints.)


You lost me with that last line. Of course there are floats which are not
representable as ints. What were you trying to say?


Both int and float are 32 bit values, and all possible bit patterns
represent valid ints. There are bit patterns for float which are not
equivalent to ints, therefore there *must* be ints which cannot be
exactly represented as floats. There just aren't enough possible bit
patterns in float.
int i = int.MaxValue/2;
float f = i;


The data loss here ocurred in the first line, not the second.


Nope - I wasn't trying to get exact (mathematical) value of
int.MaxValue/2. I was merely taking the integer value of int.MaxValue/2
as an example of an int which isn't exactly representable as a float.
The value of f is not the same as the value of i, so data has been
lost. (You cannot get the original value of i from the value of f -
indeed, several nearby integers would all have given the same value for
f.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4348
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match to the event_id. For Q? and Comments I am viewing/updating in a different table than I am question and how_to_answer. The stored proc is populated by a view that I'm using to get all these values from two tables. My quandry is, I am getting the...
7
2654
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask it differently. FOUR QUESTIONS: The background: I got three (3) files
4
547
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question"; (string)xyz; now we only have a type object of System.String type
9
340
by: LNM | last post by:
I am trying to create a survey type form where multiple choice answers are selected (radios) and I am using an onclick event to update a text box with 'correct', etc. I am using the onFocus(this.blur) so that a user is unable to change the text that is written to that text field (they are must be correct before submitting the form). I am wanting to use a hidden field that will update the same way as the text fields do (simply because i...
1
2615
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am experimenting a bit with what I need to do this and have the following code snippet. But first if I pass the assembly name and type to Activator.CreateInstance() it always fails. However if I walk my assembly and get a type value, the call to...
18
1650
by: Kamen Yotov | last post by:
hi all, i first posted this on http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx (ask a c# language designer) a couple of days ago, but no response so far... therefore, i am pasting it here as well... enjoy! (you can skip to the source at the end of the message if you like...) Consider:
10
3711
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server Error in '/QuickStartv20' Application. -------------------------------------------------------------------------------- Configuration Error Description: An error occurred during the processing of a configuration file
0
2071
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently structured relational database system. The CMS stores articles. The CMS also stores related items --...
29
1993
by: Amar Kumar Dubedy | last post by:
implement a c++ class such that it allows us to add data members at runtime.
8
1663
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n), and the information to assign (a const pointer to a class object Vector<TYPE>. According to the arrow below, I put a const keyword in the function. To my knowledge, this means that all private variables in this object
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.