Writing first Propeller program, need really basic help with rounding numbers
BigMike
Posts: 34
Hello Parallax,
Looooooong time BASIC Stamp user here, bought my first Propeller and am building my first project!
I am struggling with a super basic issue and I can't seem to find helpful documentation. I'm using SimpleIDE v1.0.1 RC1 along with a USB Propeller Activity Board.
I am performing a calculation on a float variable and am outputting its value to a 20x4 blue LCD screen. I need it to be a float because I would like to display the number to two decimal points, so I am trying to use round() to trim as such but SimpleIDE says "undefined reference to `_roundf'". Hmmmm? What is _roundf ?
My programming background is mainly with PHP & MySQL (web development) and I figured I'd try num = round(num,2) to round/trim to 2 decimal points but its not allowing it. So I thought maybe the Spin language doesn't use round(), but after a Google search I found some Propeller Reference Manual (http://www.parallax.com/sites/default/files/downloads/P8X32A-Web-PropellerManual-v1.2.pdf) which indeed does list a ROUND function, but its not working for me.
I thought about writing spaces to the characters after the 2nd decimal spot to essentially hide the rest of the number, but there has got to be a proper way to do this?
I did do quite a bit of searching and found http://forums.parallax.com/showthread.php/130526-Rounding-Numbers-with-the-Propeller, but the link Martin_H suggests is dead and I don't understand why if round() is in the manual then why do we need additional libraries?
Thank you very much in advance for the help!
Regards,
BigMike
Looooooong time BASIC Stamp user here, bought my first Propeller and am building my first project!
I am struggling with a super basic issue and I can't seem to find helpful documentation. I'm using SimpleIDE v1.0.1 RC1 along with a USB Propeller Activity Board.
I am performing a calculation on a float variable and am outputting its value to a 20x4 blue LCD screen. I need it to be a float because I would like to display the number to two decimal points, so I am trying to use round() to trim as such but SimpleIDE says "undefined reference to `_roundf'". Hmmmm? What is _roundf ?
My programming background is mainly with PHP & MySQL (web development) and I figured I'd try num = round(num,2) to round/trim to 2 decimal points but its not allowing it. So I thought maybe the Spin language doesn't use round(), but after a Google search I found some Propeller Reference Manual (http://www.parallax.com/sites/default/files/downloads/P8X32A-Web-PropellerManual-v1.2.pdf) which indeed does list a ROUND function, but its not working for me.
I thought about writing spaces to the characters after the 2nd decimal spot to essentially hide the rest of the number, but there has got to be a proper way to do this?
I did do quite a bit of searching and found http://forums.parallax.com/showthread.php/130526-Rounding-Numbers-with-the-Propeller, but the link Martin_H suggests is dead and I don't understand why if round() is in the manual then why do we need additional libraries?
Thank you very much in advance for the help!
Regards,
BigMike
Comments
Are you using Spin's round() function on a constant? That's the only way the function can be used (see below)...
Link to the manual: http://www.parallax.com/downloads/p8x32a-propeller-manual
From the Web-PropellerManual-v1.2.pdf manual:
dgately
Ahh, this makes sense now, I should have read more carefully.
Thank you for the link on this. If I read this correctly, it only rounds to the nearest integer? If so I think I like dbpage's idea below:
Ahh, I remember doing this for an assignment in a circuits lab back in college with my old Stamp. I already know how this can work and will go this route
Thanks again everyone, I'll post up my code later tonight after work
Regards,
BigMike
Got some time during my lunch break to play with my code and I can't even figure out how to get a float converted to a int so I can do integer math on it.
In a simple example, here is what I'm trying to do:
So it's only displaying 0.000000. Converting it to an integer I'd assume would print just the number 2 but so far it's only printing exactly 0.000000. Even on the terminal, it's doing the same. I tried a direct "test = vdd" and I still get "0.000000"
Any ideas?
Thanks
Mike
Do you want a %f format specifier to print an int (test)? I haven't used C in a while, I think %d is for an int.
John Abshier
John, thank you, that did the trick. This is what I'm talking about....
Maybe there just needs to be more basic examples on the Learn section of parallax.com. ......or I am spoiled in the way I came to Parallax, which was through a circuits lab class during college. Our professor taught us everything we needed and I never really browsed parallax.com. Now that I'm embarking on my own with the Propeller, it's a bunch of Google searches and trial and error, and of course excellent and speedy help now that I've begun posting to the forums '
Glad to be receiving such great help here, looking forward to leaning a lot with the Propeller and using it to build some of my longstanding plans!
Cheers,
BigMike
Here is what I'm using:
Displays, "Voltage: 2.65" and I'm not too concerned about the rounding so this works great for me.
Later in my code I've converted the voltage into manifold pressure (PSI) and encounter negative numbers. Here is what I'm doing for that along with positioning the line of text just where I want it on my LCD. Recycling n & d variables throughout:
Added blank spaces after smaller numbers to write over lingering figures per refresh
Regards,
BigMike
Documentation available through SimpleIDE Help ....
SimpleIDE User Guide
PropellerC Tutorials
Simple Library Help
PropGCC Reference
Another starting point for beginners: learn.parallax.com/propellerc
More experienced users may also learn about what parallax thinks is important in the parallax propellerc pages.
Some favorite C programming documentation links:
http://publications.gbdirect.co.uk/c_book/
http://www.cplusplus.com/reference/clibrary/
For curmudgeonous old C programmers:
Use Linux and type "man " or whatever makes you happy.
Hope this post helps a bit. If not, "we" can sort it out.
I am wondering, is there a way to use strings with the Propeller? Instead of having two variables and all these if, else if statements depending on variable size, I could just write the numbers to a string and display one string.
As a simple example, if I have the number 2.55, I am currently displaying it as:
...where '2' is stored to the 'n' int variable and '55' is stored to the 'd' int variable. I am having to code this as, %d.%d where the decimal is included between the two variables to simulate the full number.
What I'm currently thinking would be easier is to try something like...
This is like a mix of PHP and C I suppose, as I tried it and it doesn't work :P
Where it becomes more beneficial is that I have other parts of my code where I have a display of, for example, "Thr: 12%, MAP: 2.55v", and what I'm having to do is move around on the same line with a dprint command for "Thr:", another dprint for "%, MAP:", then another dprint for the Throttle value, then another dprint for the MAP value, and then a final dprint for the voltage sign "v". So that is five dprint's that I'm shifting numbers and spaces around based on variable length when if I could just store all of this to a text string and then just send the one text string to the display in one dprint that would be much more efficient.
Thoughts?
Thanks
Mike