File: float.c

package info (click to toggle)
gap 4r4p12-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 29,584 kB
  • ctags: 7,113
  • sloc: ansic: 98,786; sh: 3,299; perl: 2,263; makefile: 498; asm: 63; awk: 6
file content (581 lines) | stat: -rw-r--r-- 14,850 bytes parent folder | download | duplicates (3)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/****************************************************************************
**
*W  float.c                      GAP source                      Steve Linton
**
*H  @(#)$Id: float.c,v 4.12.6.3 2006/09/19 20:09:28 sal Exp $
**
*Y  Copyright (C)  1996,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
*Y  (C) 1998 School Math and Comp. Sci., University of St.  Andrews, Scotland
*Y  Copyright (C) 2002 The GAP Group
**
**  This file contains the functions for the float package.
**
** floats are stored as bags containing a 64 bit value
*/
#include        "system.h"              /* system dependent part           */

const char * Revision_float_c =
   "@(#)$Id: float.c,v 4.12.6.3 2006/09/19 20:09:28 sal Exp $";

#include        "gasman.h"              /* garbage collector               */
#include        "objects.h"             /* objects                         */

#include        "gap.h"                 /* error handling, initialisation  */


#include        "ariths.h"              /* basic arithmetic                */

#define INCLUDE_DECLARATION_PART
#include        "float.h"                /* floateans                        */
#undef  INCLUDE_DECLARATION_PART

#include        "bool.h"
#include        "scanner.h"
#include        "string.h"

/* the following two declarations would belong in `saveload.h', but then all
 * files get float dependencies */
extern Double LoadDouble( void);
extern void SaveDouble( Double d);

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#ifdef HAVE_MATH_H
#include <math.h>
#endif

#include <stdlib.h>

#define VAL_FLOAT(obj) (*(Double *)ADDR_OBJ(obj))
#define SET_VAL_FLOAT(obj, val) (*(Double *)ADDR_OBJ(obj) = val)
#define IS_FLOAT(obj) (TNUM_OBJ(obj) == T_FLOAT)
#define SIZE_FLOAT   sizeof(Double)

/****************************************************************************
**
*F  TypeFloat( <float> )  . . . . . . . . . . . . . . . kind of a float value
**
**  'TypeFloat' returns the kind of floatean values.
**
**  'TypeFloat' is the function in 'TypeObjFuncs' for floatean values.
*/
Obj TYPE_FLOAT;
Obj TYPE_FLOAT0;

Obj TypeFloat (
    Obj                 val )
{
  
    return VAL_FLOAT(val) == 0.0L ? TYPE_FLOAT0 : TYPE_FLOAT;
}


/****************************************************************************
**
*F  PrintFloat( <float> ) . . . . . . . . . . . . . . . . print a float value
**
**  'PrintFloat' prints the floating value <float>.
*/
#if SYS_MAC_MWC
#include <fp.h>
void PrintFloat (
    Obj                 x )
{
  Char buf[40];
  decimal dec;
  decform decf;
  	
  decf.style  = FLOATDECIMAL;
  decf.digits = 32;
  num2dec (&decf, VAL_FLOAT(x), &dec);
  dec2str (&decf, &dec, buf);
  Pr("%s",(Int)buf, 0);
}

#else

void PrintFloat (
    Obj                 x )
{
  Char buf[32];
  sprintf(buf, "%g",VAL_FLOAT(x));
  Pr("%s",(Int)buf, 0);
}
#endif



/****************************************************************************
**
*F  EqFloat( <floatL>, <floatR> )  . . . . . . . . .  test if <floatL> =  <floatR>
**
**  'EqFloat' returns 'True' if the two floatean values <floatL> and <floatR> are
**  equal, and 'False' otherwise.
*/
Int EqFloat (
    Obj                 floatL,
    Obj                 floatR )
{
  return VAL_FLOAT(floatL) == VAL_FLOAT(floatR);
}


/****************************************************************************
**
*F  LtFloat( <floatL>, <floatR> )  . . . . . . . . .  test if <floatL> <  <floatR>
**
*/
Int LtFloat (
    Obj                 floatL,
    Obj                 floatR )
{
  return VAL_FLOAT(floatL) < VAL_FLOAT(floatR);
}


