The Naked Scientists
  • Login
  • Register
  • Podcasts
      • The Naked Scientists
      • eLife
      • Naked Genetics
      • Naked Astronomy
      • In short
      • Naked Neuroscience
      • Ask! The Naked Scientists
      • Question of the Week
      • Archive
      • Video
      • SUBSCRIBE to our Podcasts
  • Articles
      • Science News
      • Features
      • Interviews
      • Answers to Science Questions
  • Get Naked
      • Donate
      • Do an Experiment
      • Science Forum
      • Ask a Question
  • About
      • Meet the team
      • Our Sponsors
      • Site Map
      • Contact us

User menu

  • Login
  • Register
  • Home
  • Help
  • Search
  • Tags
  • Member Map
  • Recent Topics
  • Login
  • Register
  1. Naked Science Forum
  2. Non Life Sciences
  3. Geek Speak
  4. Borland C++ builder log() bug?
« previous next »
  • Print
Pages: [1]   Go Down

Borland C++ builder log() bug?

  • 8 Replies
  • 7430 Views
  • 0 Tags

0 Members and 1 Guest are viewing this topic.

Offline Boogie (OP)

  • Full Member
  • ***
  • 63
  • Activity:
    0%
    • View Profile
Borland C++ builder log() bug?
« on: 21/08/2012 23:47:04 »
It appears that Borland C++ compiler (version 6) has a bug with the log() function.

For example, a compiled application using log(15) returns 2.708. My calculator and OpenOffice agree that log(15) = 1.706

Anyone else encounter this? I fought this problem almost all day thinking I was doing something wrong!!!
Logged
 



Offline RD

  • Naked Science Forum GOD!
  • *******
  • 9094
  • Activity:
    0%
  • Thanked: 152 times
    • View Profile
Re: Borland C++ builder log() bug?
« Reply #1 on: 22/08/2012 00:27:39 »
Logs can have different bases ...

Quote
The logarithm to base b = 10 is called the common logarithm and has many applications in science and engineering. The natural logarithm has the constant e (≈ 2.718) as its base; its use is widespread in pure mathematics, especially calculus. The binary logarithm uses base b = 2 and is prominent in computer science.
https://en.wikipedia.org/wiki/Logarithm

Quote from: Boogie on 21/08/2012 23:47:04
For example, a compiled application using log(15) returns 2.708.

That looks like log to base e 

 [ Invalid Attachment ]

* log 15 in dfferent bases.png (5.89 kB, 383x147 - viewed 2784 times.)
« Last Edit: 22/08/2012 00:40:25 by RD »
Logged
 

Online CliffordK

  • Naked Science Forum King!
  • ******
  • 6474
  • Activity:
    34.5%
  • Thanked: 33 times
  • Site Moderator
    • View Profile
Re: Borland C++ builder log() bug?
« Reply #2 on: 22/08/2012 00:46:44 »
Also see the change of base formula in the Wikipedia article above.

https://en.wikipedia.org/wiki/Logarithm#Change_of_base

It looks complicated, but it turns out to be  relatively simple calculation because "b" is a constant, so you just calculate the constant divisor.  If Borland c++ doesn't already have the function, you could write a quick function to change the base to the desired base (10?)
Logged
 

Offline Boogie (OP)

  • Full Member
  • ***
  • 63
  • Activity:
    0%
    • View Profile
Re: Borland C++ builder log() bug?
« Reply #3 on: 22/08/2012 02:07:47 »
Thanks Clifford, I agree. I think Borland got them backwards. The base 10 command : log10(x) produces 1.176.

This drove me nuts. The log function was only a small part of the algorithm I was working on and it took me forever to finally suspect it. I did not expect this sort of library bug with a version six release.

