Arduino can't do math?? //Resolved//

Basically, I am finding that my Arduino can't do math.. Can someone please weigh in on this?

I am attempting to perform the following computation:

RYout = ((255/(255-RYstart))RYin)-((255255)/(255-RYstart))+255;

All variables are declared as int.

Take RYstart to be 105. The following table shows the expected and actual behavior:
(1 is RYin, 2 is RYout from Arduino, 3 is RYout expected according to excel, calculator, etc)
1 | 2 | 3
165 423 102
176 434 120.7
182 440 130.9
185 443 136
191 449 146.2
194 452 151.3
206 464 171.7
208 466 175.1
213 471 183.6
218 476 192.1
224 482 202.3
237 495 224.4
253 511 251.6
255 513 255

What is going on?

Thanks,
John

What type of variables are you using?
bytes, ints, floats?
What you are experiencing is overflow problems, typical, some casts here and there will solve it.

jdeck004:

RYout = ((255/(255-RYstart))*RYin)-((255*255)/(255-RYstart))+255;

You do not mention the types of RYout, RYstart and RYin. Are these integer types? The expression you've given could be evaluated entirely with integer calculation and give a different result to what you think it should, if it were all float.

Try changing all the constant 255 to 255.0 and see if that helps. If RYout, RYstart and RYin are meant to be floating point, try making then float. Or at least mention what type you have them now.

Senso:
What type of variables are you using?
bytes, ints, floats?
What you are experiencing is overflow problems, typical, some casts here and there will solve it.

All are ints. I don't have any issue changing them or using cast (although I don't really know how). I tried changing them all to long as well, which did change anything. I have read that I should avoid floats as they are finnicky.

gardner:

jdeck004:

RYout = ((255/(255-RYstart))*RYin)-((255*255)/(255-RYstart))+255;

You do not mention the types of RYout, RYstart and RYin. Are these integer types? The expression you've given could be evaluated entirely with integer calculation and give a different result to what you think it should, if it were all float.

Try changing all the constant 255 to 255.0 and see if that helps. If RYout, RYstart and RYin are meant to be floating point, try making then float. Or at least mention what type you have them now.

This solved the problem!! Amazing!

Arduino can do maths, you just have to get the datatypes right.

Calculators and Excel use floating-point arithmetic.

You have a problem here straight away:

255*255

That is 65025, right?

The largest value you can store in an int is 32767.

If you make them floats it works:

#include <Streaming.h>

void setup ()
{
  Serial.begin (115200);
  
  float RYout;
  float RYstart = 105;
  
  for (long RYin  = 165; RYin <= 255; RYin++)
    {
    RYout = ((255.0/(255.0-RYstart))*RYin)-((255.0*255.0)/(255.0-RYstart))+255.0;  
    Serial << RYin << " " << RYout << endl;
    }
}

void loop () {}

Results:

165 102.00
166 103.70
167 105.40
168 107.10
169 108.80
170 110.50
171 112.20
172 113.90
173 115.60
174 117.30
175 119.00
176 120.70
177 122.40
178 124.10
179 125.80
180 127.50
181 129.20
182 130.90
183 132.60
184 134.30
185 136.00
186 137.70
187 139.40
188 141.10
189 142.80
190 144.50
191 146.20
192 147.90
193 149.60
194 151.30
195 153.00
196 154.70
197 156.40
198 158.10
199 159.80
200 161.50
201 163.20
...