Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-24-2004, 05:08 PM   #1 (permalink)
uman
Registered User
 
uman's Avatar
 
Join Date: Sep 2004
Posts: 1
uman is on a distinguished road
math not quite accurate in C program

I am a newb, just started learning C a few days ago. I wrote a program that calculates factorials and displays them in scientific notation.

Example:
$ ./factorial
3
3 factorial is 6.00000000000000000000000000000000000000000000000e 0

It works fine. But when I try to do a number that requires the scientific notation to be used, it is slightly off.
Example:
$ ./factorial
4
4 factorial is 2.39999999999999991118215802998747676610946655273e 1

Here is the code:
main()
{
int num,e,x;
double work=1.0;
scanf("%d", &num);
for (x=1; x<=num; ++x)
{
work=work*x;
while (work>=10.0)
{
work=work/10.0;
++e;
}
}
printf ("%d factorial is %47.47fe%d\n",num,work,e);
}
Please help me to diagnose what is wrong with my program!
uman is offline   Reply With Quote
Old 09-24-2004, 08:38 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Welcome Uman.
I can't help you in C (others might). But since you just started, why don't you start learning C++ instead?
__________________
Valmont is offline   Reply With Quote
Old 09-27-2004, 02:21 AM   #3 (permalink)
frrossk
Registered User
 
frrossk's Avatar
 
Join Date: Sep 2004
Posts: 6
frrossk is on a distinguished road
Hello Uman

Try this:
Code:
#include <stdio.h>
#include <conio.h>

int main()
{
int num,e,x;
double work=1.0;
scanf("%d", &num);
for (x=1; x<=num; ++x)
{
work=work*x;
/*while (work>=10.0)
{
work=work/10.0;
++e;
}*/
}
printf ("%d factorial is %.3e\n",num,work);
getch ();
return 0;
}
To print a number in scientific format, you can use the "e" format argument.
frrossk is offline   Reply With Quote
Old 09-27-2004, 05:08 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
However, beware that conio.h is not part of the C standard .
__________________
Valmont is offline   Reply With Quote
Old 09-27-2004, 06:19 PM   #5 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
a good c programmer, of course, always checks for error conditions.

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

int main(void)
{
  int i;
  double f = 1.0;
  
  switch(scanf("%d", &i))
  {
  case 0:
  case EOF:
    puts("invalid input");
    return 1;
  }
  if(i < 0)
  {
    puts("undefined");
    return 0;
  }
  for( ; i > 1; i--)
  {
    f *= i;
    if(isinf(f))
    {
      puts("number too large");
      return 0;
    }
  }
  printf("%.3e\n", f);
  return 0;
}
joe_bruin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 07:14 AM
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 10:43 PM
Simple C program Spooky Standard C, C++ 1 10-22-2004 08:26 AM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 06:53 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting