ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Like many languages ... it is about learning to use the correct tool:
Code:
$ irb
irb(main):001:0> Math.log10(1000)
=> 3.0
There is also log2. The standard log function is to base e, hence when the additional parameter is supplied there is a division done involving e and the new base.
Hence the rounded figure presented in your example.
Well, "lame" would be a pretty good word for it, but still it is par for the course. The difference between the implementations is simply the amount of implied rounding that the formatting library built in to the language decided to use. The internal representation of a float-binary number is never exactly as it appears when printed in base-10. It's mildly surprising to see the difference, as well as the obvious kludge in the case of .log10(), but mathematically it's not wrong.
In case you were wondering, I was thinking of using log10 for formatting floating points for FX data. I want a 5 digit display.
Truncated this should give me the significand length -1.
log10 (1000.0) = 3.
So I need 1 digit after the point.
I need to format as text anyway I can easily do it the unsubtle way but I thought I'd try it as it's a bit more aesthetically pleasing.
Last edited by bigearsbilly; 02-04-2015 at 02:27 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.