Question about type-conversion in property

S

Simon Cheng

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.WriteLine(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.WriteLine(App.F); }
}
-------------------------------------------

What am I missing?

Thanks,
Simon
 
F

Frank Oquendo

Simon said:
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)
 
J

Jon Skeet [C# MVP]

Frank Oquendo said:
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.WriteLine (i);
Console.WriteLine ("{0:f}", f);
}
}

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

Frank Oquendo

Jon said:
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)
 
J

Jon Skeet [C# MVP]

Frank Oquendo said:
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.
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top