473,388 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

12.34f vs (float) 12.34

Is there a difference between a constant like "12.34f" and "(float)
12.34"?

In principle, at least, the latter is a double constant being cast to
a float; while the two both generate actual constants, does the latter
ACTUALLY do a conversion at compile time? That is, are there constants
where

<constant>f != (float) <constant>

?

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 15 '05 #1
19 2175
No. The compiler will optimize the cast away assuming it can. It's probably
generating something like this:

private static void Blah()
..locals (float V_0, float V_1)
L_0000: ldc.r4 1.23
L_0001: stloc.0
L_0002: ldc.r4 4.56
L_0003: stloc.1

from this:

static void Blah()
{
float f1 = 1.23F;
float f2 = (float) 4.56;
}
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
Is there a difference between a constant like "12.34f" and "(float)
12.34"?

In principle, at least, the latter is a double constant being cast to
a float; while the two both generate actual constants, does the latter
ACTUALLY do a conversion at compile time? That is, are there constants
where

<constant>f != (float) <constant>

?

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 15 '05 #2
"Klaus H. Probst" wrote:
No. The compiler will optimize the cast away assuming it can. It's probably
generating something like this:

private static void Blah()
.locals (float V_0, float V_1)
L_0000: ldc.r4 1.23
L_0001: stloc.0
L_0002: ldc.r4 4.56
L_0003: stloc.1

from this:

static void Blah()
{
float f1 = 1.23F;
float f2 = (float) 4.56;
}


Indeed it is. The question, though, was HOW it's optimizing the cast
away, whether code like "1.23f" and "(float) 1.23" might ever generate
slightly different constants.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 15 '05 #3
The answers to those two questions are:

HOW: it's called constant folding, and is performed by the C# compiler, and
there's loads of information out there, and
WHETHER: in theory I think they could in your example (but that's just my
interpretation); section 11.1.3 of the CLI docs states:

"Storage locations for floating point numbers (statics, array elements, and
fields of classes) are of fixed size. The supported storage sizes are
float32 and float64. Everywhere else (on the evaluation stack, as arguments,
as return types, and as local variables) floating point numbers are
represented using an internal floating-point type. In each such instance,
the nominal type of the variable or expression is either R4 or R8, but its
value may be represented internally with additional range and/or precision.
The size of the internal floating-point representation is
implementation-dependent, may vary, and shall have precision at least as
great as that of the variable or expression being represented."

From my experience the internal representation can differ between debug and
release.

Hope that helps, but please don't take my answer as gospel.

Stu

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
"Klaus H. Probst" wrote:
No. The compiler will optimize the cast away assuming it can. It's probably generating something like this:

private static void Blah()
.locals (float V_0, float V_1)
L_0000: ldc.r4 1.23
L_0001: stloc.0
L_0002: ldc.r4 4.56
L_0003: stloc.1

from this:

static void Blah()
{
float f1 = 1.23F;
float f2 = (float) 4.56;
}


Indeed it is. The question, though, was HOW it's optimizing the cast
away, whether code like "1.23f" and "(float) 1.23" might ever generate
slightly different constants.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 15 '05 #4
Stu Smith wrote:
HOW: it's called constant folding,
Yes, thanks. Point of my question, though, is that I can imagine a few
ways that this might be done:

* Very straightforwardly, whereby "(float) 1.23" constructs a double
value 1.23, then casts it to a single.

* More convolutedly, whereby the constant folding operation sees the
(float) cast being applied to an implicitly typed double, and goes
back to the literal and reparses it as 1.23f.

* Complex parsing, where cast followed by a literal parses the literal
exactly as if it were 1.23f.

The point being that in some cases the first approach might yield
slightly different values than the first.
WHETHER: in theory I think they could in your example (but that's just my
interpretation); section 11.1.3 of the CLI docs states:


I'm not sure that ECMA-335 has much to say about the workings of the
compiler ....

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 15 '05 #5
"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
Stu Smith wrote:
HOW: it's called constant folding,
Yes, thanks. Point of my question, though, is that I can imagine a few
ways that this might be done:

* Very straightforwardly, whereby "(float) 1.23" constructs a double
value 1.23, then casts it to a single.

* More convolutedly, whereby the constant folding operation sees the
(float) cast being applied to an implicitly typed double, and goes
back to the literal and reparses it as 1.23f.

* Complex parsing, where cast followed by a literal parses the literal
exactly as if it were 1.23f.


As far as I'm aware (and we're at the limits of my knowledge here), constant
folding is done at compile time, so option one is out of the question. I
believe the compiler makes passes over the statement/expression trees it has
generated, looking for certain patterns, and replacing them with
equivalents. I guess one pattern could be something like "(builtin value
type) constant -> new constant".

The point being that in some cases the first approach might yield
slightly different values than the first.
WHETHER: in theory I think they could in your example (but that's just my interpretation); section 11.1.3 of the CLI docs states:
I'm not sure that ECMA-335 has much to say about the workings of the
compiler ....


Don't know about that, but I do know that if you have a variable of type
float it's not guaranteed to be no more than a float.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 15 '05 #6
Stu Smith wrote:
* Very straightforwardly, whereby "(float) 1.23" constructs a double
value 1.23, then casts it to a single.
As far as I'm aware (and we're at the limits of my knowledge here), constant
folding is done at compile time, so option one is out of the question.


We're pretty much at the limits of my knowledge, too. I've written a
simple compiler, so I have some knowledge of the issues involved, but
that's about it.

But I don't see why you say that option 1 can be ruled out? Seems to
me: Parser sees a stream of digits followed by a . and another stream
of digits which is NOT followed by one of {d, f, m}. Bingo: a double
constant for the parse tree. Optimizer later comes along and sees the
expression cast(float, double constant) and does a compile-time cast,
reducing it to a float constant.

* * *

I suppose I should just do the brute force thing: write a simple test
program to generate reams of float constants, and see if (say)
2.22222222222222f != (float) 2.22222222222222.

Course, I'll probably find more than I care to know about method size
limits ....

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 15 '05 #7

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
Stu Smith wrote:
* Very straightforwardly, whereby "(float) 1.23" constructs a double
value 1.23, then casts it to a single.
As far as I'm aware (and we're at the limits of my knowledge here),
constant folding is done at compile time, so option one is out of the question.


We're pretty much at the limits of my knowledge, too. I've written a
simple compiler, so I have some knowledge of the issues involved, but
that's about it.

But I don't see why you say that option 1 can be ruled out? Seems to
me: Parser sees a stream of digits followed by a . and another stream
of digits which is NOT followed by one of {d, f, m}. Bingo: a double
constant for the parse tree. Optimizer later comes along and sees the
expression cast(float, double constant) and does a compile-time cast,
reducing it to a float constant.


I thought you meant a run-time cast. Not sure whether you'd call
compile-time folding a cast but I think we're talking the same thing, even
if we use different words.

* * *

I suppose I should just do the brute force thing: write a simple test
program to generate reams of float constants, and see if (say)
2.22222222222222f != (float) 2.22222222222222.
And what I meant about having extra precision was something like this:

class Foo
{
public void Test()
{
float localFloat = _memberFloat;

// The following doesn't have to be true, ie assertion could fire.
Debug.Assert( _memberFloat == localFloat );
}

private float _memberFloat = 1.23;
}

Course, I'll probably find more than I care to know about method size
limits ....

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 15 '05 #8
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least
jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double
to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #9
The C# compiler will act the same way on ALL platforms.
The JIT will not.
The MSIL should be all the same regardless of the CPU or the OS you're
compiling under.

About the conversion - you can check out what does the C# compiler spits out
with the ILDASM utility.
Located in the .NET Framework SDK folder.

