Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to use 'LN' and 'EXP' in C language

Status
Not open for further replies.

rizalafande

Junior Member level 2
Joined
Jan 4, 2006
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,435
ln function in c

Can anyone helps me to show how to use 'LN' and 'EXP' mathematical operations in C language? I have tried using below codes but unfortunately doesn't work.
=====
#include <math.h>
.....
double x (double enter)
{
double exit;
exit = ln(enter) + exp(enter);
}
=====
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,738
Helped
14,307
Reputation
28,877
Reaction score
13,034
Trophy points
1,393
Location
Bochum, Germany
Activity points
281,503
how to use ln

The usual function name for natural logarithm is log(). But you have to check if it's implemented with your compiler's math.lib.
 

dhenzite

Newbie level 3
Joined
Apr 18, 2008
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,310
ln in c code

/* Reverse polish notation calculator. */

%{
#define YYSTYPE double
#include <math.h>
%}

%token NUM

%% /* Grammar rules and actions follow */

The C declarations section (see section The C Declarations Section) contains two preprocessor directives.

The #define directive defines the macro YYSTYPE, thus specifying the C data type for semantic values of both tokens and groupings (see section Data Types of Semantic Values). The Bison parser will use whatever type YYSTYPE is defined as; if you don't define it, int is the default. Because we specify double, each token and each expression has an associated value, which is a floating point number.

The #include directive is used to declare the exponentiation function pow.

The second section, Bison declarations, provides information to Bison about the token types (see section The Bison Declarations Section). Each terminal symbol that is not a single-character literal must be declared here. (Single-character literals normally don't need to be declared.) In this example, all the arithmetic operators are designated by single-character literals, so the only terminal symbol that needs to be declared is NUM, the token type for numeric constants.
 

jhbbunch

Full Member level 4
Joined
Feb 21, 2006
Messages
199
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
exp c language

Don't use names like enter. It looks like a programming word and is confusing.

Secondly, exit looks like a programming word also, and it is, it is a reserved word. You can't use it as a variable. You must use return(log(enter) + exp(enter));.

Lastly math.h uses log for the natural logarithm and log10 for base 10 logs.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,738
Helped
14,307
Reputation
28,877
Reaction score
13,034
Trophy points
1,393
Location
Bochum, Germany
Activity points
281,503
c ln function

I agree, that misleading variable names should be avoided, but actually none of the said variable names is a reserved word in standard C. So there's no error to correct, just a matter of taste.

Apart from using a wrong function name for log(), as I already mentioned, the function returns no value.
 

jhbbunch

Full Member level 4
Joined
Feb 21, 2006
Messages
199
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
c language ln

Good catch, you're right, exit() is not a reserved word but it is a function defined in stdlib.h, and is a very commonly called function. I used to automatically include stdlib.h in anything I wrote, so exit was about as good as a reserved word.
 

rizalafande

Junior Member level 2
Joined
Jan 4, 2006
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,435
ln in c language

thanks to all the feedback. problems now have been resolved.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,738
Helped
14,307
Reputation
28,877
Reaction score
13,034
Trophy points
1,393
Location
Bochum, Germany
Activity points
281,503
ln function c

I used to automatically include stdlib.h in anything I wrote, so exit was about as good as a reserved word.

As I already said, I wouldn't use it as a variable name, because it's a source of confusion. But even if you include stdlib.h, the variable definition overloads the function prototype and thus can be used without any problem.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top