[erlang-questions] bignum sqrt broken in erlang?

Angel Alvarez clist@REDACTED
Wed Aug 26 00:39:40 CEST 2009


Thanks

Ive written a minimal port program to do full bignums square roots
from the complex port example from erlang docs.

Surely its a lot slower than a bif but, better than nothing...

for the record the c program does:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "gmp.h"

int main (int argc, char *argv[])
{
  char  buf[500];
  int   len;

  mpz_t sq_me, sq_out, test;
  mpz_init(sq_me);
  mpz_init(sq_out);

  while (fgets(buf, 498, stdin) != NULL)
  {
    mpz_set_str(sq_me, buf, 10);
    mpz_sqrt(sq_out, sq_me);

  gmp_printf ("%Zd\n", sq_out);
  fflush(stdout);
  }
  return 0;
}

Regards, Angel


El Miércoles, 26 de Agosto de 2009 Richard O'Keefe escribió:
> 
> On Aug 26, 2009, at 1:11 AM, Angel Alvarez wrote:
> > 2> R0=trunc(math:sqrt(N)).
> > 267659337146589062070609641472
> 
> Right there is your problem.
> math:sqrt/1 is a FLOATING-POINT square root.
> The argument is first rounded to floating point,
> then its square root taken, and then you truncate that.
> 
> As far as I know, Erlang doesn't have a built-in
> integer square root function, but it's straightforward
> enough to write one.
> 
> 



-- 
No imprima este correo si no es necesario. El medio ambiente está en nuestras manos.
->>-----------------------------------------------
    Clist UAH a.k.a Angel
---------------------------------[www.uah.es]-<<--

Sex is a battle, love is war.


More information about the erlang-questions mailing list