I just noticed that I really "fat fingered" the return value in my initial post. :-[
Logged
 

Online CliffordK

  • Naked Science Forum King!
  • ******
  • 6474
  • Activity:
    34.5%
  • Thanked: 33 times
  • Site Moderator
    • View Profile
Re: Borland C++ builder log() bug?
« Reply #4 on: 22/08/2012 06:16:18 »
I don't think it is a Borland thing, but rather a C++ thing...  and perhaps Standard C.

http://www.cplusplus.com/reference/clibrary/cmath/log/

Probably somebody 30 years ago decided to use Log(x) as the natural log, rather than using ln(x).
Then, to make each version of the program more or less backward compatible, the old syntax was pushed forward.

In Unix, ln means to "link" files.
Logged
 



Offline Boogie (OP)

  • Full Member
  • ***
  • 63
  • Activity:
    0%
    • View Profile
Re: Borland C++ builder log() bug?
« Reply #5 on: 22/08/2012 16:33:54 »
Yeah, I think you're right. Unless I'm just getting confused, Dynamic C appears to be doing the same thing.

One solution :

#define LOG         log10   //lib is wrong
#define LOG10     log       //lib is wrong

Now when I use LOG(x) it replaces it with log10(x). LOG10(x) is replaced by log(x)

I was a little suprised that this worked. It's a little anal, but it might save me some head scratching if I need to go back and trouble shoot the algorithm sometime in the future.
Logged
 

Online CliffordK

  • Naked Science Forum King!
  • ******
  • 6474
  • Activity:
    34.5%
  • Thanked: 33 times
  • Site Moderator
    • View Profile
Re: Borland C++ builder log() bug?
« Reply #6 on: 22/08/2012 19:05:16 »
Quote from: Boogie on 22/08/2012 16:33:54
Yeah, I think you're right. Unless I'm just getting confused, Dynamic C appears to be doing the same thing.

One solution :

#define LOG         log10   //lib is wrong
#define LOG10     log       //lib is wrong

Now when I use LOG(x) it replaces it with log10(x). LOG10(x) is replaced by log(x)

I was a little suprised that this worked. It's a little anal, but it might save me some head scratching if I need to go back and trouble shoot the algorithm sometime in the future.

I would be careful with such a definition.
If someone else is likely to work on your program in the future, it is a sure method to spell disaster.

It would be extremely confusing to define the natural log, Loge(x) to be written as Log10(x).

If ln(x) is not used elsewhere, you might choose to define it as your natural log function.

Then you would merely define log(x) and log10(x) as meaning the same thing.  Or...  you could define log10(x) to throw an error so you could remember what you had done.
« Last Edit: 22/08/2012 19:53:56 by CliffordK »
Logged
 

Offline Boogie (OP)

  • Full Member
  • ***
  • 63
  • Activity:
    0%
    • View Profile
Re: Borland C++ builder log() bug?
« Reply #7 on: 22/08/2012 20:15:38 »
Quote from: CliffordK on 22/08/2012 19:05:16
If ln(x) is not used elsewhere, you might choose to define it as your natural log function.

Yes! That is a much better solution. Good point, thanks!

I knew confusion would get to me sooner or later.
Logged
 

Online CliffordK

  • Naked Science Forum King!
  • ******
  • 6474
  • Activity:
    34.5%
  • Thanked: 33 times
  • Site Moderator
    • View Profile
Re: Borland C++ builder log() bug?
« Reply #8 on: 22/08/2012 20:40:55 »
Ahhh
Are you using caps in your #define functions?

It may be a good mnemonic, but could also lead to a mess with using both upper and lower case log functions.

I think I'd just do a find & replace, and be done with it.  Perhaps remember to double check your work for a bit until you get used to the way natural logs are used in C++.

I must admit, however, that I was one of those people that would often define "dir" and "ls" to mean the same thing.
Logged
 



  • Print
Pages: [1]   Go Up
« previous next »
Tags:
 

Similar topics (1)

Is spin the builder of gravity ?

Started by perceptsBoard Physics, Astronomy & Cosmology

Replies: 9
Views: 4470
Last post 17/07/2014 22:03:48
by evan_au
There was an error while thanking
Thanking...
  • SMF 2.0.15 | SMF © 2017, Simple Machines
    Privacy Policy
    SMFAds for Free Forums
  • Naked Science Forum ©

Page created in 0.129 seconds with 51 queries.

  • Podcasts
  • Articles
  • Get Naked
  • About
  • Contact us
  • Advertise
  • Privacy Policy
  • Subscribe to newsletter
  • We love feedback

Follow us

cambridge_logo_footer.png

©The Naked Scientists® 2000–2017 | The Naked Scientists® and Naked Science® are registered trademarks created by Dr Chris Smith. Information presented on this website is the opinion of the individual contributors and does not reflect the general views of the administrators, editors, moderators, sponsors, Cambridge University or the public at large.