ROBOTC.net forums http://www.robotc.net/forums/ 

RobotC multiplication error http://www.robotc.net/forums/viewtopic.php?f=1&t=4209 
Page 1 of 1 
Author:  jorge_the_awesome [ Mon Feb 06, 2012 7:41 pm ] 
Post subject:  RobotC multiplication error 
I was working on a program today and came across a very strange error. Multiplying a float by an integer gives bad values. I'm sorry, I don't have the exact decimals with me. 150.(something)*10=340.(something), according to robotC. But 150*10=1500, as it should. The code was just a statement to display the values on the screen. And no, we did not make %f vs %d value formatting errors. We can get around the issue by just using 150, but we now have to check the rest of our code for incorrect multiplications. Thanks! Jorge_the_awesome 
Author:  Dad and Adam [ Mon Feb 06, 2012 8:52 pm ] 
Post subject:  Re: RobotC multiplication error 
Is this really an issue? My programming knowledge is limited, but I somehow concluded a while ago that floats and integers should never be mixed. One way to unmix the variable types is by "typecasting" as shown below. FloatVarible * (float)IntegerVariable Where the "(float)" temporarily changes an integer variable into a float. Or similarly: (int)FloatVariable * IntegerVariable Dave 
Author:  jorge_the_awesome [ Mon Feb 06, 2012 8:55 pm ] 
Post subject:  Re: RobotC multiplication error 
I tried typecasting the integer to a float. It still didn't work. And anyway, other programming languages can handle multiplying floats by integers. They might not handle rounding correctly, but they'll work. Implicit type conversions are usually done automatically. 
Author:  Dad and Adam [ Mon Feb 06, 2012 9:26 pm ] 
Post subject:  Re: RobotC multiplication error 
I just now tried multipling some floats and integers and it handled the typecasting automatically. I tried both assigning values to variables and just putting in values directly (like 43 * 29.0). It all cases it produced the correct answer. I even did your example of 150.1 * 10 and got 1501. I seem to recall that previous releases of RobotC were not so forgiving with mixed variable types (but I could be wrong). So now the question is: Why did it work with mine but not yours? I'm using RobotC 3.04, running in Windows XP. Dave 
Author:  MHTS [ Mon Feb 06, 2012 9:50 pm ] 
Post subject:  Re: RobotC multiplication error 
C languages allow automatic upconversion in mix type arithmetic expressions. So float multiply with integer will give you float. You need to post your code. I suspect you have some syntax mistake somewhere but without seeing the real code, it's hard to tell. 
Author:  jorge_the_awesome [ Tue Feb 07, 2012 1:38 pm ] 
Post subject:  Re: RobotC multiplication error 
Windows 7, RobotC 3.04 #define inchtodegrees 150.78362 move takes(long, long, int, int), although I saw the incorrect values from a nxtDisplayTextLine commands in both betterMove and move. betterMove was called as betterMove(10,10,0,80); void betterMove(int deltax,int deltay,int deltatheta, int power) { move((long)(inchtodegrees*deltax),(long)(inchtodegrees*deltay),deltatheta,power); } The long typecasts were added in an attempt to resolve the problem, which occurs with or without them. We've had problems earlier with the random(int) function crashing. Could there be something wrong with our installation? RobotC 3.04 on Windows 7. In other programs, we've had errors with the inch to encoder degree conversion constants (also floats and in similar functions). We didn't know what the errors were, but we suspect that they were caused by other multiplication errors. And those programs were on different NXTs and a different computer. 
Author:  l0jec [ Fri Feb 10, 2012 11:23 am ]  
Post subject:  Re: RobotC multiplication error  
Are you overrunning the radix in the float? What if you change:
to something like:

Author:  VatsanM [ Thu Feb 16, 2012 12:32 am ] 
Post subject:  Re: RobotC multiplication error 
I had a similar issue with multiplication/division in RobotC 3.05. I tried to calculate the circumference of the wheel so that I can get the distance the wheel travels. If I use a variable, then it fails. If I use a variable it fails, but if I use constants it works perfectly. here is a snippet. If I replace 96 with dist variable, it fails. void moveByDistance(int dist) { nMotorEncoder[motorG] = 0; // Reset the Motor Encoder of Motor B. nMotorEncoder[motorF] = 0; float rotation = ((CLICKS_PER_ROTATION * dist)/(3.14 * WHEEL_SIZE_IN_INCH * GEAR_RATIO)); //rotation = 5504 * 2; //96 inches rotation = CLICKS_PER_ROTATION * 96 /(3.14 * WHEEL_SIZE_IN_INCH * GEAR_RATIO); //rotation = 1440; //nMotorEncoderTarget[motorG] = rotation; // Set the target for Motor Encoder of Motor B to 1440. //nMotorEncoderTarget[motorF] = rotation; while ((nMotorEncoder[motorG] < rotation) && (nMotorEncoder[motorF] < rotation))//while the encoder wheel turns one revolution { moveStraight(90); } //moveStraight(speed); // While Motor F AND Motor G are still running (haven't yet reached their target) //while(nMotorRunState[motorG] != runStateIdle && nMotorRunState[motorF] != runStateIdle) //{} stopAllMotors(); //wait1Msec(2000); } 
Page 1 of 1  All times are UTC  5 hours [ DST ] 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ 