I remember seeing this somewhere, but for the life of me, I can't seem to find it again.
I have some values, in my case, battery voltage and the amp draw from my motors. To keep the resolution, but loose the float, things get multiplied by 10. For example, my voltage within the program would be considered 117. When I display this value on my screen, I want to see 11.7 volts. I need to artificially add that decimal point --only so I, a human, can read it more easily.
Now, I think there is something about dividing by 100, then 10 or something. I started thinking this way but quickly ran into the issue of how the result will be "rounded". I guess there might be a solution in dealing with the number as a string and counting the chars, but I dunno.
These has had to come up before, can I get a nudge in the right direction?
To make the algorithm more general if you don’t know how big your number is you can determine the number of digits by taking the log of the number, adding one and casting it as an integer, like this by D = log(x) + 1. You can then use that to start a for loop that will do Jad’s algorithm as many times as required.
No rounding? How do I not know this? After 2+ years of this stuff and the whole time I thought things were being rounded. Huh?
You are of course right, one simple test program and sure enough, no rounding. I am now mentally going through every problem I have ever had and trying to figure out if it was because of this issue.
Man, I can’t believe I thought things were being rounded… So simple…
When displaying internal values from my electricity meter, I decided to “invent” units like hecto Watt and deci second. They are mathematically correct and are easily abbreviated using SI conventions.
I love you all, I really do… …This was fixed, coded and incorporated two days ago! It really comes down to the fact that I simply didn’t know that rounding was not being done. I just always sorta figured that 19/10=2. What the hell, you learn something everyday.
As we sit now, the simple /10 method is working A-ok. I always know how many digits I’m going to get and the decimal place will never change. This is not to say that in the future I am going to need something more complex. I like birdmun’s idea of that extra variable --I always dig good “universal” routines where you can stick anything you want into it and it will know what to do with it. Or in this case, be able to ask for different forms of output with the same few lines of code.
So we’re groovy. I have my volts and amp draw (for each motor) up on my display (with the aforementioned decimal) and I’m all set.
I should take a moment and plug the Wild Thumper controller though. I have sorta “gotten back to it” in terms of finishing up some of the code. (This is what led to the decimal question) It is now my servo driver, ADC input slave and motordriver. All the volt and current data is coming from it and so far, has been spot-on. I mean, checked with a good meter, spot-on numbers. Speaking of which, it was the fact that I could see current draw on my display that let me know that one of my motors decided to start drawing 13+ amps and needed to be fixed. Damn good motor driver.