Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c int to double calculation [duplicate]

Possible Duplicate:
C programming division

I'm trying to calculate the period of accelerometer updates using a user entered frequency.

this is my code:

double interval = 1/Freq;

interval = period

Freq is an int set by the user.

The problem I'm having is lets say I set Freq to 2Hz so the interval should be 0.5 but instead interval is 0.0000000 why is this? Can I do anything to change it without changing Freq to a double?

like image 238
Mike Khan Avatar asked Feb 20 '12 14:02

Mike Khan


1 Answers

You are using integer division: (both 1 and Freq are integers). So the result will be an integer, and more exactly 0 in this case.

You can do something like this:

double interval = 1.0 / Freq;

Or

double interval = 1 / (double)Freq;
like image 190
sch Avatar answered Oct 29 '22 20:10

sch