Often I use text and number display functions to debug programs. I also use them a lot for NXT -> human interface in stable programs. I have recently been doing more with floating point numbers, and have seen several things I don't care for.
Here is a scenario:
I have a thermometer that is precise to about 0.02 degrees. I want to display the number in floating point, but I don't want it to read "24.1284", I want it to drop x number of exponents, so it will display "24.1". What I have been doing is calculating the temperature in floating point, multiplying by 10, setting it to a int, then setting it back to a float, and dividing it by 10. Sure, this makes it display "24.1", but when it is 24.0, it displays "24", which isn't what I want. That ".0" is very significant, and I want it displayed.
What I need to do is convert the number into a string and then display it as text. NumToStr works fine, but it drops exponent 0's, or anything in or beyond the 10,000th's place.
How do I do this? I came up with a clumsy function that does basically what I want, but I want to know how to do it "properly".
NXT text and number displaying
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
NXT text and number displaying
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXT text and number displaying
You use
If you want to store it in a string, use
Again, not sure if this actually works in NXC.
I really should figure this out for C++. All I can remember is that there's
printf()
in C. Not sure how much support NXC has for this, though:
Code: Select all
float x = 24.1284;
printf("%.1f", x);
sprintf()
:
Code: Select all
float x = 24.1284;
string s = "";
sprintf(s, "%.1f", x);
TextOut(0, 0, s, 0);
I really should figure this out for C++. All I can remember is that there's
stringstream
, and a whole bunch of other "flags"...Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: NXT text and number displaying
I'd indeed suggest printf() just as muntoo did. But there's also FormatNum(). You can look up the format strings in any C documentation, but you'll have to try out what NXC supports (I just have a nice link handy which happens to be for MATLAB, but the format specifiers should be the same). Maybe the %g is interesting for you, it should output significant digits of a float.
but that's not great either.
I currently use Qt's QString class, with the .arg() function, and I love it every day . Especially as it takes care of Unicode once and for all (let's hope) and has also methods like .toStdStr() to convert to other formats...
Yep, there are various stringstream examples. But I honestly got very annoyed with it. I then came accrossmuntoo wrote: I really should figure this out for C++. All I can remember is that there'sstringstream
, and a whole bunch of other "flags"...
Code: Select all
std::cout.precision(n);
I currently use Qt's QString class, with the .arg() function, and I love it every day . Especially as it takes care of Unicode once and for all (let's hope) and has also methods like .toStdStr() to convert to other formats...
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXT text and number displaying
Thanks both of you
I am now able to do what I want.
I am now able to do what I want.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXT text and number displaying
I normally use this:
this wrap around FormatNum is exactly the same as what NXC does with it's printf macro.
you may use numbers (integer, fp) formatted like in ANSI C, but as a substitute to "\n" you may print at (x,y) like with NumOut or TextOut:
advantage: you need not to clear the line to avoid "overwriting" garbage printing shorter numbers after longer ones.
for strings you may write - although it is sometimes not correctly interpreted by the firmware;
but if it should be faulty sometimes for strings you can use a workaround: pass your string by the Formatstring
http://www.cplusplus.com/reference/clib ... io/printf/
http://en.wikipedia.org/wiki/Printf
Code: Select all
#define printf1( _x, _y, _format1, _value1) { \
string sval1 = FormatNum(_format1, _value1); \
TextOut(_x, _y, sval1); \
}
you may use numbers (integer, fp) formatted like in ANSI C, but as a substitute to "\n" you may print at (x,y) like with NumOut or TextOut:
Code: Select all
printf1(0,40, "%5.2f", fpvar); // OR: printf1(0,40, "var=%5.2f", fpvar);
printf1(0,32, "%10d", longvar); // OR: printf1(0,32, "var=%10d", longvar);
printf1(0,24, "%06x", intVarAsHex); // OR: printf1(0,24, "var=%06x", intVarAsHex);
for strings you may write
Code: Select all
printf1(0, 8, "%s", stringvar);
but if it should be faulty sometimes for strings you can use a workaround: pass your string by the Formatstring
Code: Select all
printf1(0,0, "write your string constant or variable", 0);
http://en.wikipedia.org/wiki/Printf
Code: Select all
#define printf1( _x, _y, _format1, _value1) { \
string sval1=FormatNum(_format1, _value1); \
TextOut(_x, _y, sval1); \
}
task main(){
int i=1000;
long l=87654321;
float f= 123.45678;
string msg="HUG ME!";
printf1(0,56,"%6d", i); // 6 digits, rest space (%i = %d)
printf1(0,48,"0x%04x",i); // 4 digits hex notation, rest Zeros + "0x" prefix
printf1(0,40,"%10d", l); // 10 digits, rest space
printf1(0,32,"%7.2f",f); // 7 digits including 2 decimals, rest space
printf1(0,16,"%s", msg); // string output
while(1);
}
Who is online
Users browsing this forum: No registered users and 0 guests