# 如何正确处理大型算术

``````void calc() {
batterymoment = input*batterymass;
totalmoment = 149650+batterymoment;
buoyancymoment = buoyancymass*centrebuoyancy;
wheelmoment = totalmoment - buoyancymoment;
wheelweight = totalmass - buoyancymass;
cgwheels = wheelmoment/wheelweight;
maindistance = mainarm - wheelweight;
nosedistance = wheelweight - nosearm;
mainmass = (((wheelweight)*(1-((maindistance)/(maindistance+nosedistance))))/2);
nosemass = (((wheelweight)*(1-((nosedistance)/(nosedistance+maindistance))))/2);
mainmoment = mainmass*mainarm;
nosemoment = nosemass*nosearm;
netmoment = buoyancymoment+mainmoment+nosemoment;
netcg = netmoment/totalmass;

}
``````

``````unsigned long batterymoment,num1,num2,input,totalmoment;
double buoyancymoment,wheelmoment,cgwheels,mainmass,nosemass,maindistance,nosedistance;
double centrebuoyancy = 14.164;
double mainarm = 14.3;
double nosearm = 1.8;
int totalmass = 12404;
int batterymass = 720;
int buoyancymass = 4401;
int batteryposition,wheelweight;
boolean mySwitch = false;

void calc() {
batterymoment = input*batterymass;
totalmoment = 149650+batterymoment;
buoyancymoment = buoyancymass*centrebuoyancy;
wheelmoment = totalmoment - buoyancymoment;
wheelweight = totalmass - buoyancymass;
cgwheels = wheelmoment/wheelweight;
maindistance = mainarm - wheelweight;
nosedistance = wheelweight - nosearm;
mainmass = (((wheelweight)*(1-((maindistance)/(maindistance+nosedistance))))/2);
nosemass = (((wheelweight)*(1-((nosedistance)/(nosedistance+maindistance))))/2);
mainmoment = mainmass*mainarm;
nosemoment = nosemass*nosearm;
netmoment = buoyancymoment+mainmoment+nosemoment;
netcg = netmoment/totalmass;
}

void setup() {
Serial.begin(9600);
num1=0;
num2=0;
Serial.println("Enter battery position");
}

void loop() {
while (Serial.available()){
if(batteryposition>47 && batteryposition<58){
if(!mySwitch){
num1=(num1*10)+(batteryposition-48);
}else{
num2=(num2*10)+(batteryposition-48);
}
}

if(batteryposition==61){
input=num1+num2;
calc();
Serial.print("Static margin is: ");
num1=0;
num1=0;
mySwitch=false;
}
}
}
``````

0

## 2 答案

3

``````void calc() {
Serial.print("input:");Serial.println(input);
batterymoment = input*batterymass;
totalmoment = 149650+batterymoment;
buoyancymoment = buoyancymass*centrebuoyancy;
wheelmoment = totalmoment - buoyancymoment;
wheelweight = totalmass - buoyancymass;
cgwheels = wheelmoment/wheelweight;
maindistance = mainarm - wheelweight;
nosedistance = wheelweight - nosearm;
mainmass = (((wheelweight)*(1-((maindistance)/(maindistance+nosedistance))))/2);
nosemass = (((wheelweight)*(1-((nosedistance)/(nosedistance+maindistance))))/2);
mainmoment = mainmass*mainarm;
nosemoment = nosemass*nosearm;
netmoment = buoyancymoment+mainmoment+nosemoment;
netcg = netmoment/totalmass;