'simulate' arduino math

So my goal is to have a simple brute force way of checking what kind of results a line of equations on my arduino should give me, but without needing an arduino in front of me, or to re-write a simple sketch a hundered times to try a few iterations. I would assume there is something I can do in C++ to do this very easily, I just want to make sure I am not making any dumb assumptions. For example, with this pseudo code, I want to know waht my result will be for a few different values quickly:

float k0 = 100;
unsigned int Pre = 256;
byte lper = 20;
fud = 1562;

long Oc = fud*K0/Pre/lper-1;

obviously that is very sloppy and not good code. However I would really like to be able to quickly compile something and conclusivly know what value will fall out of misc equations with different values applied. Does the arduino do math the same way C++ does and a near identical code compiled on my PC would give me the same value? I have traditionally done this by uploading code and using the serial monitor, but its a PITA and I don't always have an arduino in front of me.

Thanks guys.

Does the arduino do math the same way C++ does

Since it IS C++ code that is compiled for the Arduino, the answer is yes. With limitations, of course. floats and doubles are the same size (4 bytes), so doubles don't give more precision.

and a near identical code compiled on my PC would give me the same value?

Yes.

But, that code, and how you initialize and type (or fail to) variables is crap.

floats should be assigned float values (100.0, not 100). Now, the compiler will see no difference, but using the .0 on the end makes it clear that you are paying attention.

You can install bloodshed dev C++. The sizes of several variable types are different than on Arduino but you can check your math. Or even easier, use an online c compiler. google online c compiler

To check your math for Arduino AVR processors, you should use the following types:

  • If you use int in the Arduino, use short in your PC program;
  • If you use unsigned int or unsigned in the Arduino, use unsigned short in your PC program;
  • If you use double in the Arduino, use float in your PC program;
  • If you use long in the Arduino and your PC/compiler are targeting 64-bit mode, use int in your PC program;
  • If you use unsigned long in the Arduino and your PC/compiler are targeting 64-bit mode, use unsigned int in your PC program;

If you have a modern C/C++ compiler that provides the stdint.h include file, you can use these types (after including stdint.h):

  • int16_t for Ardunio int;
  • uint16_t for Arduino unsigned or unsigned int;
  • int32_t for Arduino long;
  • uint32_t for Arduino unsigned long;
  • float for Arduino double.

You can use these types in your Arduino program, as the Arduino.h includes stdint.h.

It would probably do you a world of good in a "get off my lawn" kind of way to use pencil and paper.

KeithRB:
It would probably do you a world of good in a "get off my lawn" kind of way to use pencil and paper.

But you have to remember to truncate the values to match the types that you are using in the Arduino. If you use int, it can hold -32768 to +32767. Any larger value gets truncated. Similarly, if you use double, it is treated the same as float and you only get 6-7 digits of accuracy and it can only represent numbers up to +/- 3.40282e+38

Disregarding the floats, I was assuming he would do the math in long form binary, not decimal. 8^)

KeithRB:
Disregarding the floats, I was assuming he would do the math in long form binary, not decimal. 8^)

But my point is:

for (int i = 0; i < 30; i++) {
  Serial.println (1 << i);
}

Gives different output than doing the equivalent on a bigger computer due to the sizes of the int type.

for (int i = 0; i < 30; i++) {
  std::cout  << (1 << i) << "\n";
}

Agreed, but when using binary math with pencil and paper, you are strongly encouraged to keep the types the correct size to keep from doing extra work!

thanks guys! good to see I was on the right track. And yes the pseduo code is horrble, but that was my intent. I want to see my failures so I can get a good idea for exactly how the math goes down. for example one that always drvies me up a wall with doubt is type casting. IE are all those values of c the same, I figure the last two are, but the second one has me unsure.

int a = 10;
int b = 3;

float c = a/b;
c = a/(float)b;
c = (float)a/b;
c = float(a)/float(b);

permnoob:
thanks guys! good to see I was on the right track. And yes the pseduo code is horrble, but that was my intent. I want to see my failures so I can get a good idea for exactly how the math goes down. for example one that always drvies me up a wall with doubt is type casting. IE are all those values of c the same, I figure the last two are, but the second one has me unsure.

int a = 10;
int b = 3;

float c = a/b;
c = a/(float)b;
c = (float)a/b;
c = float(a)/float(b);

This gets into the area called implicit conversions. In general for most binary operators if the left side has a different type than the right side, the two sides are converted to a common type before the operation is done. If either of the sides is a floating point type (long double, double, float) and the other side is an integral type, the integral type is converted to the floating point type. So in

c = a/(float)b;

Because b was explicitly converted to float, a will be implicitly converted to float as well.