Skip to content

Commit

Permalink
Merge exponential as our subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarinero committed Dec 27, 2010
1 parent 310a2c6 commit 0bce879
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions exponential/cexpo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>
#include <math.h>

main ()
{
double x;

scanf("%lf",&x);
x = pow(10,x);
printf("%f\n",x);
}
43 changes: 43 additions & 0 deletions exponential/expo.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
;calcula 10^x
;Recibe un parametro con el número x (double)
;Retorna 10^x en st0 (double)
;si hay overflow o underflow retorna -1

%define x ebp + 8

segment .text
global expo

expo:
enter 8,0 ;No se necesita guardar los registros

finit
fstcw [ebp-4]
or word [ebp-4], 0x0C00 ;redondear hacia 0
fldcw [ebp-4]

fld qword [x]
fldl2t ;st0 = log2(10)
fmul st1 ;st0 = log2(resultado)
fld st0 ;st1 = st0
frndint
fsubr st1 ;st0 = expo-decimal
f2xm1 ;st0 = 2^expo-decimal - 1
fld1
faddp st1 ;st0 = 2^p expo-decimal
fscale ;st0 = 2^expo-decimal * 2^st1
;fscale usa st1 redondeando hacia 0 que es expo-entero
fst qword [ebp-8] ;comprobar que se puede representar en un double
;es suficiente porque si fscale hubiera causado
;overflow o underflow hubiera almacenado
;un numero no representable en un double
fstsw [ebp-4]
and word [ebp-4], 0x18 ;dejar solo overflow-underflow bits
cmp word [ebp-4], 0
je salir
mov word [ebp-4], -1
fild word [ebp-4]

salir:
leave
ret
21 changes: 21 additions & 0 deletions exponential/pract8.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>

double expo(double) __attribute__((cdecl));

main ()
{
double x, y;

printf("Introduzca un numero en punto flotante\n");
if (scanf("%lf",&x) != 1) {
fprintf(stderr, "No se ha leido correctamente el numero\n");
exit(-1);
}
y = expo(x);
if (y == -1) {
fprintf(stderr, "Se ha producido overflow o underflow\n");
exit(-1);
}
printf("10^%.6g = %.15g\n", x, y);
}

0 comments on commit 0bce879

Please sign in to comment.