/****************************************************************************
**
*F  IsFloatFilt( <self>, <obj> ) . . . . . . . . . .  test for a floatean value
**
**  'IsFloatFilt' implements the internal filter 'IsFloat'.
**
**  'IsFloat( <obj> )'
**
**  'IsFloat'  returns  'true'  if  <obj>  is   a floatean  value  and  'false'
**  otherwise.
*/
Obj IsFloatFilt;

Obj IsFloatHandler (
    Obj                 self,
    Obj                 obj )
{
  return IS_FLOAT(obj) ? True : False;
}



/****************************************************************************
**
*F  SaveFloat( <float> ) . . . . . . . . . . . . . . . . . . . . save a Floatean 
**
*/

void SaveFloat( Obj obj )
{
  SaveDouble(VAL_FLOAT(obj));
  return;
}

/****************************************************************************
**
*F  LoadFloat( <float> ) . . . . . . . . . . . . . . . . . . . . save a Floatean 
**
*/

void LoadFloat( Obj obj )
{
  SET_VAL_FLOAT(obj, LoadDouble());
}

static inline Obj NEW_FLOAT( Double val )
{
  Obj f;
  f = NewBag(T_FLOAT,SIZE_FLOAT);
  SET_VAL_FLOAT(f,val);
  return f;
}

/****************************************************************************
**
*F  ZeroFloat(<float> ) . . . . . . . . . . . . . . . . . . . return the zero 
**
*/


Obj ZeroFloat( Obj f )
{
  return NEW_FLOAT((Double)0.0);
}

/****************************************************************************
**
*F  AinvFloat(<float> ) . . . . . . . . . . . . . . . . . . . unary minus 
**
*/


Obj AInvFloat( Obj f )
{
  return NEW_FLOAT(-VAL_FLOAT(f));
}

/****************************************************************************
**
*F  OneFloat(<float> ) . . . . . . . . . . . . . . . . . . . return the one 
**
*/


Obj OneFloat( Obj f )
{
  return NEW_FLOAT((Double)1.0);
}

/****************************************************************************
**
*F  InvFloat(<float> ) . . . . . . . . . . . . . . . . . . . reciprocal
**
*/


Obj InvFloat( Obj f )
{
  return NEW_FLOAT((Double)1.0/VAL_FLOAT(f));
}

/****************************************************************************
**
*F  ProdFloat(<floatl>, <floatr> ) . . . . . . . . . . . . . . . product
**
*/


Obj ProdFloat( Obj fl, Obj fr )
{
  return NEW_FLOAT(VAL_FLOAT(fl)*VAL_FLOAT(fr));
}

/****************************************************************************
**
*F  PowFloat(<floatl>, <floatr> ) . . . . . . . . . . . . . . exponentiation
**
*/


Obj PowFloat( Obj fl, Obj fr )
{
  return NEW_FLOAT(pow(VAL_FLOAT(fl),VAL_FLOAT(fr)));
}

/****************************************************************************
**
*F  SumFloat(<floatl>, <floatr> ) . . . . . . . . . . . . . .  sum
**
*/


Obj SumFloat( Obj fl, Obj fr )
{
  return NEW_FLOAT(VAL_FLOAT(fl)+VAL_FLOAT(fr));
}

/****************************************************************************
**
*F  DiffFloat(<floatl>, <floatr> ) . . . . . . . . . . . . . . difference
**
*/


Obj DiffFloat( Obj fl, Obj fr )
{
  return NEW_FLOAT(VAL_FLOAT(fl)-VAL_FLOAT(fr));
}

/****************************************************************************
**
*F  QuoFloat(<floatl>, <floatr> ) . . . . . . . . . . . . . . quotient
**
*/


Obj QuoFloat( Obj fl, Obj fr )
{
  return NEW_FLOAT(VAL_FLOAT(fl)/VAL_FLOAT(fr));
}

/****************************************************************************
**
*F  LQuoFloat(<floatl>, <floatr> ) . . . . . . . . . . . . . .left quotient
**
*/


Obj LQuoFloat( Obj fl, Obj fr )
{
  return NEW_FLOAT(VAL_FLOAT(fr)/VAL_FLOAT(fl));
}

/****************************************************************************
**
*F  ModFloat(<floatl>, <floatr> ) . . . . . . . . . . . . . . .mod
**
*/


