View unanswered posts | View active topics It is currently Sun Sep 21, 2014 5:03 pm






Reply to topic  [ 3 posts ] 
Why 59/2=29.0 in %f 
Author Message
Rookie

Joined: Sat Jul 13, 2013 3:19 am
Posts: 1
Post Why 59/2=29.0 in %f
Code:
task main()
{
   while(1)
   {
      nxtDisplayTextLine(3,"%f",59/2);
      wait1Msec(100);
   }
}

Image
How can make it correct?
Thank.


Sat Jul 13, 2013 3:30 am
Profile
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post Re: Why 59/2=29.0 in %f
In integer math, 59/2 = 29. You aren't saving the result in a float, or type casting the result as a float. "%f" tells the function to format the value as a float, but the parameter you are passing is integer (i.e. 59/2).

_________________
Matt


Sat Jul 13, 2013 11:05 am
Profile WWW
Professor
User avatar

Joined: Sat May 18, 2013 1:24 pm
Posts: 272
Location: Olympia, WA
Post Re: Why 59/2=29.0 in %f
A quick way to fix this would be to use:
Code:
nxtDisplayTextLine(3,"%f",(float)59/2);

Of course, if these things bother you, you're welcome to convert ("cast") both numbers to a float.

_________________
FTC Team 6424, the 'Oly Cow - Chief programmer.
FRC Team 4450, Olympia Robotics Federation (ORF).

and also quadrotors. Quadrotors!


Sat Jul 13, 2013 7:05 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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.