Cheers,
Branimir
--
Branimir Giurov
MCSD.NET, MCDBA
eAgility LLC

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 16 '05 #10
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least
jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double
to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #11
The C# compiler will act the same way on ALL platforms.
The JIT will not.
The MSIL should be all the same regardless of the CPU or the OS you're
compiling under.

About the conversion - you can check out what does the C# compiler spits out
with the ILDASM utility.
Located in the .NET Framework SDK folder.

Cheers,
Branimir
--
Branimir Giurov
MCSD.NET, MCDBA
eAgility LLC

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 16 '05 #12
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least
jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double
to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #13
The C# compiler will act the same way on ALL platforms.
The JIT will not.
The MSIL should be all the same regardless of the CPU or the OS you're
compiling under.

About the conversion - you can check out what does the C# compiler spits out
with the ILDASM utility.
Located in the .NET Framework SDK folder.

Cheers,
Branimir
--
Branimir Giurov
MCSD.NET, MCDBA
eAgility LLC

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 16 '05 #14
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least
jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double
to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #15
The C# compiler will act the same way on ALL platforms.
The JIT will not.
The MSIL should be all the same regardless of the CPU or the OS you're
compiling under.

About the conversion - you can check out what does the C# compiler spits out
with the ILDASM utility.
Located in the .NET Framework SDK folder.

Cheers,
Branimir
--
Branimir Giurov
MCSD.NET, MCDBA
eAgility LLC

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 16 '05 #16
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least
jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double
to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #17
The C# compiler will act the same way on ALL platforms.
The JIT will not.
The MSIL should be all the same regardless of the CPU or the OS you're
compiling under.

About the conversion - you can check out what does the C# compiler spits out
with the ILDASM utility.
Located in the .NET Framework SDK folder.

Cheers,
Branimir
--
Branimir Giurov
MCSD.NET, MCDBA
eAgility LLC

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 16 '05 #18
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least
jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double
to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #19
The C# compiler will act the same way on ALL platforms.
The JIT will not.
The MSIL should be all the same regardless of the CPU or the OS you're
compiling under.

About the conversion - you can check out what does the C# compiler spits out
with the ILDASM utility.
Located in the .NET Framework SDK folder.

Cheers,
Branimir
--
Branimir Giurov
MCSD.NET, MCDBA
eAgility LLC

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
cody wrote:
Is there a difference between a constant like "12.34f" and "(float)
12.34"?


You can be sure that both are evaluated to compile time contants (at least jit time).
But you cannot assume that 12.34f ==(float)12.34 because conversion double to float might result in loss of precision.


Right. This is pretty much where I ended up. Even if the canonical
(MS) C# compiler works this way on a Pentium, that is not to say that
all C# compilers on all processors will act this way.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

Nov 16 '05 #20

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

Similar topics

5
by: Pat | last post by:
Give two double-typed variable X and Y. If (X==Y) is true, then how about the following results: (float(X) > float(Y))? (float(X) < float(Y))? (float(X) >= float(Y))? ( X > float(Y) )? ( X...
14
by: Glen Able | last post by:
Should it be possible to create a custom class, 'Float', which would behave as a drop-in replacement for the builtin float type? As mentioned in another thread, I once tried this in rather a...
16
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why...
6
by: Dave win | last post by:
Hi all: I'm confused with the expression "(float *())". Book says that this is a cast. But, I have no idea of this expr. why could this expr ignore the variable??? Thanx!!!
9
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
11
by: Marc Pelletier | last post by:
Hello, I am having trouble implementing the following callback: CNCSError CECWCompressor::WriteReadLine(UINT32 nNextLine, void **ppInputArray) where ppInputArray is a 3 by x array. The...
20
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51...
8
by: vjnr83 | last post by:
Hi, I have a doubt: what is the difference between float **p and float *p? Thanks in advance, Vijay
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
3
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.