Obj ModFloat( Obj fl, Obj fr )
{
  return NEW_FLOAT(fmod(VAL_FLOAT(fl),VAL_FLOAT(fr)));
}


/****************************************************************************
**
*F  FuncFLOAT_INT(<int>) . . . . . . . . . . . . . . . conversion
**
*/

Obj FuncFLOAT_INT( Obj self, Obj i)
{
  if (!IS_INTOBJ(i))
    return Fail;
  else
    return NEW_FLOAT((Double)INT_INTOBJ(i));
}

/****************************************************************************
**
*F  FuncFLOAT_STRING(<string>) . . . . . . . . . . . . . . . conversion
**
*/

Obj FuncFLOAT_STRING( Obj self, Obj s)
{

  while (!IsStringConv(s))
    {
      s = ErrorReturnObj("FLOAT_STRING: object to be converted must be a string not a %s",
			 (Int)(InfoBags[TNUM_OBJ(s)].name),0,"You can return a string to continue" );
    }
  return NEW_FLOAT((Double) atof((char*)CHARS_STRING(s)));
}

/****************************************************************************
**
*F SumIntFloat( <int>, <float> )
**
*/

Obj SumIntFloat( Obj i, Obj f )
{
  return NEW_FLOAT( (Double)(INT_INTOBJ(i)) + VAL_FLOAT(f));
}


/****************************************************************************
**
*F FuncSIN_FLOAT( <self>, <float> ) . .The sin function from the math library
**
*/

Obj FuncSIN_FLOAT( Obj self, Obj f)
{
  return NEW_FLOAT(sin(VAL_FLOAT(f)));
}

Obj FuncLOG_FLOAT( Obj self, Obj f)
{
  return NEW_FLOAT(log(VAL_FLOAT(f)));
}

Obj FuncEXP_FLOAT( Obj self, Obj f)
{
  return NEW_FLOAT(exp(VAL_FLOAT(f)));
}

Obj FuncRINT_FLOAT( Obj self, Obj f)
{
  return NEW_FLOAT(rint(VAL_FLOAT(f)));
}

Obj FuncFLOOR_FLOAT( Obj self, Obj f)
{
  return NEW_FLOAT(floor(VAL_FLOAT(f)));
}

Obj FuncSTRING_FLOAT( Obj self, Obj f)
{
  Char buf[32];
  Obj str;
  UInt len;
  sprintf(buf, "%g",VAL_FLOAT(f));
  len = SyStrlen(buf);
  str = NEW_STRING(len);
  SyStrncat(CSTR_STRING(str),buf,len);
  return str;
}

/****************************************************************************
**

*F * * * * * * * * * * * * * initialize package * * * * * * * * * * * * * * *
*/

/****************************************************************************
**

*V  GVarFilts . . . . . . . . . . . . . . . . . . . list of filters to export
*/
static StructGVarFilt GVarFilts [] = {

    { "IS_FLOAT", "obj", &IsFloatFilt,
      IsFloatHandler, "src/float.c:IS_FLOAT" },

    { 0 }

};


/****************************************************************************
**
*V  GVarFuncs . . . . . . . . . . . . . . . . . . list of functions to export
*/
static StructGVarFunc GVarFuncs [] = {
  { "FLOAT_INT", 1, "int",
    FuncFLOAT_INT, "src/float.c:FLOAT_INT" },

  { "FLOAT_STRING", 1, "int",
    FuncFLOAT_STRING, "src/float.c:FLOAT_STRING" },

  { "SIN_FLOAT", 1, "float",
    FuncSIN_FLOAT, "src/float.c:SIN_FLOAT" },

  { "LOG_FLOAT", 1, "float",
    FuncLOG_FLOAT, "src/float.c:LOG_FLOAT" },

  { "EXP_FLOAT", 1, "float",
    FuncEXP_FLOAT, "src/float.c:EXP_FLOAT" },

  { "RINT_FLOAT", 1, "float",
    FuncRINT_FLOAT, "src/float.c:RINT_FLOAT" },

  { "FLOOR_FLOAT", 1, "float",
    FuncFLOOR_FLOAT, "src/float.c:FLOOR_FLOAT" },

  { "STRING_FLOAT", 1, "float",
    FuncSTRING_FLOAT, "src/float.c:STRING_FLOAT" },



  {0}
};


