Im adding a specialised formula to a calculator, and it uses the Ln/ln function which is often on scientific calculators, how do i get this function to work in c++?
The formula is as follows:
'formula: pl * (10*diff + 10*creat +2*lines)
------------------------------------------
150 * (Ln(pl/1.5))'
Can anyone help?

Dani AI

Generated

Two distinct issues were interacting here: a naming collision and a grouping/precedence bug. correctly spotted that declaring a variable named log hides the math function and leads to the compiler error about a “term not evaluating to a function.” was also right to call attention to how the fraction was written — the way the division and multiplication were grouped changed the math. fixed the parentheses in the x expression, which resolved the incorrect results.

Why that happened: C++ resolves an identifier to the closest declaration, so a local log variable will shadow the log function. Renaming that variable (for example to lnArg, logValue, or similar) or explicitly calling std::log(...) fixes the compile error. Also remember that std::log computes the natural logarithm (ln); std::log10 is base‑10.

A note on operator precedence: * and / have the same precedence and are evaluated left-to-right. That means a/150*log(...) is parsed as (a/150) * log(...), not a / (150*log(...)). If the formula intends the latter, put parentheses around the denominator. Wrong grouping can produce negative or tiny values fed into log, which then gives NaN or garbage.

Practical checklist to avoid this in future:

  • Rename any variable that conflicts with standard names (no log variable).
  • Break the expression into named intermediate values (numerator, denominator, addition) for clarity and easier debugging.
  • Use double for math unless you have a reason for float.
  • Validate inputs before calling log (argument must be > 0).
  • Print or inspect intermediate values when results don’t match expectations.

Following those steps makes the code clearer and stops the two kinds of errors that tripped this example.

Recommended Answers

All 18 Replies

#include <cmath>

Use the 'log' function (which computes the natural logarithm).

When i have that in it gives an error saying that the log hasnt been initialised, maybe youll understand it better if you look at the code:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
        char name [100];
        char quit;
        quit ='\0';
        while (quit !='q')
        {
                system("title AnimeDD Power Level Calculator");
                system("color 84");
                float creat, diff, lines, pl, log, x, y, z; //loads integers into memory
                cout <<"This is a simple power level calculator"<< endl;
                cout<<"What is your characters name?"<< endl;
                cin.getline (name,100);
                cout<<"Hello, " << name << "."<<  endl;
				cout<<"Put in your current pl"<<endl;
				cin>> pl;
                cout<<"Put in your difficulty points:"<< endl; //asks for diff
                cin>> diff; //Receives difficulty and loads it into memory
                cout<<"Put in your creativity points:"<< endl;
                cin>> creat;
                cout<<"Put in your line count:"<< endl;
                cin>> lines;
                x = pl*(10*diff)+(10*creat)+(2*lines);
                y = x/150*(log*(pl/1.5)); 
				z = pl+y;
                cout<<"Your Power level is " << z << ".\a" << endl;
                cout<<""<< endl;
                cout<<"Q - Quit"<< endl;
                cout<<"R - Repeat"<< endl;
                cout<<"Please type in the corresponding letter for your choice then press enter."<< endl;
                cin>> quit;
                cout<<"Goodbye, " << name << "."<< endl;
                system("PAUSE");
                system("cls");
        }

        return 0;
}

Surely you meant

x/(150*log(pl/1.5));

I removed the multiplication sign after 'log' (which made it treated like a variable instead of a function name).

Also, as you wrote it, log(pl/1.5) would have been on the top of the fraction, so I added parentheses to put it on the bottom. (Which by your original post's ASCII art I assumed you wanted.)

Hmmm, now it says 'error C2064: term does not evaluate to a function' =/

Try x/(150*log((double)(pl/1.5))); since log takes a double as its argument.

Hmmm it still gives the same error =/ how can it be made to evaluate?

the probleme is that you have declared log as a float. and a float is not a function and a float variable cant be used as a function.
so ether change (150*log(pl/1.5)); to (150*std::log(pl/1.5));
or remove the log float.

Awesome its building now, thanks alot =D

Bah namespaces. Use std::log instead of log.

Thanks for the help guys, but i need to find the natural log, that 'log' function is the common log, how can i get the natural log, ln, in?

log(x) is the natural logarithm of x (base e)
log10(x) is the common logarithm of x (base 10)

Yah, it's time to get used to the idea that the natural log is called both "ln" and "log", and that the common logarithm is called "log" and "log10". For extra fun, find a teacher that writes "ln" while speaking "log"!

So i use 'log10' instead of log? Is 'log10' the Ln?

I have an idea. Try testing the functions.

What functions? =/
I thought log10 was right but its not, there must be another way to include the natural logarithm >_<

log(x) ; returns the natural log of x, accept it.
Are you sure that you are thinking of log?
can you give us a couple of examples of wath you want to be done?
Guessing wath you want is hard.

natural log:
log(3) = 1,0986122886681096913952452369225
log(10) = 2,3025850929940456840179914546844
log(20) = 2,9957322735539909934352235761425
log(100 = 4,6051701859880913680359829093687

common log:
log10(3) = 0,47712125471966243729502790325512
log10(10) = 1
log10(20) = 1,3010299956639811952137388947245
log10(100) = 2

I have taken a closer look on your code, and think I see wher your error is.
y = x/150*(log(pl/1.5));
and
y = x/(150*(log(pl/1.5)));
are 2 completly diferent calculation. this might be your error.
and the last is the corect one, at last from your formula.

I found out why, the log was correct all along lol, the problem was in the 'x' part of the formula, i made a parenthetical error on the first line, this was my line:
x = pl*(10*diff)+(10*creat)+(2*lines);
and this is the correct version:
x = pl*((10*diff)+(10*creat)+(2*lines));
which explains why the log wasnt functioning correctly, thanks alot for your help guys ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.