0 converted to 0.0 for float variable?

V

vlsidesign

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.
 
M

Martin Ambuhl

vlsidesign said:
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
 
P

Peter Pichler

vlsidesign said:
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.
 
B

Ben Pfaff

vlsidesign said:
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
 
E

Eric Sosman

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.
 
J

jaysome

#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
 
J

jacob navia

jaysome said:
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

:)
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top