473,320 Members | 1,914 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,320 software developers and data experts.

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.

Nov 8 '07 #1
6 3930
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
Nov 8 '07 #2
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.
Nov 8 '07 #3
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.

--
Er*********@sun.com
Nov 8 '07 #4
vlsidesign <fo*****@gmail.comwrites:
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;}}}
Nov 8 '07 #5
On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl
<ma*****@earthlink.netwrote:
>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
Nov 9 '07 #6
jaysome wrote:
On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl
<ma*****@earthlink.netwrote:
>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
Nov 9 '07 #7

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

Similar topics

7
by: Ben | last post by:
Hi all, I'm not yet good at thinking the right way in c++ so although I could solve this problem, I'm not sure if they way I'm thinking of is the best way to do it. I need a data type or class...
8
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need...
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!!!
15
by: Kay Schluehr | last post by:
I wonder why this expression works: >>> decimal.Decimal("5.5")**1024 Decimal("1.353299876254915295189966576E+758") but this one causes an error 5.5**1024 Traceback (most recent call...
4
by: rass.elma | last post by:
Hi all I fetch the following value from a string (VCAHR(250))colmun in a MySql table: "300000000000000000000000000000000000000000000000000" When I write it out using echo() I get : 3E+50 ...
7
by: Partho | last post by:
I have a float variable which I need to add to a short variable. How do I do this? Do I have to typecast or is there a way around? I tried typecasting the float to a short, but that gives me a 0 or...
2
by: robert | last post by:
in Gnuplot (Gnuplot.utils) the input array will be converted to a Numeric float array as shown below. When I insert a numpy array into Gnuplot like that below, numbers 7.44 are cast to 7.0 Why is...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.