/****************************************************************************
**

*F  InitKernel( <module> )  . . . . . . . . initialise kernel data structures
*/
static Int InitKernel (
    StructInitInfo *    module )
{
    /* install the marking functions for floatean values                    */
    InfoBags[ T_FLOAT ].name = "float";
    InitMarkFuncBags( T_FLOAT, MarkNoSubBags );

    /* init filters and functions                                          */
    InitHdlrFiltsFromTable( GVarFilts );
    InitHdlrFuncsFromTable( GVarFuncs );

    /* install the kind function                                           */
    ImportGVarFromLibrary( "TYPE_FLOAT", &TYPE_FLOAT );
    ImportGVarFromLibrary( "TYPE_FLOAT0", &TYPE_FLOAT0 );
    TypeObjFuncs[ T_FLOAT ] = TypeFloat;

    /* install the saving functions                                       */
    SaveObjFuncs[ T_FLOAT ] = SaveFloat;

    /* install the loading functions                                       */
    LoadObjFuncs[ T_FLOAT ] = LoadFloat;

    /* install the printer for floatean values                              */
    PrintObjFuncs[ T_FLOAT ] = PrintFloat;

    /* install the comparison functions                                    */
    EqFuncs[ T_FLOAT ][ T_FLOAT ] = EqFloat;
    LtFuncs[ T_FLOAT ][ T_FLOAT ] = LtFloat;
    
    /* install the unary arithmetic methods                                */
    ZeroFuncs[ T_FLOAT ] = ZeroFloat;
    ZeroMutFuncs[ T_FLOAT ] = ZeroFloat;
    AInvMutFuncs[ T_FLOAT ] = AInvFloat;
    OneFuncs [ T_FLOAT ] = OneFloat;
    OneMutFuncs [ T_FLOAT ] = OneFloat;
    InvFuncs [ T_FLOAT ] = InvFloat;

    /* install binary arithmetic methods */
    ProdFuncs[ T_FLOAT ][ T_FLOAT ] = ProdFloat;
    PowFuncs [ T_FLOAT ][ T_FLOAT ] = PowFloat;
    SumFuncs[ T_FLOAT ][ T_FLOAT ] = SumFloat;
    DiffFuncs [ T_FLOAT ][ T_FLOAT ] = DiffFloat;
    QuoFuncs [ T_FLOAT ][ T_FLOAT ] = QuoFloat;
    LQuoFuncs [ T_FLOAT ][ T_FLOAT ] = LQuoFloat;
    ModFuncs [ T_FLOAT ][ T_FLOAT ] = ModFloat;
    SumFuncs [ T_INT ][ T_FLOAT ] = SumIntFloat;
    
    /* Probably support mixed ops with small ints in the kernel as well
       on any reasonable system, all small ints should have float equivalents

       Anything else, like mixed ops with rationals, we can leave to the library
       at least for a while */
     
    
    /* return success                                                      */
    return 0;
}


/****************************************************************************
**
*F  InitLibrary( <module> ) . . . . . . .  initialise library data structures
*/
static Int InitLibrary (
    StructInitInfo *    module )
{
/*  UInt            gvar; */
/*  Obj             tmp;  */

    /* init filters and functions                                          */
    InitGVarFiltsFromTable( GVarFilts );
    InitGVarFuncsFromTable( GVarFuncs );

    /* return success                                                      */
    return 0;
}


/****************************************************************************
**
*F  InitInfoFloat()  . . . . . . . . . . . . . . . . . table of init functions
*/
static StructInitInfo module = {
    MODULE_BUILTIN,                     /* type                           */
    "float",                             /* name                           */
    0,                                  /* revision entry of c file       */
    0,                                  /* revision entry of h file       */
    0,                                  /* version                        */
    0,                                  /* crc                            */
    InitKernel,                         /* initKernel                     */
    InitLibrary,                        /* initLibrary                    */
    0,                                  /* checkInit                      */
    0,                                  /* preSave                        */
    0,                                  /* postSave                       */
    0                                   /* postRestore                    */
};

StructInitInfo * InitInfoFloat ( void )
{
    module.revision_c = Revision_float_c;
    module.revision_h = Revision_float_h;
    FillInVersion( &module );
    return &module;
}


/****************************************************************************
**

*E  float.c  . . . . . . . . . . . . . . . . . . . . . . . . . . . . ends here
*/