- **C Programming**
(*http://www.velocityreviews.com/forums/f42-c-programming.html*)

- - **0 converted to 0.0 for float variable?**
(*http://www.velocityreviews.com/forums/t550414-0-converted-to-0-0-for-float-variable.html*)

0 converted to 0.0 for float variable?Newbie question: 0 converted to 0.0 for float variable?
If I have the following code, float myTotal; myTotal = 0; will C automatically add a decimal point and at least one zero to the right of the decimal place? I am expecting that 0 will become at least 0.0 or maybe something with more zeros. |

Re: 0 converted to 0.0 for float variable?vlsidesign wrote:
> Newbie question: 0 converted to 0.0 for float variable? > > If I have the following code, > float myTotal; > myTotal = 0; > > will C automatically add a decimal point and at least one zero to the > right of the decimal place? I am expecting that 0 will become at least > 0.0 or maybe something with more zeros. > #include <stdio.h> inline void pf(int n, float x) { printf("%%9.%df %9.*f; ", n, n, x); printf("%%9.%dg %9.*g; ", n, n, x); printf("%%9.%de %9.*e\n", n, n, x); } int main(void) { float myTotal = 0; int i; for (i = 0; i < 4; i++) pf(i, myTotal); putchar('\n'); myTotal = 1; for (i = 0; i < 4; i++) pf(i, myTotal); return 0; } %9.0f 0; %9.0g 0; %9.0e 0e+00 %9.1f 0.0; %9.1g 0; %9.1e 0.0e+00 %9.2f 0.00; %9.2g 0; %9.2e 0.00e+00 %9.3f 0.000; %9.3g 0; %9.3e 0.000e+00 %9.0f 1; %9.0g 1; %9.0e 1e+00 %9.1f 1.0; %9.1g 1; %9.1e 1.0e+00 %9.2f 1.00; %9.2g 1; %9.2e 1.00e+00 %9.3f 1.000; %9.3g 1; %9.3e 1.000e+00 |

Re: 0 converted to 0.0 for float variable?vlsidesign wrote:
> Newbie question: 0 converted to 0.0 for float variable? > > If I have the following code, > float myTotal; > myTotal = 0; > > will C automatically add a decimal point and at least one zero to the > right of the decimal place? I am expecting that 0 will become at least > 0.0 or maybe something with more zeros. Almost good. 0 gets converted to (float)0, which indeed can be written as 0.0f, but that is not the same as adding extra zeros in the conversion. C works with values, that's all you need to know. |

Re: 0 converted to 0.0 for float variable?vlsidesign <fordgwf@gmail.com> writes:
> If I have the following code, > float myTotal; > myTotal = 0; > > will C automatically add a decimal point and at least one zero to the > right of the decimal place? I am expecting that 0 will become at least > 0.0 or maybe something with more zeros. > The value 0 will be converted to type float for storage in myTotal, yes. Zero is a value that can be represented exactly as a floating-point number, so it's not really meaningful to try to count the "number of zeros" being stored. Floating-point numbers are probably represented like this in your computer: http://en.wikipedia.org/wiki/IEEE_754 -- char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[] ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+ 2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}} |

Re: 0 converted to 0.0 for float variable?vlsidesign wrote On 11/08/07 15:54,:
> Newbie question: 0 converted to 0.0 for float variable? > > If I have the following code, > float myTotal; > myTotal = 0; > > will C automatically add a decimal point and at least one zero to the > right of the decimal place? I am expecting that 0 will become at least > 0.0 or maybe something with more zeros. Strictly speaking, the statement involves a float variable and an int value. When you assign a value of one type to a variable of a different type, the value is converted to the type of the variable (if possible) as part of the process. So the statement says - obtain an int with the value zero - convert it to the equivalent float value - store that float value in myTotal In practice, most compilers will recognize that 0 is not just an int but an int whose value does not change, and therefore they will know that the conversion from int to float will always produce the same result. So they'll take a shortcut and generate code to do - obtain a float with the value zero - store that float value in myTotal Note that I'm being very careful to talk about types (int, float) and values (zero), not about how the values are represented in C source. The description above does not talk about adding decimal points or adding zero digits to something in your source code; it talks about what the computer does with the values and variables that your source code defines. You need to know what the various code constructs mean -- 0 and 0.0 and .0f and 0L and 0u are all zeroes, but all different -- but you should think of the program as working with the values, not with the source code that produced them. You'll be happier that way, I promise. -- Eric.Sosman@sun.com |

Re: 0 converted to 0.0 for float variable?On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl
<mambuhl@earthlink.net> wrote: >vlsidesign wrote: >> Newbie question: 0 converted to 0.0 for float variable? >> >> If I have the following code, >> float myTotal; >> myTotal = 0; >> >> will C automatically add a decimal point and at least one zero to the >> right of the decimal place? I am expecting that 0 will become at least >> 0.0 or maybe something with more zeros. >> > >#include <stdio.h> > >inline void pf(int n, float x) There should be an informal rule that states that code that uses the inline function specifier should also include at least a sprinkle of "//" comments :^) And anyone who uses the inline function specifier should insure that their compiler conforms to the current standard (in at least one specific area) by subjecting it to the following code (in a hosted environment): inline int main(void) { return 0; } Regards -- jay |

Re: 0 converted to 0.0 for float variable?jaysome wrote:
> On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl > <mambuhl@earthlink.net> wrote: > >> vlsidesign wrote: >>> Newbie question: 0 converted to 0.0 for float variable? >>> >>> If I have the following code, >>> float myTotal; >>> myTotal = 0; >>> >>> will C automatically add a decimal point and at least one zero to the >>> right of the decimal place? I am expecting that 0 will become at least >>> 0.0 or maybe something with more zeros. >>> >> #include <stdio.h> >> >> inline void pf(int n, float x) > > There should be an informal rule that states that code that uses the > inline function specifier should also include at least a sprinkle of > "//" comments :^) > > And anyone who uses the inline function specifier should insure that > their compiler conforms to the current standard (in at least one > specific area) by subjecting it to the following code (in a hosted > environment): > > inline int main(void) > { > return 0; > } > > Regards OOOOOPS!!! I just discovered I forgot that test. Thanks :-) -- jacob navia jacob at jacob point remcomp point fr logiciels/informatique http://www.cs.virginia.edu/~lcc-win32 |

All times are GMT. The time now is 04:56 AM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.