summaryrefslogtreecommitdiff
path: root/libguile/num2float.i.c
blob: b393ba9b75d69e1a5fa93bd40fa1cae5a8c43d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* this file is #include'd (several times) by numbers.c */

FTYPE
NUM2FLOAT (SCM num, unsigned long int pos, const char *s_caller)
{
  if (SCM_INUMP (num))
    return SCM_INUM (num);
  else if (SCM_BIGP (num))
    { /* bignum */
    
      FTYPE res = 0.0;
      size_t l;

      for (l = SCM_NUMDIGS (num); l--;)
	res = SCM_BIGRAD * res + SCM_BDIGITS (num)[l];
      
      if (SCM_BIGSIGN (num))
	res = -res;
      
      if (isfinite (res))
	return res;
      else
	scm_out_of_range (s_caller, num);
    }
  else if (SCM_REALP (num))
    return SCM_REAL_VALUE (num);
  else
    scm_wrong_type_arg (s_caller, pos, num);
}

SCM
FLOAT2NUM (FTYPE n)
{
  SCM z;
  z = scm_double_cell (scm_tc16_real, 0, 0, 0);
  SCM_REAL_VALUE (z) = n;
  return z;
}

/* clean up */
#undef FLOAT2NUM
#undef NUM2FLOAT
#undef FTYPE

/*
  Local Variables:
  c-file-style: "gnu"
  End:
*/