source: git/Singular/gnumpfl.cc @ 97a7b44

fieker-DuValspielwiese
Last change on this file since 97a7b44 was 0ae5116, checked in by Hans Schönemann <hannes@…>, 25 years ago
*hannes: introduced complex=(real,<len>,<par>) git-svn-id: file:///usr/local/Singular/svn/trunk@3035 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: gnumpfl.cc,v 1.3 1999-05-10 15:10:49 Singular Exp $ */
5/*
6* ABSTRACT: computations with GMP floating-point numbers
7*
8* ngf == number gnu floats
9*/
10
11#include "mod2.h"
12#include "tok.h"
13#include "febase.h"
14#include "mmemory.h"
15#include "numbers.h"
16#include "modulop.h"
17#include "longrat.h"
18
19#include "gnumpfl.h"
20#include "mpr_complex.h"
21
22extern size_t gmp_output_digits;
23
24static int ngfPrimeM;
25static number ngfMapP(number from)
26{
27  number to;
28  int save=npPrimeM;
29  npPrimeM=ngfPrimeM;
30  to = ngfInit(npInt(from));
31  npPrimeM=save;
32  return to;
33}
34static number ngfMapQ(number from)
35{
36  gmp_float *res= new gmp_float();
37  *res= numberToFloat(from);
38  return (number)res;
39}
40
41BOOLEAN ngfSetMap(int c, char ** par, int nop, number minpol)
42{
43  if (c == 0)
44  {                      /* Q -> R      */
45    nMap = ngfMapQ;
46    return TRUE;
47  }
48  if (c>1)
49  {
50    if (par==NULL)
51    {                    /* Z/p -> R    */
52      nMap = ngfMapP;
53      return TRUE;
54    }
55    else
56    {                    /* GF(q) -> R  */
57      return FALSE;
58    }
59  }
60  else if (c<0)
61     return FALSE;       /* Z/p(a) -> R */
62  else if (c==1)
63     return FALSE;       /* Q(a) -> R   */
64  return FALSE;
65}
66
67void ngfNew (number * r)
68{
69  *r= NULL;
70}
71
72/*2
73* n := i
74*/
75number ngfInit (int i)
76{
77  gmp_float* n= NULL;
78  if ( i != 0 )
79  {
80    n= new gmp_float( i );
81  }
82  return (number)n;
83}
84
85/*2
86* convert number to int
87*/
88int ngfInt(number &i)
89{
90  if ( i == NULL ) return 0;
91  return (int)*(gmp_float*)i;
92}
93
94/*2
95* delete a
96*/
97#ifdef LDEBUG
98void ngfDBDelete (number * a,char *f, int l)
99#else
100void ngfDelete (number * a)
101#endif
102{
103  if ( *a != NULL )
104  {
105    delete *(gmp_float**)a;
106    *a=NULL;
107  }
108}
109
110/*2
111* copy a to b
112*/
113number ngfCopy(number a)
114{
115  gmp_float* b= NULL;
116  if ( a !=  NULL )
117  {
118    b= new gmp_float( *(gmp_float*)a );
119  }
120  return (number)b;
121}
122
123/*2
124* za:= - za
125*/
126number ngfNeg (number a)
127{
128  if ( a == NULL ) return NULL;
129  *(gmp_float*)a= -(*(gmp_float*)a);
130  return (number)a;
131}
132
133/*
134* 1/a
135*/
136number ngfInvers(number a)
137{
138  gmp_float* r= NULL;
139  if ( (a==NULL) /*|| ((gmp_float*)a)->isZero()*/ )
140  {
141    WerrorS("div. 1/0");
142  }
143  else
144  {
145    r= new gmp_float( (gmp_float)1 / (*(gmp_float*)a) );
146  }
147  return (number)r;
148}
149
150/*2
151* u:= a + b
152*/
153number ngfAdd (number a, number b)
154{
155  gmp_float* r= NULL;
156  if ( a==NULL && b==NULL )
157  {
158    return NULL;
159  }
160  else if ( a == NULL )
161  {
162    r= new gmp_float( *(gmp_float*)b );
163  }
164  else if ( b == NULL )
165  {
166    r= new gmp_float( *(gmp_float*)a );
167  }
168  else
169  {
170    r= new gmp_float( (*(gmp_float*)a) + (*(gmp_float*)b) );
171  }
172  return (number)r;
173}
174
175/*2
176* u:= a - b
177*/
178number ngfSub (number a, number b)
179{
180  gmp_float* r= NULL;
181  if ( a==NULL && b==NULL )
182  {
183    return NULL;
184  }
185  else if ( a == NULL )
186  {
187    r= new gmp_float( -(*(gmp_float*)b) );
188  }
189  else if ( b == NULL )
190  {
191    r= new gmp_float( *(gmp_float*)a );
192  }
193  else
194  {
195    r= new gmp_float( (*(gmp_float*)a) - (*(gmp_float*)b) );
196  }
197  return (number)r;
198}
199
200/*2
201* u := a * b
202*/
203number ngfMult (number a, number b)
204{
205  gmp_float* r= NULL;
206  if ( a==NULL || b==NULL )
207  {
208    return NULL;
209  }
210  else
211  {
212    r= new gmp_float( (*(gmp_float*)a) * (*(gmp_float*)b) );
213  }
214  return (number)r;
215}
216
217/*2
218* u := a / b
219*/
220number ngfDiv (number a, number b)
221{
222  if ( b==NULL /*|| ((gmp_float*)b)->isZero()*/ )
223  {
224    // a/0 = error
225    WerrorS("div. 1/0");
226    return NULL;
227  }
228  else if ( a==NULL )
229  {
230    // 0/b = 0
231    return NULL;
232  }
233  gmp_float* r= new gmp_float( (*(gmp_float*)a) / (*(gmp_float*)b) );
234  return (number)r;
235}
236
237/*2
238* u:= x ^ exp
239*/
240void ngfPower ( number x, int exp, number * u )
241{
242  if ( exp == 0 )
243  {
244    *(gmp_float*)u= 1.0;
245    return;
246  }
247  if ( exp == 1 )
248  {
249    if ( x == NULL )
250    {
251      *(gmp_float*)u= 0.0;
252    }
253    else
254    {
255      *(gmp_float*)u= *(gmp_float*)x;
256    }
257    return;
258  }
259  ngfPower(x,exp-1,u);
260  *(gmp_float*)u*= *(gmp_float*)x;
261}
262
263BOOLEAN ngfIsZero (number a)
264{
265  if ( a == NULL ) return TRUE;
266  return ( ((gmp_float*)a)->isZero() );
267}
268
269/*2
270* za >= 0 ?
271*/
272BOOLEAN ngfGreaterZero (number a)
273{
274  if ( a == NULL ) return TRUE;
275  return ( (*(gmp_float*)a) >= (gmp_float)0.0 );
276}
277
278/*2
279* a > b ?
280*/
281BOOLEAN ngfGreater (number a, number b)
282{
283  if ( a==NULL )
284  {
285    return (((gmp_float*)b)->sign() < 0);
286  }
287  if ( b==NULL )
288  {
289    return (((gmp_float*)a)->sign() < 0);
290  }
291  return ( (*(gmp_float*)a) > (*(gmp_float*)b) );
292}
293
294/*2
295* a = b ?
296*/
297BOOLEAN ngfEqual (number a, number b)
298{
299  if ( a == NULL && b == NULL )
300  {
301    return TRUE;
302  }
303  else if ( a == NULL || b == NULL )
304  {
305    return FALSE;
306  }
307  return ( (*(gmp_float*)a) == (*(gmp_float*)b) );
308}
309
310/*2
311* a == 1 ?
312*/
313BOOLEAN ngfIsOne (number a)
314{
315  if ( a == NULL ) return FALSE;
316  return ((gmp_float*)a)->isOne();
317}
318
319/*2
320* a == -1 ?
321*/
322BOOLEAN ngfIsMOne (number a)
323{
324  if ( a == NULL ) return FALSE;
325  return ((gmp_float*)a)->isMOne();
326}
327
328/*2
329* result =gcd(a,b)
330* dummy, returns 1
331*/
332number ngfGcd(number a, number b)
333{
334  gmp_float *result= new gmp_float( 1 );
335  return (number)result;
336}
337
338char * ngfEatFloatNExp( char * s )
339{
340  char *start= s;
341
342  // eat floats (mantissa) like:
343  //   0.394394993, 102.203003008,  .300303032
344  while ((*s >= '0' && *s <= '9')||(*s == '.'))
345  {
346    s++;
347  }
348  // eat the exponent, starts with 'e' followed by '+', '-'
349  // or no sign and digits, like:
350  //  e1322, e-202, e+393
351  if ( (s != start) && (*s == 'e') )
352  {
353    s++;
354    if ( (*s=='-') || (*s=='+') ) s++;
355    while ((*s >= '0' && *s <= '9'))
356    {
357      s++;
358    }
359  }
360
361  return s;
362}
363
364/*2
365* extracts the number a from s, returns the rest
366*/
367char * ngfRead (char * s, number * a)
368{
369  char *start= s;
370
371  s= ngfEatFloatNExp( s );
372
373  if (*s=='\0')
374  {
375    if ( *(gmp_float**)a == NULL ) (*(gmp_float**)a)= new gmp_float();
376    (*(gmp_float**)a)->setFromStr(start);
377  }
378  else if (s==start)
379  {
380    if ( *(gmp_float**)a != NULL )  delete (*(gmp_float**)a);
381    (*(gmp_float**)a)= new gmp_float(1);
382  }
383  else
384  {
385    char c=*s;
386    *s='\0';
387    if ( *(gmp_float**)a == NULL ) (*(gmp_float**)a)= new gmp_float();
388    (*(gmp_float**)a)->setFromStr(start);
389    *s=c;
390  }
391  return s;
392}
393
394/*2
395* write a floating point number
396*/
397void ngfWrite (number &a)
398{
399  char *out;
400//Print("ngfWrite %d\n",gmp_output_digits);
401  out= floatToStr(*(gmp_float*)a,gmp_output_digits);
402  StringAppend(out);
403
404  Free((ADDRESS)out, (strlen(out)+1)* sizeof(char) );
405}
406
407#ifdef LDEBUG
408BOOLEAN ngfDBTest(number a, char *f, int l)
409{
410  return TRUE;
411}
412#endif
413
414// local Variables: ***
415// folded-file: t ***
416// compile-command: "make installg" ***
417// End: ***
Note: See TracBrowser for help on using the repository browser.