| Author |
Message |
rizalafande
Joined: 04 Jan 2006 Posts: 11
|
01 Aug 2008 10:22 How to use 'LN' and 'EXP' in C language |
|
|
|
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);
}
=====
|
|
| Back to top |
|
 |
FvM
Joined: 22 Jan 2008 Posts: 2679 Helped: 438 Location: Bochum, Germany
|
01 Aug 2008 10:46 How to use 'LN' and 'EXP' in C language |
|
|
|
| The usual function name for natural logarithm is log(). But you have to check if it's implemented with your compiler's math.lib.
|
|
| Back to top |
|
 |
dhenzite
Joined: 18 Apr 2008 Posts: 3
|
01 Aug 2008 17:28 Re: How to use 'LN' and 'EXP' in C language |
|
|
|
/* 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.
|
|
| Back to top |
|
 |
jhbbunch
Joined: 21 Feb 2006 Posts: 220 Helped: 16
|
02 Aug 2008 7:11 How to use 'LN' and 'EXP' in 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.
|
|
| Back to top |
|
 |
FvM
Joined: 22 Jan 2008 Posts: 2679 Helped: 438 Location: Bochum, Germany
|
02 Aug 2008 9:39 Re: How to use 'LN' and 'EXP' in C language |
|
|
|
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.
|
|
| Back to top |
|
 |
jhbbunch
Joined: 21 Feb 2006 Posts: 220 Helped: 16
|
03 Aug 2008 0:48 Re: How to use 'LN' and 'EXP' in C language |
|
|
|
| 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.
|
|
| Back to top |
|
 |
rizalafande
Joined: 04 Jan 2006 Posts: 11
|
04 Aug 2008 14:33 Re: How to use 'LN' and 'EXP' in C language |
|
|
|
| thanks to all the feedback. problems now have been resolved.
|
|
| Back to top |
|
 |
FvM
Joined: 22 Jan 2008 Posts: 2679 Helped: 438 Location: Bochum, Germany
|
04 Aug 2008 15:51 Re: How to use 'LN' and 'EXP' in C language |
|
|
|
| Quote: |
| 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.
|
|
| Back to top |
|
 |