LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C (math.h)not doing right math? exp() issue. (https://www.linuxquestions.org/questions/programming-9/c-math-h-not-doing-right-math-exp-issue-729341/)

knockout_artist 05-29-2009 10:45 AM

C (math.h)not doing right math? exp() issue.
 
Hi,

Look at the following please.

Code:

#include<stdio.h>
#include<math.h>

main()
  {
int i;
double x;

x=exp(60);     

printf("%d ===== %f \n",  x);  //trying to print as double and as float point.

  }

That's what I got.
Code:

[champ@alligator dsp]$ ./signal
-1648018330 ===== 0.000000

Now If I do same thing on octave I get

Code:

octave:12> exp(61)
ans =  3.1043e+26

I wrote code for sin() with that I had same values in octave/C.

Any ideas?

Thanks

Sergei Steshenko 05-29-2009 10:55 AM

Quote:

Originally Posted by knockout_artist (Post 3556668)
Hi,

Look at the following please.

Code:

#include<stdio.h>
#include<math.h>

main()
  {
int i;
double x;

x=exp(60);     

printf("%d ===== %f \n",  x);  //trying to print as double and as float point.

  }

That's what I got.
Code:

[champ@alligator dsp]$ ./signal
-1648018330 ===== 0.000000

Now If I do same thing on octave I get

Code:

octave:12> exp(61)
ans =  3.1043e+26

I wrote code for sin() with that I had same values in octave/C.

Any ideas?

Thanks


Sure - compile with -Wall and make sure you have no warnings - you should have warnings until you fix your 'printf' statement.

And, to be more precise - never compile without -Wall, better yet compile also with -Wextra.

knockout_artist 05-29-2009 11:20 AM

I tried
Code:


#include<stdio.h>
#include<math.h>

main()
{

double x;
double y;

x=exp(60);    //exp
y=sin(60);    ////sin

printf("%f ===== %f \n",  x,y);

}

I compiled with -Wall
Code:


[champ@alligator dsp]$ gcc -o signal signal.c  -Wall
signal.c:5: warning: return type defaults to ‘int’
signal.c: In function ‘main’:
signal.c:15: warning: control reaches end of non-void function
[champ@alligator dsp]$ ./signal

114200738981568423454048256.000000 ===== -0.304811
[champ@alligator dsp]$

sin() prints right value
Code:

octave:14> sin(60)
ans = -0.30481
octave:15> exp(60)
ans =  1.1420e+26
octave:16>

Thanks.

raconteur 05-29-2009 11:22 AM

Though I do agree with Sergei, I might have pointed out the specific error in the arguments and format specifier for printf.

Try this:
Code:

#include <stdio.h>
#include <math.h>

int main()
{
  double x;

  x=exp(60);

  printf("%e ===== %f \n", x, x);  //trying to print as double and as float point.

  return 0;
}

Saved as exptest.c and compiled with:
Code:

gcc -Wall -lm -o exptest exptest.c
Output:
Code:

./exptest
1.142007e+26 ===== 114200738981568423454048256.000000


knockout_artist 05-29-2009 11:27 AM

Awesome!!!!

Thank you so much!1

Sergei Steshenko 05-29-2009 11:46 AM

Quote:

Originally Posted by raconteur (Post 3556709)
Though I do agree with Sergei, I might have pointed out the specific error in the arguments and format specifier for printf.
...

Well, it's the case that I believe in "no pain, no gain". I.e. the person who had the problem should apply some effort to correct the mistake and to realize that the effort wasn't in vain.

From my very long career I know that achieving the state when developers around finally accept that they can make and do make mistakes, and that's why compilers and other tools that report developers' mistakes (especially the subtle ones) should be used is often much more difficult than solving technical problems.

So, my goal was to show the OP that compiler is smart and that the smartness should be utilized.

I always say that if compiler issues a warning, either the compiler or I am not right, but typically it's me who isn't right.

On the same note - however much I love Perl, I hate even more Perl users who don't use

Code:

use strict;
use warnings;

in their code.

sarbjeet2612 11-25-2011 11:24 AM

thanks.
 
Thanks.
Please Tell me how can i use this function using scanf function.

Sergei Steshenko 11-25-2011 02:13 PM

Quote:

Originally Posted by sarbjeet2612 (Post 4533789)
Thanks.
Please Tell me how can i use this function using scanf function.

Which function ? 'exp' ?


All times are GMT -5. The time now is 06:58 PM.