I have a number that is determined from some parameters for some LED's and to calculate them correctly i need to ensure the number i always rounded up to the nearest integer regardless of whether its 1.008 or 1.95 however i seem to be having great difficulty finding some code that works for this or coming up with something myself.
I tried first some of another website that involved using the modulo function but it ignored the 1.008 value and a couple others but in the end i have resorted to dare i say it floating point math.
My code i have is below however although works for some does not work for all such as in the case of 255 / 10 which returns 25.5 so should be 25 but doesn't get recognized or rounded.
Delta_G:
To round a float variable up to the next whole number:
float someFloat = 4.2;
int roundedUp = ((int)someFloat) +1;
Casting to int will always truncate the number, that is round it down. So just add one to the number and now it's rounded up.
That will not work when the float has a fractional part of .000000, unlikely as that is.
How many decimal places of accuracy are you looking for? I'd be inclined to do the math as integers if possible, since float can sometimes give an erroneous answer such as x.000001 or x.999999 instead of precisely x.000000
I have a number that is determined from some parameters for some LED's and to calculate them correctly i need to ensure the number i always rounded up to the nearest integer regardless of whether its 1.008 or 1.95 however i seem to be having great difficulty finding some code that works for this or coming up with something myself.
That is not the normal function of round(), 1.008 would round down.
You could probably try something like round(f1/f2 + 0.4999999)
I am working out the steps of increment in an LED animation and i have this correct in an excel sheet i have attached in an image below but i use the RoundUp function which is where my issue lies.
Delta_G that works perfect for the 30 different tests i have just tried.
I am also sorry if i caused confusion i don't know why it always ends up with me having to explain 3 different ways i genuinely thought about my first question and then confused everyone with the word integer.
I appreciate your help although i must once again say it strangely disslikes the two numbers 40/23 and although this equals 1.73 it returns a 0 in the double !! section and as such doesnt round up.
I must say that i dont quite understand that section of the code and as such having trouble working out why this is happening.
void setup() {
Serial.begin(9600);
Serial.println(calculate(40, 23));
}
int calculate(int num1, int num2){
int output = num1 / num2 + !!(num1%num2);
return output;
}
In fact upon typing this out i discovered that instead of % i had put an & well atleast its here for everyone to see now and working, thank you for your help.