View unanswered posts | View active topics It is currently Sat Jul 30, 2016 5:15 am

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Can someone explain displaying floating point values?
Author Message
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia
Can someone explain displaying floating point values?
Hi

I'm having a bit of trouble displaying floating point values that make sense.

Can anyone explain why the code below is displaying the following:

x= 536870912
PI= 1610612736

Does the display show decimals? Why doesn't the display show

x= 7.89
PI= 3.142 etc

 Code:task main(){   float x;    eraseDisplay();   x= 7.89;    nxtDisplayTextLine(1, "x = %d", x);    nxtDisplayTextLine(4, "PI = %d", PI);   wait1Msec(10000); //pause ten seconds}}

Thanks
James

Tue Feb 20, 2007 8:01 am
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia

To answer my own email in the interest of others who may follow.

To correctly display floating point variables you need to define how many decimal points you want displayed in the format shown below.

 Code:task main() { float x;    x= (60/180.0); eraseDisplay(); nxtDisplayTextLine(1, "x = %+5.3f", x); nxtDisplayTextLine(4, "PI = %+5.3f", PI); wait1Msec(10000); //pause ten seconds }

Another thing to note is that, in order for x to be a decimal/floating point the 180 needs to be written as 180.0 (otherwise x displays 0). Is this intentional? Is it a convention of C based languages? This took me quite a while to work out.

Thanks
James

Wed Feb 21, 2007 9:44 pm
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 616

Regarding evaluating the equation "x = 60 / 180;" where 'x' is a 'float' variable. This is the behavior of the standard C language. Here's what is happening:
• Arithmetic expressions are evaluated using the type of the parameters('integer' or 'float'). so [int] / [int] is evaluated with integer arithmetic and results in an [int] result. If one of the operands is a [float] then floating point arithmetic will be used in the expression calculation.
• 60 /180, using integer arithmetic results in 0.
• The result of the expression is converted to a floating point value (0.0) before it is stored in variable 'x'.

So when you changed '180' to '180.0', the compiler then did the calculations using floating point arithmetic and you get the desired results of 0.3333.

You'd get the same original results if the code was.
int i = 60;
int j = 180;
float x;

x = i/j;

There is a way to force the compiler to use floating point arithmetic in the above. You use a "cast" statement which converts one 'type' to another. The last line above would be replaced with

x = i / (float) j;

The "(float)" is a 'cast' modifier which says convert the expression to the right to a 'float' value. There's a lot more powerful things that you can do with 'cast' but can't be covered here.

Mon Feb 26, 2007 5:01 am
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia

Dear Dick,

Thank you for elaborating on this topic. Very helpful.

James

Tue Feb 27, 2007 6:52 am
Rookie

Joined: Fri Mar 16, 2007 11:00 pm
Posts: 2
Re: Can someone explain displaying floating point values?
 Code:task main(){   float x;    eraseDisplay();   x= 7.89;    nxtDisplayTextLine(1, "x = %d", x);    nxtDisplayTextLine(4, "PI = %d", PI);   wait1Msec(10000); //pause ten seconds}}

Just change %d for %f in your code, I think it will work.

Fri Mar 16, 2007 11:14 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 5 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning