TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Jett Genilo
from: Bob Stout
date: 1998-08-28 22:49:48
subject: (double) a/b

On , Jett Genilo (6:750/113{at}fidonet) wrote: 

 >  int a,b,;
 >  double u,z;

 >  z = (double) a/b;    /* what's the purpose of the "()" on
double here? */

Jett...

  It's a type cast. The quotient of one int divided by another int is
itself an int. Without a type cast, the compiler should issue a warning
that your integer quotient is being assigned to a double.

Using the (double) tells the compiler to convert  to a double
before performing the division. In this case, this forces  to also
be converted to a double so the resulting quotient is a double.

 >  u = (double) (a/b);     /* what's the purpose of the 2 "()" here? */

  In this case, you're telling the compiler to convert the integer quotient
after the division. Perhaps some test code might help...


----[ Test.C ]----

#include 

int main(void)
{
      int a, b;
      douvle u, z;

      z = (double)a / b;            /* Spaces added for clarity   */
      u = (double)(a/b);

      printf("Using floating point math, %d/%d = %f\n", a, b, z);
      printf("Using integer math, %d/%d = %f\n", a, b, u);

      return 0;
}

----[ Test.Out ]----

Using floating point math, 5/2 = 2.500000
Using integer math, 5/2 = 2.000000

----[ finis ]---- 


--- QM v1.00
* Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
SEEN-BY: 396/1 622/419 632/0 371 633/260 267 270 371 634/397 635/506 728 810
SEEN-BY: 639/252 670/213 218
@PATH: 30883/6 106/2000 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.