Write your question here.
I need help. From the subject from the Faculty of Electrical Engineering I was given the task to calculate a certain physical quantity, however I got transcendental equations and in that case it is necessary to program that equation in some programming language. As I know c ++ best, I tried to program it, but it doesn't work.
c++ authors for some unholy reason made the log function names really weird.
here, log = ln, and log10 is 'log', and lg is __lg. I am sure there is madness in the naming method, but it escapes me.
also note that c++ will do integer division unless one of the things being divided is floating point.
so, consider
while( fabs(10.0/3 - log(d/0.0015)*d*d) > 1e-5) //when its close to zero, you have an approximate answer
{
update d somehow.
}
note that coding it yourself in c++ is the hard way. It may not matter here, you can probably brute force the answer to this one, but in general, if you get nasty equations, you should have access to a math package like maple or mathmatica etc to do these things (does your college have a math-based computer lab?). In c++, you will have to consider numerical methods to avoid roundoff problems as well as convergence issues. Of course, if the goal is to study coding such things yourself, carry on..!
yes, the simple while loop with some hands-on trial and error yield:
d=0.733674 -> 3.33334
Natural log is written as 'log' in various academic/mathematical contexts. __lg is reserved for internal use, hence the double underscore. But yes, you probably need to use numerical methods. Perhaps Newton's method.
As I know c ++ best, I tried to program it, but it doesn't work.
Show us what you tried, and what errors or incorrect results you run into.
dang you guys are industrious. I just said
d = 1
while()
d -= .000001; //ok, yea, I tried += first.
The benefits of having terraflop computers @ home is you can be a little lazy/sloppy.
Ganado, I looked it up, and it appears that 'log' is used for all 3 (bases 2, e and 10) of the most common logs, partly tied to the discipline at hand and who wrote the book. So that one is whatever. But the real complaint is lack of consistency. LOG2, LOG10, LOGE would have been fine names (whatever case). And LG should be commonly available, not just internal, as .. well these computers all seem to work in base 2... but much like the misnamed vector, no one asked me .. they never do :)
c++ authors for some unholy reason made the log function names really weird.
here, log = ln, and log10 is 'log', and lg is __lg. I am sure there is madness in the naming method, but it escapes me.
Hysterical Raisins
The oldest C libraries had log for the natural logarithm. In retrospect it should have been named “ln”, but alas, while Dennis Ritchie studied maths his friend Ken Thompson did not. As you can see, the very first logarithm function added to the C library was by him:
11/3/71 LOG (III)
NAME log -- logarithm base e
SYNOPSIS jsr r5,log
DESCRIPTION The logarithm base e of fr0 is returned in fr0. The floatingpoint simulation should be active in either floating or double
mode, but in single precision integer mode.
FILES kept in /etc/liba.a
SEE_ALSO fptrap
DIAGNOSTICS The error bit (c--bit) is set if the input argument is less thanor equal to zero.
BUGS
OWNER ken
We cannot fault him much for this, as “log” is a perfectly reasonable shortening of “logarithm”, and even today the choice between short forms depends a lot on context.
Further, I think it unlikely that either Ritchie or Thompson cared much. You can easily rewrite any logarithm to another base with a simple division.
So after some 50 years of use it is kind of hard to rewrite the meaning of a function, so what do you do? Be explicit.
log → loge
log10 → log10
log2 → log2
Again, the modern Standard does a good job of naming things explicitly, as “lg” does not necessarily refer to log-base-2: https://mathworld.wolfram.com/Lg.html