Simulator issues with arithmetics

This forum is for all topics related to Simulator for Arduino v0.98. Please include sample code where possible.

Moderator: Adrian

juho
Posts: 7
Joined: Tue Mar 05, 2013 8:09 am

Simulator issues with arithmetics

Postby juho » Sat Apr 06, 2013 7:13 am

Hi,
I found one difference in simulator behavior compared to arduino hardware. I had a simple expression in my code that calculated value based on analog digits. See code below. In real arduino analog_value_1 and analog_value_2 return same result as they should. The simulator however is confused by the simpler form without ().

BR,
Juho


int analog_sum = 300;
int summed_digits = 3;
float analog_coefficient = 0.05;
float analog_value_1;
float analog_value_2;

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

analog_value_1 = analog_sum/summed_digits*analog_coefficient;
analog_value_2 = (analog_sum/summed_digits)*analog_coefficient;

Serial.print("1st equation: ");
Serial.print(analog_sum);
Serial.print("/");
Serial.print(summed_digits);
Serial.print("*");
Serial.print(analog_coefficient);
Serial.print(" = ");
Serial.println(analog_value_1);

Serial.print("2nd equation: (");
Serial.print(analog_sum);
Serial.print("/");
Serial.print(summed_digits);
Serial.print(")*");
Serial.print(analog_coefficient);
Serial.print(" = ");
Serial.println(analog_value_2);


}

void loop() {
}

juho
Posts: 7
Joined: Tue Mar 05, 2013 8:09 am

Re: Simulator issues with arithmetics

Postby juho » Thu Apr 11, 2013 6:32 am

This may be part of same issue I found in my code with if statement of following form:

if(custom_function() == false) {
}

Here custom_function() returns a boolean value. Simulator returns error while trying to evaluate the if statement even though this statement works in real arduino hardware without problems. I modified code as follows to make it run in simulator:

boolean custom_function_return = custom_function();
if(custom_function_return == false) {
}

BR,
Juho

Simulator_admin
Site Admin
Posts: 239
Joined: Thu Feb 02, 2012 6:07 pm

Re: Simulator issues with arithmetics

Postby Simulator_admin » Thu Apr 11, 2013 12:10 pm

Hi Juho

With the code below, the Simulator has an order of precedence and the multiply * comes before the divide / so the expression with brackets will work. I guess that using BODMAS, division should come first. We will look into this.

Code: Select all

analog_value_1 = analog_sum/summed_digits*analog_coefficient;
analog_value_2 = (analog_sum/summed_digits)*analog_coefficient;


With the subroutines, this is a recent upgrade and no doubt there are some issues. When evaluating subroutines, the Simulator loads the return value when the return statement is found and should then substitute the value back into the equation. We will look into this soon. Thanks


Return to “Version 0.98”

Who is online

Users browsing this forum: Google [Bot] and 1 guest