# Floating Point (Help)

I have wrote some code what I want to do is measure an analog voltage Vin (512-1023)

Change the voltage in to a frequency, 2.5V = 25Hz and 5V = 50Hz

so

(Vin/1023)*50 = Hz

(Hz/50)*86 is the number of steps in my Table (86/50)=1.72 Iwant it as an int so I round with ceil

`````` index = 43;
int TNUM=86;
float TEST_HERTZ = ((TEST_VIN)/1023.0)*50.0;
float FLOAT_NUM = TEST_HERTZ*1.72;
NUM=ceil(FLOAT_NUM);
HERTZ =ceil(TEST_HERTZ);

STEP_TIME = (1.0)/(HERTZ*4*NUM);
float x = TEST_HERTZ*1.72;
float y = (TEST_HERTZ*FLOAT_NUM)/17.5;

Timer1.initialize(STEP_TIME);
index=0;

increment=1;

Serial.println(STEP_TIME);
Serial.println(HERTZ);
Serial.println(NUM);

Serial.println(TEST_HERTZ);
``````

I have printed the values above and I cant get the step time to display it just says 0.00???????

I have

steptime = 0
Hertz=50
NUM=86
TEST_HERTZ= 49.71

Now after playing around I can see its the line 1.0/(HERTZ4NUM)

(HERTZ4NUM) =17200

which is just too big for a 16 bit number, is this whyI get a result of 0?

1/17200= 0.00

How can I work around this please, is it possible?

``````STEP_TIME = (1.0)/(HERTZ*4*NUM);
``````

None of these are declared as a variable type: index, NUM, HERTZ, and STEP_TIME.
If you were coding in Perl, it wouldn't matter but in AVR GCC they might default to int.

17200

which is just too big for a 16 bit number,

Too big for a 14 bit number maybe.

Yeah your right!, I dunno what I was thinking actually

But that leaves the question why does it return 0.00 for 1/17200

I am a total noob

Floating point 1.0 divided by any number over 1.0 makes a result less than 1. 1.0 / 17200 is much less than 1.

If STEP_TIME is an int then when you try to assign less than 1 to it, it will equal 0.

Integer, you know what that is? .... -3, -2, -1, 0, 1, 2, 3, 4.... no fractions, no decimals, integers only.

int x = 1/2; // x == 0

Hey thank you very much, that makes perfect sense and I actually managed to get it working now

I like maths and I know what an int is etc

Thanks again

Sorry mate but I wasn't sure how much you know. There is much of the basics to C code you haven't picked up yet though. In time I am sure you will.

GoForSmoke:
Sorry mate but I wasn't sure how much you know. There is much of the basics to C code you haven't picked up yet though. In time I am sure you will.

My python professor put this sort of problem on every exam this semester. People still got it wrong on the final, apparently. It's really not that difficult compared to the crap you get in java, IMO

int x = 5
int y = 6

solve x/y