Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 1/2 =?

Reply
Thread Tools

1/2 =?

 
 
hyu163@gmail.com
Guest
Posts: n/a
 
      06-24-2005
Hi there, i'm a newbie in c.
I'm using Turbo C 2.0 compiler.
Here is my question:


main(){
int a=1, b=2;
float c;

c=a/b;
printf("%f", c); /* why i get "0.0000"? */

c = (float)(a/b);
printf("%f", c); /* why i get "0.0000" again? */

c = (float) a/b;
printf("%f", c); /* then i get "0.5000" this time */

}


so, 1/2=?

thanks!

 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      06-24-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi there, i'm a newbie in c.
> I'm using Turbo C 2.0 compiler.
> Here is my question:
>
>
> main(){
> int a=1, b=2;
> float c;
>
> c=a/b;
> printf("%f", c); /* why i get "0.0000"? */


Since a and b are int, division is done in the int domain

1 goes into 2 zero times with 1 remainder
so, the result is 0

Since c is a float, the results of the int division are converted to float
0 becomes 0.0


>
> c = (float)(a/b);
> printf("%f", c); /* why i get "0.0000" again? */


Since a and b are int, division is done in the int domain

1 goes into 2 zero times with 1 remainder
so, the result is 0

The result is then cast to float
0 becomes 0.0

Since c is a float and the results of the computation are in float, no
conversion is performed

0.0 remains 0.0

> c = (float) a/b;
> printf("%f", c); /* then i get "0.5000" this time */


a and b are int

(float) a converts a into a float, so 1 becomes 1.0

Now the division is between a float and an int, and so the computation and the
results are float
1.0 / 2 becomes 1.0 / 2.0 which becomes 0.5

Since c is a float and the results of the computation are in float, no
conversion is performed

0.5 remains 0.5

> }
>
>
> so, 1/2=?
>
> thanks!
>



- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCu22eagVFX4UWr64RAuE9AJ9VegjhFu3qzvZexaRRts cNmPNiQwCg0r37
wm1fKvBgiVrhhrkSo+PyKOw=
=85zE
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-24-2005
(E-Mail Removed) wrote:

> so, 1/2=?


1 / 2 == 0
1 / 2.0 == 0.5
1.0 / 2 == 0.5
1.0 / 2.0 == 0.5

--
pete
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      06-24-2005
(E-Mail Removed) wrote:
> Hi there, i'm a newbie in c.
> I'm using Turbo C 2.0 compiler.
> Here is my question:
>
>
> main(){
> int a=1, b=2;
> float c;
>
> c=a/b;
> printf("%f", c); /* why i get "0.0000"? */


Integer division. The expression `1/2' yields 0, which is then assigned
to the float `c'.
>
> c = (float)(a/b);
> printf("%f", c); /* why i get "0.0000" again? */


Same thing. `1/2' yields 0 which is then cast to a float and ultimately
assigned to `c'.
>
> c = (float) a/b;
> printf("%f", c); /* then i get "0.5000" this time */
>
> }
>

Ah, *this* time, because `a' (the `1') has been cast to float, it's
*not* integer division that takes place -- so you get the answer you
were expecting.
>
> so, 1/2=?


Well, 0, of course!

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
 
Reply With Quote
 
Dan Henry
Guest
Posts: n/a
 
      06-24-2005
(E-Mail Removed) wrote:

>Hi there, i'm a newbie in c.
>I'm using Turbo C 2.0 compiler.
>Here is my question:
>
>
>main(){
>int a=1, b=2;
>float c;
>
>c=a/b;
>printf("%f", c); /* why i get "0.0000"? */


Integer division of 1 by 2 yields 0. Zero converted to a float is
0.0.

>c = (float)(a/b);
>printf("%f", c); /* why i get "0.0000" again? */


Integer division of 1 by 2 yields 0. Zero converted to a float is
0.0.

>c = (float) a/b;
>printf("%f", c); /* then i get "0.5000" this time */


Integer 'a' explicitly converted to a float (1.0) divided by 'b'
implicitly converted to a float (2.0) reasonably yields 0.5.

>so, 1/2=?


In the expression 1/2, both operands are integers, so the answer is 0.
See above.

--
Dan Henry
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      06-24-2005
Lew Pitcher wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> (E-Mail Removed) wrote:
> > Hi there, i'm a newbie in c.
> > I'm using Turbo C 2.0 compiler.
> > Here is my question:
> >
> >
> > main(){
> > int a=1, b=2;
> > float c;
> >
> > c=a/b;
> > printf("%f", c); /* why i get "0.0000"? */

>
> Since a and b are int, division is done in the int domain
>
> 1 goes into 2 zero times with 1 remainder
> so, the result is 0


2 goes into 1 zero times with 1 remainder.

Robert Gamble

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-24-2005
(E-Mail Removed) wrote:
>
> main(){
> int a=1, b=2;
> float c;
>
> c=a/b;


a/b is 0 with a remainder of 1

> printf("%f", c); /* why i get "0.0000"? */
>
> c = (float)(a/b);


when you convert 0 to a float you hope to get 0. Of course the
cast is unnecessary, since the system will do the conversion on the
assignment. All casts are suspect.

> printf("%f", c); /* why i get "0.0000" again? */
>
> c = (float) a/b;


Now you used a meaningful cast. (float)a is a float with the value
1.0. Division by an integer, such as 2, requires that the integer
be promoted to a real, in this case 2.0. Now you are computing
1.0/2.0 and in place of a remainder you have more significant
digits.

> printf("%f", c); /* then i get "0.5000" this time */


--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
MJ
Guest
Posts: n/a
 
      06-24-2005
Hi
The thing is that when you have two int the result will be like this
int a,b ;
so a/b will always be int value
the result takes the largest data type of the two
thats why a/b = int

in the second case when you do a type cast
it does not have any effect. Since the result is already generated and
then if you type cast it has no meaning

In the third case you are type casting the variable a from int to float

so when you divide the two variables after type casting it will take
the largest data type in the sence ( float) and
you will get the desired result.

if you dont want to type cast you can just multiply any of the int
varialbe with 1.00 and you will get the expected result
float C = (a*1.00)/ b = (float)a / b = a / (flaot)b = a/(b*1.00)


Mayur

 
Reply With Quote
 
akarl
Guest
Posts: n/a
 
      06-25-2005
(E-Mail Removed) wrote:
> Hi there, i'm a newbie in c.
> I'm using Turbo C 2.0 compiler.
> Here is my question:
>
>
> main(){
> int a=1, b=2;
> float c;
>
> c=a/b;
> printf("%f", c); /* why i get "0.0000"? */
>
> c = (float)(a/b);
> printf("%f", c); /* why i get "0.0000" again? */
>
> c = (float) a/b;
> printf("%f", c); /* then i get "0.5000" this time */
>
> }
>
>
> so, 1/2=?


Here we go again. See the thread "pow(2, 1/2) != pow(2, 0.5)" started by
Michel Rouzic 2005-06-15. No, it's not at all a stupid question, it's
one of the C obscurities.
 
Reply With Quote
 
snnn
Guest
Posts: n/a
 
      06-27-2005
>
> main(){
> int a=1, b=2;
> float c;
>
> c=a/b;
> printf("%f", c); /* why i get "0.0000"? */


Since a and b are int, a/b should be int too,and the result is 0.
then,the result is to be assigned to a float variable,the result "0" is
converted to "0.0000"

>
> c = (float)(a/b);
> printf("%f", c); /* why i get "0.0000" again? */
>

(float) convert the result of (a/b) to float.
so the result is the same as above.


> c = (float) a/b;
> printf("%f", c); /* then i get "0.5000" this time */
>
> }
>

first, "(float)" convert a to a float value,so the division is done in
the float domain,the result is certain should be 0.5000.

>
> so, 1/2=?
>

^_^


 
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




Advertisments