Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 0 converted to 0.0 for float variable?

Reply
Thread Tools

0 converted to 0.0 for float variable?

 
 
vlsidesign
Guest
Posts: n/a
 
      11-08-2007
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.

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-08-2007
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
 
Reply With Quote
 
 
 
 
Peter Pichler
Guest
Posts: n/a
 
      11-08-2007
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.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      11-08-2007
vlsidesign <(E-Mail Removed)> 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 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-08-2007
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
jaysome
Guest
Posts: n/a
 
      11-09-2007
On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl
<(E-Mail Removed)> 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
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      11-09-2007
jaysome wrote:
> On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl
> <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
static_cast<float>(a) versus float(a) Jim West C++ 4 01-16-2004 12:36 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM
Does a float need to be converted to network order? sidewinder C++ 10 06-26-2003 10:52 AM



Advertisments