View unanswered posts | View active topics It is currently Thu Jul 31, 2014 9:34 am






Reply to topic  [ 8 posts ] 
RobotC multiplication error 
Author Message
Rookie

Joined: Sun Dec 05, 2010 11:58 am
Posts: 28
Post 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


Mon Feb 06, 2012 7:41 pm
Profile
Novice

Joined: Thu Apr 17, 2008 6:04 am
Posts: 70
Location: Plymouth, Indiana, USA
Post 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

_________________
Dad (Dave) and Adam


Mon Feb 06, 2012 8:52 pm
Profile
Rookie

Joined: Sun Dec 05, 2010 11:58 am
Posts: 28
Post 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.


Mon Feb 06, 2012 8:55 pm
Profile
Novice

Joined: Thu Apr 17, 2008 6:04 am
Posts: 70
Location: Plymouth, Indiana, USA
Post 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

_________________
Dad (Dave) and Adam


Mon Feb 06, 2012 9:26 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: RobotC multiplication error
C languages allow automatic up-conversion 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.


Mon Feb 06, 2012 9:50 pm
Profile
Rookie

Joined: Sun Dec 05, 2010 11:58 am
Posts: 28
Post 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.


Tue Feb 07, 2012 1:38 pm
Profile
Expert

Joined: Mon Oct 27, 2008 9:59 pm
Posts: 137
Post Re: RobotC multiplication error
Are you overrunning the radix in the float? What if you change:
Code:
#define inchtodegrees 150.78362

to something like:
Code:
#define inchtodegrees 150.783


Fri Feb 10, 2012 11:23 am
Profile
Rookie

Joined: Thu Feb 16, 2012 12:25 am
Posts: 3
Post 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);
}


Thu Feb 16, 2012 12:32 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.