LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2009, 10:45 AM   #1
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
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
 
Old 05-29-2009, 10:55 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by knockout_artist View Post
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.
 
Old 05-29-2009, 11:20 AM   #3
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
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.
 
Old 05-29-2009, 11:22 AM   #4
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
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
 
Old 05-29-2009, 11:27 AM   #5
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
Awesome!!!!

Thank you so much!1
 
Old 05-29-2009, 11:46 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by raconteur View Post
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.
 
1 members found this post helpful.
Old 11-25-2011, 11:24 AM   #7
sarbjeet2612
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
thanks.

Thanks.
Please Tell me how can i use this function using scanf function.
 
Old 11-25-2011, 02:13 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by sarbjeet2612 View Post
Thanks.
Please Tell me how can i use this function using scanf function.
Which function ? 'exp' ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
math.h Damicles Programming 9 12-04-2010 05:51 AM
Running a 2.6.* kernel with math emulation ( Does the math emulation work ?) dar_beh_dar Linux - Kernel 3 05-20-2009 11:43 PM
math program that I can enter math functions ... Four General 5 04-19-2006 08:02 PM
math and QStings -- help!!! CrazyPilot Programming 0 07-08-2004 04:53 AM
do i need math-emu? FirebirdV0273 Linux - General 2 10-27-2003 02:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration