RHMCSUPV@MIAMIU.BITNET (Douglas M. MacFarlane) (08/28/90)
Excuse the novice question . . . I'm new to C and liking it, but this puzzles me. I'm using Microsoft C ver 5.1. When I assign a value to a float variable, I get a data- conversion warning (with warning level 2 set). Why? For example: float fValue ; /* define fValue as a floating point variable */ other lines fValue = 35 ; /* produces a Data Conversion warning at compile time */ Whasss Happening ???? Douglas M. MacFarlane rhmcsupv@miamiu.acs.muohio.edu
stever@Octopus.COM (Steve Resnick ) (08/29/90)
In article <90240.003415RHMCSUPV@MIAMIU.BITNET> RHMCSUPV@MIAMIU.BITNET (Douglas M. MacFarlane) writes: >Excuse the novice question . . . > >I'm new to C and liking it, but this puzzles me. I'm using Microsoft >C ver 5.1. When I assign a value to a float variable, I get a data- >conversion warning (with warning level 2 set). Why? > >For example: > >float fValue ; /* define fValue as a floating point variable */ > >other lines > >fValue = 35 ; /* produces a Data Conversion warning at compile time */ > > Forgive me if I am wrong, and this is all supposition on my part, but I think that Microsoft C is a little more "strongly typed" in that when it sees a constant that will fit in an int, it assumes int for that type. when you assign it to a float, the compiler warns that you are putting and int value into a float. This should be OK, since the compiler should be assigning the value as if it were (float) 35, or 35.0. In orderd to avoid warnings like that, for float values which do not have a fractional portion, I always assign the value <number>.0. Hope this helps... Steve -- ---------------------------------------------------------------------------- steve.resnick@f105.n143.z1@FIDONET.ORG #include<std_disclaimer.h> Flames, grammar errors, spelling errrors >/dev/nul ----------------------------------------------------------------------------
hp@vmars.tuwien.ac.at (Peter Holzer) (08/29/90)
RHMCSUPV@MIAMIU.BITNET (Douglas M. MacFarlane) writes: >For example: > >float fValue ; /* define fValue as a floating point variable */ >fValue = 35 ; /* produces a Data Conversion warning at compile time */ > > >Whasss Happening ???? > MSC finds the integer 35, and an a float variable fValue so it figures out that 35 should really read 35.0. But this is a double value (all floating point constants are double) and through the assignment to a float bits could be lost, so the compiler warns you. The same effect happens if you do something like: char flag; flag = !flag; The char flag is promoted to int before the ! operation. The result (although in range [0..1]) is of type int, which is larger than a char ==> warning. If you do not like the warnings, use explicit casts. (One of the reasons why I prefer Turbo C over MSC is that you can enable and disable warnings individually) Hope I haven't confused you even more, Peter. -- | _ | Peter J. Holzer | Think of it | | |_|_) | Technische Universitaet Wien | as evolution | | | | | hp@vmars.tuwien.ac.at | in action! | | __/ | ...!uunet!mcsun!tuvie!vmars!hp | Tony Rand |
browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) (08/29/90)
In article <90240.003415RHMCSUPV@MIAMIU.BITNET>, RHMCSUPV@MIAMIU.BITNET (Douglas M. MacFarlane) writes: > Excuse the novice question . . . > > I'm new to C and liking it, but this puzzles me. I'm using Microsoft > C ver 5.1. When I assign a value to a float variable, I get a data- > conversion warning (with warning level 2 set). Why? > > For example: > > float fValue ; /* define fValue as a floating point variable */ > > other lines > > fValue = 35 ; /* produces a Data Conversion warning at compile time */ > > > Whasss Happening ???? > > Douglas M. MacFarlane > rhmcsupv@miamiu.acs.muohio.edu From the Microsoft C 5.1 user's guide, page 275: C4051 data conversion Two data items in an expression had different types, causing the type of one item to be converted. "fValue" is a float. "35" is an int. The compiler converted an int to a float for you. If you wanted a float, it thinks, you'd have written 35.0. (Of course 35.0 is actually a double, as are all floating-point constants, but that's another story.) -- Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A. (216) 371-0043 The opinions expressed are mine. Mine alone! Nobody else is responsible for them or even endorses them--except my cat Dexter, and he signed the power of attorney only under my threat to cut off his Cat Chow!
volpe@underdog.crd.ge.com (Christopher R Volpe) (08/29/90)
In article <667.26da7736@iccgcc.decnet.ab.com>, browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) writes: |>In article <90240.003415RHMCSUPV@MIAMIU.BITNET>, RHMCSUPV@MIAMIU.BITNET (Douglas M. MacFarlane) writes: |>> |>> float fValue ; /* define fValue as a floating point variable */ |>> |>> other lines |>> |>> fValue = 35 ; /* produces a Data Conversion warning at compile time */ |>> |>> |>> Whasss Happening ???? |>> |>> Douglas M. MacFarlane |>> rhmcsupv@miamiu.acs.muohio.edu |> |> |>From the Microsoft C 5.1 user's guide, page 275: |> |>C4051 data conversion |> Two data items in an expression had different types, causing |> the type of one item to be converted. |> |>"fValue" is a float. "35" is an int. The compiler converted an int to a |>float for you. If you wanted a float, it thinks, you'd have written 35.0. |>(Of course 35.0 is actually a double, as are all floating-point constants, |>but that's another story.) Ok, so which is it? Is the compiler complaining about the int-to-double conversion for not writing 35.0? Or is it complaining about the double-to-float conversion resulting in possible loss of precision? Does the warning go away if you write "fValue=35.0;" or do you need to write "fValue = (float) 35.0"? ================== Chris Volpe G.E. Corporate R&D volpecr@crd.ge.com
prk@planet.bt.co.uk (Peter Knight) (08/29/90)
RHMCSUPV@MIAMIU.BITNET (Douglas M. MacFarlane) writes: >Excuse the novice question . . . > >I'm new to C and liking it, but this puzzles me. I'm using Microsoft >C ver 5.1. When I assign a value to a float variable, I get a data- >conversion warning (with warning level 2 set). Why? > >For example: > >float fValue ; /* define fValue as a floating point variable */ > >other lines > >fValue = 35 ; /* produces a Data Conversion warning at compile time */ ^^ This is an integer constant, as it does not of the form 35.0, 35e0 or 3.5e1, etc, which are floating point (actually double) constants. Peter Knight BT Research #include <std.disclaimer>
hp@vmars.tuwien.ac.at (Peter Holzer) (08/29/90)
volpe@underdog.crd.ge.com (Christopher R Volpe) writes: >Ok, so which is it? Is the compiler complaining about the int-to-double >conversion for not writing 35.0? Or is it complaining about the >double-to-float conversion resulting in possible loss of precision? >Does the warning go away if you write "fValue=35.0;" or do you >need to write "fValue = (float) 35.0"? It complains about the double-to-float conversion. Here is a program that shows the not quite consequent behaviour of MSC: int main () { char c; int i; long l; float f; double d; c = 0; i = c; l = i; f = l; /* warning */ d = f; f = d; /* warning */ l = f; /* warning */ i = l; /* warning */ c = i; /* warning */ c = c; f = f; c = ! c; /* warning */ f = f + 1.0; /* Why doesn't it warn about this one ? * f + 1.0 is of type double. */ return 0; } >================== >Chris Volpe >G.E. Corporate R&D >volpecr@crd.ge.com -- | _ | Peter J. Holzer | Think of it | | |_|_) | Technische Universitaet Wien | as evolution | | | | | hp@vmars.tuwien.ac.at | in action! | | __/ | ...!uunet!mcsun!tuvie!vmars!hp | Tony Rand |
brianh@hpcvia.CV.HP.COM (brian_helterline) (08/29/90)
volpe@underdog.crd.ge.com (Christopher R Volpe) writes: -In article <667.26da7736@iccgcc.decnet.ab.com>, -browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) writes: -|>In article <90240.003415RHMCSUPV@MIAMIU.BITNET>, -RHMCSUPV@MIAMIU.BITNET (Douglas M. MacFarlane) writes: -|>> -|>> float fValue ; /* define fValue as a floating point variable */ -|>> -|>> other lines -|>> -|>> fValue = 35 ; /* produces a Data Conversion warning at compile time */ -|>> -|>> -|>> Whasss Happening ???? -|>> -|>> Douglas M. MacFarlane -|>> rhmcsupv@miamiu.acs.muohio.edu -|> -|> -|>From the Microsoft C 5.1 user's guide, page 275: -|> -|>C4051 data conversion -|> Two data items in an expression had different types, causing -|> the type of one item to be converted. -|> -|>"fValue" is a float. "35" is an int. The compiler converted an int to a -|>float for you. If you wanted a float, it thinks, you'd have written 35.0. -|>(Of course 35.0 is actually a double, as are all floating-point constants, -|>but that's another story.) -Ok, so which is it? Is the compiler complaining about the int-to-double -conversion for not writing 35.0? Or is it complaining about the -double-to-float conversion resulting in possible loss of precision? -Does the warning go away if you write "fValue=35.0;" or do you -need to write "fValue = (float) 35.0"? MSC is complainting about the double-to-float conversion because there is loss of precision and not all doubles will fit into a float. On the other hand, all ints will fit into floats. The only way to quiet MSC is fValue = (float) 35 /* or 35.0 */
roger@everexn.uucp (Roger House) (08/31/90)
In <90240.003415RHMCSUPV@MIAMIU.BITNET> RHMCSUPV@MIAMIU.BITNET (Douglas M. MacFarlane) writes: >Excuse the novice question . . . > >I'm new to C and liking it, but this puzzles me. I'm using Microsoft >C ver 5.1. When I assign a value to a float variable, I get a data- >conversion warning (with warning level 2 set). Why? > >For example: > >float fValue ; /* define fValue as a floating point variable */ > >other lines > >fValue = 35 ; /* produces a Data Conversion warning at compile time */ > > >Whasss Happening ???? The type of the constant 35 is int. Thus, in order to assign it to a float variable, the constant must be converted to type float. This is what the warning is telling you. Most likely the compiler converts 35 to float so that there is no runtime overhead associated with the conversion. It is not clear that the warning in this case is of much value. However, in the case of fValue = i, where i is of type int, the warning is of use because it draws your attention to the fact that a conversion will be done at runtime. The obvious way to get rid of the warning is to write fValue = 35.0. However, if I remember MSC 5.1 correctly, this will also cause a warning. Reason: The constant 35.0 is of type double. Thus, in order to assign it to a float, there must be a conversion from type double to type float. Is there any way out? Yes: fValue = (float) 35. Roger House
mad-2@kub.nl (C. Wekx) (09/03/90)
I don't understand what the fuss is all about as a well-known C-author, H. Schildt writes in his book 'C: The complete reference' on page 40: float f=123.23, without an explicit type cast.... Joris
volpe@underdog.crd.ge.com (Christopher R Volpe) (09/04/90)
In article <1258@kubix.kub.nl>, mad-2@kub.nl (C. Wekx) writes: |>I don't understand what the fuss is all about as a well-known C-author, |>H. Schildt writes in his book 'C: The complete reference' on page 40: |>float f=123.23, without an explicit type cast.... This raises a question I've been wondering about for a while: When we say that a float (or char) gets converted to a double (or int) in an expression, what exactly constitutes an expression? Is a single value on the RHS of an assignment operator an "expression" for conversion purposes? Or is at least one operator required? If I do a "f1=f2", does it first convert f2 to double and then back to float in order to do the assignment? How about "c1=c2" for chars? ================== Chris Volpe G.E. Corporate R&D volpecr@crd.ge.com
scjones@thor.UUCP (Larry Jones) (09/04/90)
In article <11583@crdgw1.crd.ge.com>, volpe@underdog.crd.ge.com (Christopher R Volpe) writes: > This raises a question I've been wondering about for a while: When > we say that a float (or char) gets converted to a double (or int) in > an expression, what exactly constitutes an expression? Is a single > value on the RHS of an assignment operator an "expression" for > conversion purposes? Or is at least one operator required? If > I do a "f1=f2", does it first convert f2 to double and then back > to float in order to do the assignment? How about "c1=c2" for chars? An "expression" is defined by the C grammar. No operators are required, "i" is an expression as is "f1=f2" (note that "=" is the assignment >operator< in C). In fact "f1=f2" should cause f2 to be widened to double and then narrowed back to float. Fortunately, even very dump compilers are smart enough to optimize out the spurious widening and narrowing operations. ---- Larry Jones UUCP: uunet!sdrc!thor!scjones SDRC scjones@thor.UUCP 2000 Eastman Dr. BIX: ltl Milford, OH 45150-2789 AT&T: (513) 576-2070 Start tying the sheets together. We'll go out the window. -- Calvin
steve@taumet.com (Stephen Clamage) (09/05/90)
volpe@underdog.crd.ge.com (Christopher R Volpe) writes: |When we say that a float (or char) gets converted to a double (or int) in |an expression, what exactly constitutes an expression? Is a single |value on the RHS of an assignment operator an "expression" for |conversion purposes? Or is at least one operator required? If |I do a "f1=f2", does it first convert f2 to double and then back |to float in order to do the assignment? How about "c1=c2" for chars? In C, assignment is not necessarily a statement, as in some other languages. Assignment is accomplished by the '=' operator, and the assignment is an expression like any other. Section 3.3.16 (and its subsections) describe precisely what happens. Briefly, the value of the rhs is converted to the type of the lhs and the assignment is performed. As to whether multiple conversions are performed, the "as-if" rule applies. In principle, integral promotions and other specified conversions are performed (e.g., char to int) and the result converted to the type of the lhs. If this turns out to be identical in result to just copying the value, it is sufficient to copy the value. -- Steve Clamage, TauMetric Corp, steve@taumet.com