LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2015, 02:10 AM   #1
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
ruby drops a small log


I was looking for the log (base 10 ) of 1000

Code:
ruby:
Math::log(1000, 10)
=> 2.9999999999999996

 Math::log(1000)/Math::log(10)
=> 2.9999999999999996
that's a bit rubbish isn't it?

perl
Code:
$ perl -e 'print log(1000)/(log(10))'  
3
lisp
Code:
(log 1000 10)
3
even C
Code:
int main(int argc, char ** argv)
{
    for( ++argv; *argv; ++argv) {

        float x = atof(*argv);
        printf("log10(%f) = %f", x, log10(x));

    }
 return 0;
}
$ ./log 1000
log10(1000.000000) = 3.000000
even a schoolboy should get that right

Last edited by bigearsbilly; 02-03-2015 at 02:25 AM.
 
Old 02-03-2015, 03:22 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Floating point calculations are never absolutely precise. Please learn this by heart.

Also read this: https://en.wikipedia.org/wiki/Floati...uracy_problems
 
Old 02-03-2015, 03:26 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
I am well aware of floating points.

It is nevertheless, still, a bit rubbish.
 
Old 02-03-2015, 03:44 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I'm afraid you don't have enough experience in this question, that's why you have exaggerated expectations.
 
Old 02-03-2015, 03:57 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
No experience indeed?

I expect the log base 10 of 1000 to be 3

Call me old-fashioned!
 
Old 02-03-2015, 04:02 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
even the MS windows calculator gets it right!
 
Old 02-03-2015, 07:54 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 02-03-2015, 08:14 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That was a very useful piece of information, even if unrelated to the topic.
 
Old 02-03-2015, 09:16 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Ok ... so I am lost ... how does using the right function to get the answer be unrelated?
 
Old 02-03-2015, 09:20 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Methinks the answer the OP wanted to hear would have been this: 'yes, you're right, Math::log in ruby is lame'

Edit: an example written in C (tested on x86):
Code:
/* acc.c */

#include <stdio.h>

int main (void)
{
    double f;

    f= 0.1;
    printf ("%g\n", 10*f - 1);
    return 0;
}
What result should I expect?

Last edited by NevemTeve; 02-03-2015 at 09:30 AM.
 
Old 02-03-2015, 12:47 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
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.
 
Old 02-04-2015, 01:27 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
OK well done Grail, that's the answer then.
phew!

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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RVM + local install ruby + system ruby maintenance Corpus-Khu Linux - Software 0 02-13-2014 01:45 AM
Any issues installing Ruby Gems and Ruby on Rails in Slackware? Lufbery Slackware 8 02-09-2011 07:22 PM
Ruby SDL/ Ruby OpenGL tutorials/reference? LinuxNoob75 Programming 0 12-30-2007 12:47 PM
Sysstat drops a message in /var/log/messages every 10 minutes... how to stop this? zan_messengrr Linux - General 2 11-15-2006 09:40 AM
man despairs as imap-server drops log in request esteeven Linux - Software 7 02-05-2004 04:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration