# Easy C math question

We wrote some code to do some iterative functions. They did not work as expected so I started breaking it down. It got so simple that I finally realized what was causing the problem - simple math.

Here is our simple code

StartTimer(1);
while (1);
LeftArmTimer=GetTimer(1);
PrintToScreen(“Main Loop %d\n” , LeftArmTimer);
TempA=LeftArmTimer / 350 ; // TempA is declared as int
PrintToScreen(“TempA%d” , TempA);
TempB=TempA/2;
PrintToScreen(“TempB%d” ,TempB);
wait(5);

Here is a snapshot of the output…

TempA1TempB0Main Loop 672
TempA1TempB0Main Loop 678
TempA1TempB0Main Loop 684
TempA1TempB0Main Loop 690
TempA1TempB0Main Loop 696
TempA1TempB0Main Loop 702
TempA2TempB1072693248Main Loop 708
TempA2TempB1072693248Main Loop 714
TempA2TempB1072693248Main Loop 720
TempA2TempB1072693248Main Loop 726
TempA2TempB1072693248Main Loop 732
TempA2TempB1072693248Main Loop 738

I have tried this with TempB declared as both FLOAT and as DOUBLE. Doesnt seem to make a difference.

HELP PLEASE. It doesn’t make sense to me.

Yes, I believe %d is for integers and %f is for floats

I’m not sure what you are trying to do.

The statement while(1); would run as an infinite loop.

In RobotC the timers report back in milliseconds. Your wait at the bottom is only for 5 milliseconds so dividing by 350 does not make any sense.

Foster, this is EasyC, not Robotc.
Pinion, pasting code is better than handtyping misleading mistakes.
The following code compiles in EasyC as a user C code function, but I don’t have a cortex handy to run it to check output.
At least it has all the vars declared locally so we can see their types.
Note that GetTimer() returns unsigned long (32 bit pos = 4billion),
your application can likely define LAT as int, which is max 2billion,
so that all the variables are the same type.
PrintToScreen uses C printf descriptors, so fixing position count, and prepending 0 to get leading zeros should make columns line up better.
so it might help to print unsigned long in their correct marker %lu,
etc
Welcome to the strict typecasting world of C

``````#include "Main.h"
void timerloop ( void )  {
unsigned long LAT;   int TempA;   int TempB;
StartTimer ( 1 ) ;
while (1){ // repeat forever
LAT=GetTimer(1); // LAT is ulong, same as GetTimer
TempA=LAT / 350 ; // TempA is declared as int, want 0..=>0, 350..=>1
TempB=TempA/2;  //TempB is int : want 0.. =>0, 2.. =>1
PrintToScreen("\n LAT:%012lu  A:%04d  B: %04d" , LAT,TempA,TempB);
Wait ( 5 ) ; // wait N milliseconds,  maybe 50 would be better than 5, since print takes a while...
} // wend
} //timerloop``````

Edit: I agree with stack overflow link. you didn’t comment if TempB was int, or float, or long, or what type

Thanks JGrabber for the reminder it’s Easy C.

Your code makes sense since I can now see the braces.

Still confused about the divide by 350 since that section of code will run in a much shorter time.

Looks like you are using a double and printing the hex (well decimal) representation of it. 1072693248 is hex 0x3FF00000, if this were the upper 32 bits of a 64 bit double (0x3ff0000000000000), it would have been 1.0. (ie. 1.0 in it’s 8 byte double internal format is 0x3ff0000000000000).

So, you calculated 2/2 which is 1.0

You need to use %lf as the format string for double numbers.

Thanks for all the help guys.

As a mentor, every moment should be a teaching moment.

We started out talking about doing something on a repitive basis, like every 1/2 second. For example, turn on a motor for 1/2 second in one direction, stop, run the motor in the opposite direction for 1/2 second and then repeat.

So, I started talking about a counter, then using the counter to determine whether it was odd or even. This led to a formula for comparison where the number would be divided by 2 and compared to the int(of the number divided by 2).

Using the crazy math functions in Easy C led to interesting case for the variables and I suspect run time errors which are not reported back. Stupidly we displayed the values and jumped to conclusions without looking at the %d vs %f in the display.

SO, one last question if you all know…

When we enter a line of C code in easy C will it properly interpret the line or do we need to use the math functions built into the system?

Thanks for everyone’s help.

Not sure what you mean by “math functions built into the system”. EasyC is just using the math functions in the standard library that comes with gcc, same as PROS and ConVEX. EasyC uses the CodeSourcery release of gcc, they should be correct.

I don’t have a working EasyC license in front of me to check, but I believe that if you want to type your own code (for example a math equation), you need to use the “user code” option from the menu.