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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
|
#define _GNU_SOURCE
#include "services/quick-answers/math.h"
#include <math.h>
//
// FoobarMathExpression:
//
// A fully parsed mathematical expression which can be evaluated, for example "sin(pi) - 3" would be represented as:
// - Operation: SUB
// - Function: SIN
// - Constant: PI
// - Value: 3
//
struct _FoobarMathExpression
{
FoobarMathExpressionType type;
union
{
struct
{
FoobarMathValue v;
} value;
struct
{
FoobarMathFunction f;
FoobarMathExpression* input;
} function;
struct
{
FoobarMathConstant c;
} constant;
struct
{
FoobarMathOperation o;
FoobarMathExpression* lhs;
FoobarMathExpression* rhs;
} operation;
};
};
//
// FoobarMathValue:
//
// Representation of a value. The goal of this memory structure is to store the value as an integer for as long as
// possible. Because of this, there is a "decimal_places" field, so the actual value of an integer is
// $v * 10^(-decimal_places)$.
//
// If absolutely necessary (for example, because we use a function like sin), the value is converted to a floating-point
// value.
//
// Note that we use GMP to store the integer value to allow for arbitrarily large values. Because of this, the structure
// should always be freed using foobar_math_value_free.
//
// ---------------------------------------------------------------------------------------------------------------------
// Expressions
// ---------------------------------------------------------------------------------------------------------------------
//
// Helper to allocate a new FoobarMathExpression representing a value.
//
// Ownership of "value" is transferred to the new expression.
//
FoobarMathExpression* foobar_math_expression_new_value( FoobarMathValue value )
{
FoobarMathExpression* res = g_new0( FoobarMathExpression, 1 );
res->type = FOOBAR_MATH_EXPRESSION_VALUE;
res->value.v = value;
return res;
}
//
// Helper to allocate a new FoobarMathExpression representing a function to be evaluated with another expression as its
// parameter.
//
// Ownership of "input" is transferred to the new expression.
//
FoobarMathExpression* foobar_math_expression_new_function(
FoobarMathFunction function,
FoobarMathExpression* input )
{
FoobarMathExpression* res = g_new0( FoobarMathExpression, 1 );
res->type = FOOBAR_MATH_EXPRESSION_FUNCTION;
res->function.f = function;
res->function.input = input;
return res;
}
//
// Helper to allocate a new FoobarMathExpression representing a known constant.
//
FoobarMathExpression* foobar_math_expression_new_constant( FoobarMathConstant constant )
{
FoobarMathExpression* res = g_new0( FoobarMathExpression, 1 );
res->type = FOOBAR_MATH_EXPRESSION_CONSTANT;
res->constant.c = constant;
return res;
}
//
// Helper to allocate a new FoobarMathExpression representing an operation to be evaluated with two expressions as its
// parameters.
//
// Ownership of "lhs" and "rhs" is transferred to the new expression.
//
FoobarMathExpression* foobar_math_expression_new_operation(
FoobarMathOperation operation,
FoobarMathExpression* lhs,
FoobarMathExpression* rhs )
{
FoobarMathExpression* res = g_new0( FoobarMathExpression, 1 );
res->type = FOOBAR_MATH_EXPRESSION_OPERATION;
res->operation.o = operation;
res->operation.lhs = lhs;
res->operation.rhs = rhs;
return res;
}
//
// Release the memory associated with a mathematical expression, along with the expressions it owns.
//
void foobar_math_expression_free( FoobarMathExpression* expression )
{
if ( expression )
{
switch ( expression->type )
{
case FOOBAR_MATH_EXPRESSION_VALUE:
foobar_math_value_free( expression->value.v );
break;
case FOOBAR_MATH_EXPRESSION_FUNCTION:
foobar_math_expression_free( expression->function.input );
break;
case FOOBAR_MATH_EXPRESSION_CONSTANT:
break;
case FOOBAR_MATH_EXPRESSION_OPERATION:
foobar_math_expression_free( expression->operation.lhs );
foobar_math_expression_free( expression->operation.rhs );
break;
default:
g_warn_if_reached( );
break;
}
g_free( expression );
}
}
//
// Print a tree representation of a mathematical expression.
//
// This is mainly useful for debugging. "indentation" describes the current indentation level for the recursive call
// (should be 0 initially).
//
void foobar_math_expression_print(
FoobarMathExpression const* expr,
gint indentation )
{
for (gint i = 0; i < indentation; ++i) { g_print("| "); }
switch ( expr->type )
{
case FOOBAR_MATH_EXPRESSION_VALUE:
{
g_autofree gchar* val = foobar_math_value_to_string( expr->value.v );
g_print( "%s\n", val );
break;
}
case FOOBAR_MATH_EXPRESSION_FUNCTION:
switch ( expr->function.f )
{
case FOOBAR_MATH_FUNCTION_NEGATE:
g_print( "-\n" );
break;
case FOOBAR_MATH_FUNCTION_SIN:
g_print( "sin\n" );
break;
case FOOBAR_MATH_FUNCTION_COS:
g_print( "cos\n" );
break;
case FOOBAR_MATH_FUNCTION_SEC:
g_print( "sec\n" );
break;
case FOOBAR_MATH_FUNCTION_CSC:
g_print( "csc\n" );
break;
case FOOBAR_MATH_FUNCTION_TAN:
g_print( "tan\n" );
break;
case FOOBAR_MATH_FUNCTION_COT:
g_print( "cot\n" );
break;
case FOOBAR_MATH_FUNCTION_ARCSIN:
g_print( "arcsin\n" );
break;
case FOOBAR_MATH_FUNCTION_ARCCOS:
g_print( "arccos\n" );
break;
case FOOBAR_MATH_FUNCTION_ARCSEC:
g_print( "arcsec\n" );
break;
case FOOBAR_MATH_FUNCTION_ARCCSC:
g_print( "arccsc\n" );
break;
case FOOBAR_MATH_FUNCTION_ARCTAN:
g_print( "arctan\n" );
break;
case FOOBAR_MATH_FUNCTION_ARCCOT:
g_print( "arccot\n" );
break;
case FOOBAR_MATH_FUNCTION_EXP:
g_print( "exp\n" );
break;
case FOOBAR_MATH_FUNCTION_SQRT:
g_print( "sqrt\n" );
break;
default:
g_warn_if_reached( );
break;
}
foobar_math_expression_print( expr->function.input, indentation + 1 );
break;
case FOOBAR_MATH_EXPRESSION_CONSTANT:
switch ( expr->constant.c )
{
case FOOBAR_MATH_CONSTANT_PI:
g_print( "pi\n" );
break;
case FOOBAR_MATH_CONSTANT_E:
g_print( "e\n" );
break;
default:
g_warn_if_reached( );
break;
}
break;
case FOOBAR_MATH_EXPRESSION_OPERATION:
switch ( expr->operation.o )
{
case FOOBAR_MATH_OPERATION_ADD:
g_print( "+\n" );
break;
case FOOBAR_MATH_OPERATION_SUB:
g_print( "-\n" );
break;
case FOOBAR_MATH_OPERATION_MUL:
g_print( "*\n" );
break;
case FOOBAR_MATH_OPERATION_DIV:
g_print( "/\n" );
break;
case FOOBAR_MATH_OPERATION_POW:
g_print( "^\n" );
break;
default:
g_warn_if_reached( );
break;
}
foobar_math_expression_print( expr->operation.lhs, indentation + 1 );
foobar_math_expression_print( expr->operation.rhs, indentation + 1 );
break;
default:
g_warn_if_reached( );
break;
}
}
//
// Evaluate an expression, producing a value.
//
// On success (indicated by the return value TRUE), the value should be freed using foobar_math_value_free.
//
gboolean foobar_math_evaluate(
FoobarMathExpression const* expr,
FoobarMathValue* out_value )
{
switch ( expr->type )
{
case FOOBAR_MATH_EXPRESSION_VALUE:
foobar_math_value_copy( expr->value.v, out_value );
return TRUE;
case FOOBAR_MATH_EXPRESSION_CONSTANT:
switch ( expr->constant.c )
{
case FOOBAR_MATH_CONSTANT_PI:
foobar_math_value_from_float( M_PIl, out_value );
return TRUE;
case FOOBAR_MATH_CONSTANT_E:
foobar_math_value_from_float( M_El, out_value );
return TRUE;
default:
g_warn_if_reached( );
return FALSE;
}
case FOOBAR_MATH_EXPRESSION_FUNCTION:
{
FoobarMathValue input = { 0 };
if ( !foobar_math_evaluate( expr->function.input, &input ) ) { return FALSE; }
if ( expr->function.f == FOOBAR_MATH_FUNCTION_NEGATE )
{
*out_value = input;
foobar_math_value_negate( out_value );
return TRUE;
}
long double input_float = foobar_math_value_to_float( input );
foobar_math_value_free( input );
long double result = NAN;
switch ( expr->function.f )
{
case FOOBAR_MATH_FUNCTION_SIN:
result = sinl( input_float );
break;
case FOOBAR_MATH_FUNCTION_COS:
result = cosl( input_float );
break;
case FOOBAR_MATH_FUNCTION_SEC:
result = 1.L / cosl( input_float );
break;
case FOOBAR_MATH_FUNCTION_CSC:
result = 1.L / sinl( input_float );
break;
case FOOBAR_MATH_FUNCTION_TAN:
result = tanl( input_float );
break;
case FOOBAR_MATH_FUNCTION_COT:
result = 1.L / tanl( input_float );
break;
case FOOBAR_MATH_FUNCTION_ARCSIN:
result = asinl( input_float );
break;
case FOOBAR_MATH_FUNCTION_ARCCOS:
result = acosl( input_float );
break;
case FOOBAR_MATH_FUNCTION_ARCSEC:
result = acosl( 1.L / input_float );
break;
case FOOBAR_MATH_FUNCTION_ARCCSC:
result = asinl( 1.L / input_float );
break;
case FOOBAR_MATH_FUNCTION_ARCTAN:
result = atanl( input_float );
break;
case FOOBAR_MATH_FUNCTION_ARCCOT:
result = atanl( 1.L / input_float );
break;
case FOOBAR_MATH_FUNCTION_EXP:
result = expl( input_float );
break;
case FOOBAR_MATH_FUNCTION_SQRT:
result = sqrtl( input_float );
break;
case FOOBAR_MATH_FUNCTION_NEGATE:
default:
g_warn_if_reached( );
return FALSE;
}
if ( isnanl( result ) || isinfl( result ) ) { return FALSE; }
foobar_math_value_from_float( result, out_value );
return TRUE;
}
case FOOBAR_MATH_EXPRESSION_OPERATION:
{
FoobarMathValue lhs = { 0 };
if ( !foobar_math_evaluate( expr->operation.lhs, &lhs ) ) { return FALSE; }
FoobarMathValue rhs = { 0 };
if ( !foobar_math_evaluate( expr->operation.rhs, &rhs ) )
{
foobar_math_value_free( lhs );
return FALSE;
}
gboolean success = FALSE;
switch ( expr->operation.o )
{
case FOOBAR_MATH_OPERATION_ADD:
success = foobar_math_value_add( &lhs, &rhs, out_value );
break;
case FOOBAR_MATH_OPERATION_SUB:
success = foobar_math_value_sub( &lhs, &rhs, out_value );
break;
case FOOBAR_MATH_OPERATION_MUL:
success = foobar_math_value_mul( &lhs, &rhs, out_value );
break;
case FOOBAR_MATH_OPERATION_DIV:
success = foobar_math_value_div( &lhs, &rhs, out_value );
break;
case FOOBAR_MATH_OPERATION_POW:
success = foobar_math_value_pow( &lhs, &rhs, out_value );
break;
default:
g_warn_if_reached( );
break;
}
foobar_math_value_free( lhs );
foobar_math_value_free( rhs );
return success;
}
default:
g_warn_if_reached( );
return FALSE;
}
}
// ---------------------------------------------------------------------------------------------------------------------
// Values
// ---------------------------------------------------------------------------------------------------------------------
//
// Initialize a new integer value to zero.
//
// After this, the structure should be freed using foobar_math_value_free.
//
void foobar_math_value_new_int( FoobarMathValue* out_value )
{
out_value->type = FOOBAR_MATH_VALUE_INT;
mpz_init( out_value->int_value.v );
out_value->int_value.decimal_places = 0;
}
//
// Initialize a new value from its floating point representation.
//
// Although technically not necessary, you should still call foobar_math_value_free on the returned value.
//
void foobar_math_value_from_float(
long double value,
FoobarMathValue* out_value )
{
out_value->type = FOOBAR_MATH_VALUE_FLOAT;
out_value->float_value.v = value;
}
//
// Parse a value from its string representation. This will always return an integer representation.
//
// On success (return value TRUE), the structure should be freed using foobar_math_value_free.
//
gboolean foobar_math_value_from_string(
gchar const* input,
gsize input_length,
FoobarMathValue* out_value )
{
g_autofree gchar* without_sep = g_new0( gchar, input_length + 1 );
gsize sep_pos = input_length - 1;
gsize j = 0;
for ( gsize i = 0; i < input_length; ++i )
{
if ( input[i] == '.' )
{
sep_pos = i;
}
else
{
without_sep[j++] = input[i];
}
}
foobar_math_value_new_int( out_value );
out_value->int_value.decimal_places = input_length - 1 - sep_pos;
if ( mpz_init_set_str( out_value->int_value.v, without_sep, 10 ) == -1 )
{
mpz_clear( out_value->int_value.v );
return FALSE;
}
return TRUE;
}
//
// Create a copy of a value, allocating necessary memory.
//
// After this, the structure should be freed using foobar_math_value_free.
//
void foobar_math_value_copy(
FoobarMathValue value,
FoobarMathValue* out_value )
{
*out_value = value;
switch ( value.type )
{
case FOOBAR_MATH_VALUE_INT:
mpz_init_set( out_value->int_value.v, value.int_value.v );
break;
case FOOBAR_MATH_VALUE_FLOAT:
break;
default:
g_warn_if_reached( );
break;
}
}
//
// Release memory associated with a value.
//
// This is necessary because the integer representation is stored using GMP, which can store arbitrarily large values.
//
void foobar_math_value_free( FoobarMathValue value )
{
switch ( value.type )
{
case FOOBAR_MATH_VALUE_INT:
mpz_clear( value.int_value.v );
break;
case FOOBAR_MATH_VALUE_FLOAT:
break;
default:
g_warn_if_reached( );
break;
}
}
// ---------------------------------------------------------------------------------------------------------------------
// Operations
// ---------------------------------------------------------------------------------------------------------------------
//
// Operation equivalent to "value *= -1".
//
void foobar_math_value_negate( FoobarMathValue* value )
{
switch ( value->type )
{
case FOOBAR_MATH_VALUE_INT:
mpz_neg( value->int_value.v, value->int_value.v );
break;
case FOOBAR_MATH_VALUE_FLOAT:
value->float_value.v = -value->float_value.v;
break;
default:
g_warn_if_reached( );
break;
}
}
//
// Operation setting "out_value = lhs + rhs".
//
// After this, the result value should be freed using foobar_math_value_free.
//
gboolean foobar_math_value_add(
FoobarMathValue* lhs,
FoobarMathValue* rhs,
FoobarMathValue* out_value )
{
if ( foobar_math_value_unify_integers( lhs, rhs ) )
{
foobar_math_value_new_int( out_value );
out_value->int_value.decimal_places = lhs->int_value.decimal_places;
mpz_add( out_value->int_value.v, lhs->int_value.v, rhs->int_value.v );
return TRUE;
}
long double lhs_float = foobar_math_value_to_float( *lhs );
long double rhs_float = foobar_math_value_to_float( *rhs );
foobar_math_value_from_float( lhs_float + rhs_float, out_value );
return TRUE;
}
//
// Operation setting "out_value = lhs - rhs".
//
// After this, the result value should be freed using foobar_math_value_free.
//
gboolean foobar_math_value_sub(
FoobarMathValue* lhs,
FoobarMathValue* rhs,
FoobarMathValue* out_value )
{
if ( foobar_math_value_unify_integers( lhs, rhs ) )
{
foobar_math_value_new_int( out_value );
out_value->int_value.decimal_places = lhs->int_value.decimal_places;
mpz_sub( out_value->int_value.v, lhs->int_value.v, rhs->int_value.v );
return TRUE;
}
long double lhs_float = foobar_math_value_to_float( *lhs );
long double rhs_float = foobar_math_value_to_float( *rhs );
foobar_math_value_from_float( lhs_float - rhs_float, out_value );
return TRUE;
}
//
// Operation setting "out_value = lhs * rhs".
//
// After this, the result value should be freed using foobar_math_value_free.
//
gboolean foobar_math_value_mul(
FoobarMathValue* lhs,
FoobarMathValue* rhs,
FoobarMathValue* out_value )
{
if ( lhs->type == FOOBAR_MATH_VALUE_INT && rhs->type == FOOBAR_MATH_VALUE_INT )
{
foobar_math_value_new_int( out_value );
out_value->int_value.decimal_places = lhs->int_value.decimal_places + rhs->int_value.decimal_places;
mpz_mul( out_value->int_value.v, lhs->int_value.v, rhs->int_value.v );
return TRUE;
}
long double lhs_float = foobar_math_value_to_float( *lhs );
long double rhs_float = foobar_math_value_to_float( *rhs );
foobar_math_value_from_float( lhs_float * rhs_float, out_value );
return TRUE;
}
//
// Operation setting "out_value = lhs / rhs".
//
// After this, the result value should be freed using foobar_math_value_free.
//
gboolean foobar_math_value_div(
FoobarMathValue* lhs,
FoobarMathValue* rhs,
FoobarMathValue* out_value )
{
if ( lhs->type == FOOBAR_MATH_VALUE_INT && rhs->type == FOOBAR_MATH_VALUE_INT )
{
if ( mpz_sgn( rhs->int_value.v ) == 0 ) { return FALSE; }
if ( rhs->int_value.decimal_places > lhs->int_value.decimal_places )
{
foobar_math_value_unify_integers( lhs, rhs );
}
foobar_math_value_new_int( out_value );
out_value->int_value.decimal_places = lhs->int_value.decimal_places - rhs->int_value.decimal_places;
mpz_t rem;
mpz_init( rem );
mpz_divmod( out_value->int_value.v, rem, lhs->int_value.v, rhs->int_value.v );
gboolean result_is_int = mpz_sgn( rem ) == 0;
mpz_clear( rem );
if ( result_is_int ) { return TRUE; }
foobar_math_value_free( *out_value );
}
long double lhs_float = foobar_math_value_to_float( *lhs );
long double rhs_float = foobar_math_value_to_float( *rhs );
long double result = lhs_float / rhs_float;
if ( isnanl( result ) || isinfl( result ) ) { return FALSE; }
foobar_math_value_from_float( result, out_value );
return TRUE;
}
//
// Operation setting "out_value = lhs ^ rhs".
//
// After this, the result value should be freed using foobar_math_value_free.
//
gboolean foobar_math_value_pow(
FoobarMathValue* lhs,
FoobarMathValue* rhs,
FoobarMathValue* out_value )
{
if ( lhs->type == FOOBAR_MATH_VALUE_INT &&
rhs->type == FOOBAR_MATH_VALUE_INT &&
rhs->int_value.decimal_places == 0 &&
mpz_fits_ulong_p( rhs->int_value.v ) )
{
unsigned long exp = mpz_get_ui( rhs->int_value.v );
foobar_math_value_new_int( out_value );
out_value->int_value.decimal_places = lhs->int_value.decimal_places * exp;
mpz_pow_ui( out_value->int_value.v, lhs->int_value.v, exp );
return TRUE;
}
long double lhs_float = foobar_math_value_to_float( *lhs );
long double rhs_float = foobar_math_value_to_float( *rhs );
long double result = powl( lhs_float, rhs_float );
if ( isnanl( result ) || isinfl( result ) ) { return FALSE; }
foobar_math_value_from_float( result, out_value );
return TRUE;
}
//
// Ensure that two integer values have the same number of decimal places.
//
// This may change the in-memory-representation of a value, but not its actual value. It only increases the precision of
// one value
//
// If either a or b is not stored as an integer, this will return FALSE.
//
gboolean foobar_math_value_unify_integers(
FoobarMathValue* a,
FoobarMathValue* b )
{
if ( a->type != FOOBAR_MATH_VALUE_INT || b->type != FOOBAR_MATH_VALUE_INT ) { return FALSE; }
// Extend the less precise representation so both numbers have the same number of decimal places.
gsize decimal_places = MAX( a->int_value.decimal_places, b->int_value.decimal_places );
mpz_t exp;
mpz_init( exp );
if ( decimal_places > a->int_value.decimal_places )
{
mpz_ui_pow_ui( exp, 10, decimal_places - a->int_value.decimal_places );
mpz_mul( a->int_value.v, a->int_value.v, exp );
}
if ( decimal_places > b->int_value.decimal_places )
{
mpz_ui_pow_ui( exp, 10, decimal_places - b->int_value.decimal_places );
mpz_mul( b->int_value.v, b->int_value.v, exp );
}
mpz_clear( exp );
a->int_value.decimal_places = b->int_value.decimal_places = decimal_places;
return TRUE;
}
// ---------------------------------------------------------------------------------------------------------------------
// Conversions
// ---------------------------------------------------------------------------------------------------------------------
//
// Format a value as a string.
//
// The result is a null-terminated string which should be freed using g_free.
//
gchar* foobar_math_value_to_string( FoobarMathValue value )
{
switch ( value.type )
{
case FOOBAR_MATH_VALUE_INT:
{
gsize cap = mpz_sizeinbase( value.int_value.v, 10 );
cap = MAX( cap, value.int_value.decimal_places + 1 ); // pad with zeroes if necessary
gchar* result = g_new0( gchar, cap + 3 ); // length + null terminator + sign + separator
mpz_get_str( result, 10, value.int_value.v );
gsize len = strlen( result ); // actual size may be 1 char less (+ len now also includes sign)
gboolean has_sign = result[0] == '-';
if ( has_sign )
{
// add the sign back later, makes everything else easier
result += 1;
len -= 1;
}
// pad with leading zeroes if necessary
gsize padded_len = MAX( len, value.int_value.decimal_places + 1 );
memmove( &result[padded_len - len], result, len + 1 );
memset( result, '0', padded_len - len );
len = padded_len;
gboolean trailing_zeros = TRUE;
gsize decimal_places = value.int_value.decimal_places;
gsize actual_decimal_places = decimal_places;
for ( gsize i = 0; i < decimal_places; ++i )
{
if ( trailing_zeros && result[len - 1 - i] == '0' )
{
result[len - 1 - i] = '\0';
actual_decimal_places -= 1;
}
else
{
trailing_zeros = FALSE;
}
}
if ( actual_decimal_places > 0 )
{
memmove( &result[len + 1 - decimal_places], &result[len - decimal_places], decimal_places + 1 );
result[len - decimal_places] = '.';
}
if ( has_sign )
{
result -= 1;
len += 1;
}
return result;
}
case FOOBAR_MATH_VALUE_FLOAT:
{
gchar* result = g_strdup_printf( "%.30Lf", value.float_value.v );
// trim trailing zeros
size_t len = strlen( result );
for ( gsize i = 0; i < len; ++i )
{
if ( result[len - 1 - i] == '.' )
{
result[len - 1 - i] = '\0';
break;
}
else if ( result[len - 1 - i] == '0' )
{
result[len - 1 - i] = '\0';
}
else
{
break;
}
}
return result;
}
default:
g_warn_if_reached( );
return NULL;
}
}
//
// Convert a value to its floating-point representation.
//
// This may be a lossy conversion and should only be done if absolutely necessary.
//
long double foobar_math_value_to_float( FoobarMathValue value )
{
switch ( value.type )
{
case FOOBAR_MATH_VALUE_INT:
{
mpz_t exp, integer, frac;
mpz_inits( exp, integer, frac, NULL );
mpz_ui_pow_ui( exp, 10, value.int_value.decimal_places );
mpz_mod( frac, value.int_value.v, exp );
mpz_div( integer, value.int_value.v, exp );
mpf_t f_exp, f_frac;
mpf_inits( f_exp, f_frac, NULL );
mpf_set_z( f_exp, exp );
mpf_set_z( f_frac, frac );
mpf_div( f_frac, f_frac, f_exp );
long double result = mpz_get_d( integer ) + mpf_get_d( f_frac );
mpf_clears( f_exp, f_frac, NULL );
mpz_clears( exp, integer, frac, NULL );
return result;
}
case FOOBAR_MATH_VALUE_FLOAT:
return value.float_value.v;
default:
g_warn_if_reached( );
return 0;
}
}
|