I have a formula that uses floats. I’d like to make it float free, and use ints only. Ideas?

The float version:

variables:

Where num = 2300, bin will return 93

// convert mm to bin numbers
int convert() {
float mm;
float maxmm = 2.330;
float range = .003;
float jump = .004;
int maxl = 100;
mm = (float)num/1000;
float formula = round((((maxmm-(range/2.000))-mm)/jump));
int f;
f = (int)formula;
int bin = maxl-f;
return bin;
}

You have to decide on the precision you want, and then multiply everything by that.

So, let's say you want a 3 decimal precision - you'd pick a multiply factor of 1000.

Representing 9 would be 9000. Representing 0.004 would be 4.

Then, at the end, divide the whole answer by the scaling factor.

// convert mm to bin numbers
int convert(int num) {
long convnum;
long mm;
int maxmm = 2330;
int range = 3;
int jump = 4;
long maxl = 100000;
convnum = num * 1000; // scale it
mm = num/1000; // Yes, this makes the previous line redundant - but only for a scaling of 1000.
long formula = (((maxmm-(range/2))-mm)/jump);
return formula/1000;
}

Of course, I don't know if this actually works, as I haven't tested it, but you get the idea (I hope)

float maxmm = 2.330;
float range = .003;
float jump = .004;
int maxl = 100;

If they are constants, you can calculate the result, then your formula will be easier. After that, you can multiply everything in order to eliminate decimals.

probably does not do what is intended. Working with integers requires a bit thinking about intermediate results, underflow and overflow. float arithmetics make us a bit lazy

jorgepl, different sections of the program have different constants based on what gear family you are testing (ok, so that kind of makes them variables, doesn’t it? My bad). You are still using floats in that formula.

I’ve attached the complete sketch so you can see how it’s being used. I’m trying to speed things up by eliminating float arithmatic, since I can put an int in, and get an int out.