NO

Author Topic: divisions  (Read 5096 times)

Marten

  • Guest
divisions
« on: December 30, 2006, 07:43:20 pm »
How can i divide 4/8 in c or 3/2 or things like that cause i always get 0.0000 when i use the following code:

float a=4/8;
printf("%f",a);

!?!?!? :cry:

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1889
divisions
« Reply #1 on: December 30, 2006, 08:31:37 pm »
Code: [Select]

#include <stdio.h>
#include <math.h>

int main(int argc,char **argv)
{
float a;
a=4/8;
printf("%f\n",a);  //0.000000
a=4.0/8;
printf("%f\n",a);  //0.500000
a=(float)4/8;
printf("%f\n",a);  //0.500000
return 0;
}
May the source be with you

cane

  • Guest
divisions
« Reply #2 on: December 30, 2006, 08:33:39 pm »
Either:
Code: [Select]

float a=4.0/8.0;
printf("%f",a);

Code: [Select]

float a=(float)4/8;
printf("%f",a);

Marten

  • Guest
divisions
« Reply #3 on: December 30, 2006, 09:07:38 pm »
Thanx !!!