Date: Mon, 7 Feb 2000 22:41:31 -0500 From: "Crist J. Clark" <cjc@cc942873-a.ewndsr1.nj.home.com> To: Fabio Miranda <fmirand@yahoo.com> Cc: freebsd-questions@FreeBSD.ORG Subject: Re: Floating point expection - Core dumped Message-ID: <20000207224131.A59640@cc942873-a.ewndsr1.nj.home.com> In-Reply-To: <20000207234427.14329.qmail@web124.yahoomail.com>; from fmirand@yahoo.com on Mon, Feb 07, 2000 at 03:44:27PM -0800 References: <20000207234427.14329.qmail@web124.yahoomail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Feb 07, 2000 at 03:44:27PM -0800, Fabio Miranda wrote:
> hi, the following ask for x and n values and realize
> this funcion :
>
> P = (3x+2)/n! * E(n,i=1) (x^(n+1-i)) / (n+1-i)!))^2
>
> I compile it $gcc -o e1 e1.c -lm
> I compile and works fine on NetBSD and solaris.
> On FreeBSD compiles good, but when n and x are >= 10,
> it says "Floating point expection - core dumped"
>
> thanks for help, code below:
> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>
> main()
> {
> /*Declaracion de variables*/
> int x, n, a1, i=1, a2=1, temp=1, b1, b2=1, w=1,
> temp1=1, t, y;
> double A, B, P, C, sumt=0;
>
> /* Inicio */
> printf("Estructura de Lenguajes de Programacion\n");
> printf("Laboratorio No. 1\n");
> printf("Desarrollado por: Fabio Andres Miranda\n");
> printf("Compilado en GNU cc sobre FreeBSD
> (Unix)\n\n");
> printf("Use: $ cc -o e1 e1.c -lm\n");
> printf(" $ ./e1\n");
> printf("----------------------------------------\n");
>
> /* Ingreso de variables */
> printf("Por favor, ingrese el valor de x:");
> scanf("%d", &x);
> printf("Por favor, ingrese el valor de n:");
> scanf("%d", &n);
>
> /* Comprobacion de que n es mayor que cero */
> if (n <=0){
> printf("\nError: El valor de n (%d) sera usado en
> funcion factorial y no puede ser menor que cero\n\n",
> n);
> return 0;
> }
>
> /* Desarollo del numerador del primer parentesis */
> a1 = (3 * x) + 2;
>
> /* Factorial de n (n!) */
> i = n;
> while (i != 0) {
> temp = i;
> a2 = a2 * temp;
> i = i - 1;
> }
> /* Primer Cociente */
> A = (double) a1 / a2;
>
> /* Sumatoria */
> for ( w = 1; w <= n ; w++) {
> i = w;
> t = ( n + 1 - i);
>
> /* Numerador */
> b1 = pow(x, t);
^^^^^^^^^^^^^^^^
This is where it dies. You have b1 as an int. Change b1 to a double
and it will work fine. In general, you have a lot of implicit casts
going on here that could make a lot of trouble.
> /* Denominador, factorial de (n + 1 -i )! */
> y = t;
> b2 = 1;
> while(y != 0){
> temp = y;
> b2 = b2 * temp;
> y = y - 1;
> }
>
> /* Numerador / denominador */
> B = (double) b1 / b2;
> sumt = (double) sumt + B;
>
> }
> C = (double) pow(sumt,2);
> P = (double) A * C;
>
> /* Muestra de resultados */
> printf(" \n P = %.4f\n\n ", P);
> return 0;
> }
>
>
> __________________________________________________
> Do You Yahoo!?
> Talk to your friends online with Yahoo! Messenger.
> http://im.yahoo.com
>
>
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-questions" in the body of the message
--
Crist J. Clark cjclark@home.com
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000207224131.A59640>
