-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgebra.hpp
2327 lines (2117 loc) · 77.8 KB
/
Algebra.hpp
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of HMMoC-BFloat-Algebra, a tiny C++ library supplying
* the Algebra<> template, for polymorphic representation of non-negative
* floating-point values in either native (eg double), log-transformed, or
* BFloat representations. BFloat is the Buoyant Float type, allowing
* fixed-precision representation at an arbitrary precision range. The
* library is based on code from: Lunter G. HMMoC—a compiler for hidden
* Markov models. Bioinformatics (2007) 23(18): 2485-2487. The code was
* derived by Ian Holmes and Gerton Lunter from Holmes I, Rubin GM. An
* expectation maximization algorithm for training hidden substitution
* models. J Mol Biol. 2002 Apr 12;317(5):753-64. You may use at will,
* subject to the license (Apache v2.0), but *please cite those two papers* in
* your documentation and publications associated with uses of this library.
* Thank you!
*
*
* Copyright (C) 2011 by Paul T Edlefsen, Fred Hutchinson Cancer Research
* Center.
*
* This file is based on algebras.h in HMMoC 1.3, a hidden Markov model
* compiler. Copyright (C) 2007 by Gerton Lunter, Oxford University.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
\*/
//
// Algebra.hpp - extended real types
//
// Paul T Edlefsen, June 1, 2011
// Gerton Lunter, 27/8/04
//
#ifndef HMMOC_BFLOAT_ALGEBRA_HPP
#define HMMOC_BFLOAT_ALGEBRA_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
#if defined(_MSC_VER) && (_MSC_VER >= 1310)
# pragma warning (disable : 4675) // suppress ADL warning
#endif
#include <assert.h>
// Do under- and overflow checking in BFloat? (undef for no)
#ifndef NDEBUG
#define BFLOAT_CHECK_UOFLOW
// Do extra checks in operator- to diagnose negative values? (set to 0 for no)
#define ALGEBRA_CHECK_NEGATIVE
#endif // NDEBUG
#ifndef NBOOST_SERIALIZATION // #define NBOOST_SERIALIZATION if you don't want to compile in the boost serialization library
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/version.hpp>
#endif // NBOOST_SERIALIZATION
// TODO: PUT BACK ? See below (search for seqan)...
//#include <seqan/basic.h>
#include <iostream>
#include <cmath> // for isinf and isnan
#include <limits>
//TAH 11/01/2011 this defines uint32_t, used below
#include <stdint.h>
// You can control some inlining recommentations to the compiler using these:
#define ALGEBRA_INLINE_ARITHMETIC inline
#define ALGEBRA_INLINE_CAST inline
#define ALGEBRA_INLINE_CAST_COMPARE inline
#define ALGEBRA_INLINE_CAST_ARITHMETIC inline
// typedefs
typedef float BFMantissa;
//typedef double BFMantissa;
const BFMantissa cBFloatRange = 20282409603651670423947251286016.0; // 2.03e+31; 2^104
const BFMantissa cBFloatRangeInv = 1.0/cBFloatRange;
// Aaron E. Darling 6/7/7: need to typecast to avoid compiler warnings about imprecise FP representations
const BFMantissa cBFloatRangeSqrt = (BFMantissa)1.0e+18; // Value between square root of the exponent, and the exponent
const BFMantissa cBFloatRangeInvSqrt = (BFMantissa)1.0e-18; // Square of this should still be representable, with full mantissa!
const BFMantissa logcBFloatRange = ::log(cBFloatRange);
const int cBFloatDigits = 7; // Number of significant digits (7 for floats, 16 for doubles?)
const int cBFloatInfinity = 1000000000; // Tiniest number representable is cBFloatRangeInv ^ BFloatInfinity
// PTE 15/9/08
const BFMantissa cBFloatEpsilon = ( BFMantissa )1.193e-07;
const int cBFloatConvTableSize = 100; // This includes many zero entries, it makes additions a bit faster
const int cBFloatDoubleConvTableSize = 50; // Table size for bfloat -> double conversion; cBFloatRange^(-size/2) is double 0
const double cLogspaceEpsilon = 36.7; // TODO: Test & verify
namespace hmmoc {
// PTE 14/9/08
// To help disambiguate ::log( double ) from hmmoc::log(Algebra)
template <typename other_type>
inline
other_type
log ( other_type const & to_be_logged )
{
return ::log( to_be_logged );
} // log( other_type const & )
// Forward decls
class Logspace;
class DoubleRealspace;
class LongDoubleRealspace;
class FloatRealspace;
class BFloat;
static inline std::ostream& bfloat_print ( std::ostream& out, const BFloat& x );
static inline void assertBFloatValidity ( BFloat const & bf );
static inline void BFloatNormalise ( BFloat& a );
//
// BFloats: more buoyant floats.
//
// NOTE: For now BFloats can represent only positive values.
//
// struct{ float + int } is 8 bytes; nice size makes noticable speed difference
//
// For BFloats, infinity is represented as ( e == cBFloatInfinity, and we never let f be std::numeric_limits<BFMantissa>::infinity() ). 0 is represented as ( f == e == 0 ), and NaN as ( f != f ).
class BFloat {
// Boost serialization
#ifndef NBOOST_SERIALIZATION
private:
friend class boost::serialization::access;
template<class Archive>
inline
void serialize ( Archive & ar, const unsigned int /* file_version */ )
{
ar & BOOST_SERIALIZATION_NVP( f );
ar & BOOST_SERIALIZATION_NVP( e );
} // serialize( Archive &, const unsigned int )
#endif // NBOOST_SERIALIZATION
public:
static BFMantissa* aConversionLookup; // used by addition
static double* aDoubleConversionLookup; // used by Value()
BFMantissa f;
int e;
public:
inline
BFloat ( BFMantissa const & iF, int const & iE, bool normalise = false ) :
f( iF ), e( iE )
{
if( normalise ) {
BFloatNormalise( *this );
} else {
assertBFloatValidity( *this );
}
};
inline
BFloat () : f( 0.0 ), e( 0 ) {};
inline
BFloat ( double const & prob ) { *this = prob; }
inline
BFloat ( BFloat const & copy_from ) : f( copy_from.f ), e( copy_from.e ) {
assertBFloatValidity( *this );
};
// Defined later, after these others are defined.
inline
BFloat ( Logspace const & copy_from );
// Defined later, after these others are defined.
inline
BFloat ( DoubleRealspace const & copy_from );
// Defined later, after these others are defined.
inline
BFloat ( LongDoubleRealspace const & copy_from );
// Defined later, after these others are defined.
inline
BFloat ( FloatRealspace const & copy_from );
inline
~BFloat () {};
inline
BFloat &
operator= ( BFloat const & copy_from )
{
f = copy_from.f;
e = copy_from.e;
return *this;
} // operator=( BFloat const & )
// Defined later, after these others are defined.
inline BFloat &
operator= ( Logspace const & copy_from );
// Defined later, after these others are defined.
inline BFloat &
operator= ( DoubleRealspace const & copy_from );
// Defined later, after these others are defined.
inline BFloat &
operator= ( LongDoubleRealspace const & copy_from );
// Defined later, after these others are defined.
inline BFloat &
operator= ( FloatRealspace const & copy_from );
inline BFloat &
operator= ( double prob )
{
if( prob != prob ) {
// NaN
// For now, don't let it be set to NaN.
assert( prob == prob ); // NOT NaN
f = prob;
e = 0;
} else
if( prob == std::numeric_limits<double>::infinity() ) {
f = 1.0;
e = cBFloatInfinity;
} else
// Simplistic double-to-BFloat conversion - can be slow if 'standard' numbers get very large/small
if( prob < std::numeric_limits<double>::min() ) {
#ifdef ALGEBRA_CHECK_NEGATIVE
if( prob < 0.0 ) {
std::cerr << "BFloat: Negative number: " << prob << std::endl;
// TODO: ?
assert( prob >= 0.0 );
}
#endif
f = 0.0;
e = 0;
} else {
f = 0.0;
e = 0;
while( prob > cBFloatRangeSqrt ) {
prob *= cBFloatRangeInv;
e++;
}
while( prob < cBFloatRangeInvSqrt ) {
prob *= cBFloatRange;
e--;
}
f = prob;
}
assertBFloatValidity( *this );
return *this;
} // operator=( double const & )
inline double Value () const {
if( e == 0 ) {
return f;
}
if( e == cBFloatInfinity ) {
return std::numeric_limits<double>::infinity();
}
if( f != f ) {
return std::numeric_limits<double>::quiet_NaN();
}
if (abs(e) < cBFloatDoubleConvTableSize/2) {
return (double)f * aDoubleConversionLookup[ e + cBFloatDoubleConvTableSize/2 ];
} else if (e < cBFloatDoubleConvTableSize/2) {
return 0.0;
} else {
return (double)f * exp((double)e * logcBFloatRange);
}
} // double Value()
// sets to zero
inline
void clear () {
f=0.0; e=0;
}
inline
bool isZero () const
{
return ( f == 0.0 );
} // isZero() const
inline
bool isOne () const
{
return ( f == 1.0 );
} // isOne() const
inline
bool isInfinity () const
{
return ( e == cBFloatInfinity );
} // isInfinity() const
inline
bool isNaN () const
{
return ( f != f );
} // isNaN() const
}; // End class BFloat
//
// dummy class to initialise BFloat lookup table
//
class _BFloatInitialize {
public:
_BFloatInitialize();
};
//
// implementations of BFloat calculations
//
static inline void assertBFloatValidity ( BFloat const & bf )
{
// Never negative
assert( bf.f >= 0.0 );
// TODO: REMOVE? NaN should be ok to represent, but we might want to raise a red flag...
assert( !bf.isNaN() );
// we represent infinity as ( e == cBFloatInfinity ), not as ( f == std::numeric_limits<BFMantissa>::infinity() ).
assert( bf.f != std::numeric_limits<BFMantissa>::infinity() );
if( bf.isZero() ) {
// 0 is represented as both e and f being 0
assert( bf.e == 0 );
//} else if( !bf.isInfinity() ) {
// f must be in this range (AFTER NORMALIZATION)
//assert( bf.f <= cBFloatRangeSqrt );
//assert( bf.f >= cBFloatRangeInvSqrt );
}
return;
} // static assertBFloatValidity( BFloat const & )
// Normalization of BFloat result of a single operation
#ifdef BFLOAT_CHECK_UOFLOW
static inline void BFloatNormalise ( BFloat& a )
//#define BFloatNormalise(a)
{
if (a.f > cBFloatRangeSqrt) {
a.f *= cBFloatRangeInv;
a.e++;
} else if (a.f < cBFloatRangeInvSqrt) {
if( a.f == 0.0 ) {
a.e = 0;
} else {
a.f *= cBFloatRange;
a.e--;
}
}
if( a.e > cBFloatInfinity ) {
std::cerr << "BFloat: Overflow" << std::endl;
a.e = cBFloatInfinity;
} else if( a.e < -cBFloatInfinity ) {
std::cerr << "BFloat: Underflow" << std::endl;
a.e = 0;
a.f = 0.0;
}
// TODO: REMOVE
#ifdef ALGEBRA_CHECK_NEGATIVE
if( a.f < 0 ) {
std::cerr << "BFloat: Negative number: ";
bfloat_print( std::cerr, a );
std::cerr << std::endl;
std::cerr << "\tin BFloatNormalise( BFMantissa & ) [BFLOAT_CHECK_UOFLOW is true]" << std::endl;
}
#endif // ALGEBRA_CHECK_NEGATIVE
#ifndef NDEBUG
assertBFloatValidity( a );
#endif // NDEBUG
}; // BFloatNormalise ( BFloat & ) // when BFLOAT_CHECK_UOFLOW
#else // if BFLOAT_CHECK_UOFLOW .. else ..
static inline void BFloatNormDown ( BFloat& a ) {
a.f *= cBFloatRangeInv;
a.e++;
assert( a.f <= cBFloatRangeSqrt );
}
static inline void BFloatNormUp ( BFloat& a ) {
if (a.f == 0.0) {
a.e = 0;
} else {
a.f *= cBFloatRange;
a.e--;
assert( a.f >= cBFloatRangeInvSqrt );
}
}
static inline void BFloatNormalise ( BFloat& a )
//#define BFloatNormalise(a)
{
if (a.f > cBFloatRangeSqrt) {
BFloatNormDown(a);
} else if (a.f < cBFloatRangeInvSqrt) {
BFloatNormUp(a);
}
#ifdef ALGEBRA_CHECK_NEGATIVE
if( a.f < 0 ) {
std::cerr << "BFloat: Negative number: ";
bfloat_print( std::cerr, a );
std::cerr << std::endl;
std::cerr << "\tin BFloatNormalise( BFMantissa & ) [BFLOAT_CHECK_UOFLOW is false]" << std::endl;
}
#endif // ALGEBRA_CHECK_NEGATIVE
#ifndef NDEBUG
assertBFloatValidity( a );
#endif // NDEBUG
}; // BFloatNormalise ( BFloat & ) // when NOT BFLOAT_CHECK_UOFLOW
#endif // End if BFLOAT_CHECK_UOFLOW .. else ..
static inline void DoubleNormalise ( double& f, int& e )
{
// comparing to 0.0 here fails, because the comparison is done
// using higher-precision doubles, but the subsequent while-loop
// uses true doubles, resulting in an infinite loop. (G.L. 3/9/07)
if( f < std::numeric_limits<double>::min() ) {
#ifdef ALGEBRA_CHECK_NEGATIVE
if( f < 0.0 ) {
std::cerr << "BFloat: Negative number: " << f << std::endl;
// TODO: ?
assert( f >= 0.0 );
}
#endif // ALGEBRA_CHECK_NEGATIVE
f = 0.0;
e = 0;
} else {
while (f > (double)cBFloatRangeSqrt) {
f *= (double)cBFloatRangeInv;
e++;
}
while (f < (double)cBFloatRangeInvSqrt) {
f *= (double)cBFloatRange;
e--;
}
}
}; // DoubleNormalise( double &, int & )
// Logarithm of a BFloat
static inline double bfloat_doublelog ( const BFloat& a ) { return a.e*logcBFloatRange+::log(a.f); }
// BFloat exp of a double -- could be a teensy tiny value.
static inline BFloat bfloat_doubleexp ( double iA )
{
if( iA == std::numeric_limits<double>::infinity() ) {
// exp( inf ) is inf
return BFloat( 1.0, cBFloatInfinity );
}
if( iA == -std::numeric_limits<double>::infinity() ) {
// exp( -inf ) is 0
return 0.0;
}
int iE = (int)floor( iA / ::log(cBFloatRange) );
iA -= iE * ::log(cBFloatRange);
return BFloat( ::exp(iA), iE, true );
} // static bfloat_doubleexp( double )
// Returns a double value - or underflow/overflow if it does not fit.
static inline double bfloat2double ( const BFloat & bfloat) { return bfloat.Value(); }
/// TODO: PUT BACK
//template <typename T>
//inline static double
//toDouble ( T const & v )
//{
// return seqan::convert<T, double>( v );
//} // toDouble( T const & )
inline static double
toDouble ( BFloat const & v )
{
return bfloat2double( v );
} // toDouble( BFloat const & )
inline static long double
toLongDouble ( BFloat const & v )
{
return bfloat2double( v );
} // toLongDouble( BFloat const & )
inline static double
toLogDouble ( BFloat const & v )
{
return bfloat_doublelog( v );
} // toLogDouble( BFloat const & )
static inline BFloat double2bfloat ( double prob )
{
return prob;
}
static inline BFloat bfloat_pr_product ( const BFloat& a, const BFloat& b )
{
//assertBFloatValidity( a );
//assertBFloatValidity( b );
if( a.isZero() ) {
return 0.0;
}
if( b.isZero() ) {
return 0.0;
}
return BFloat( a.f*b.f, a.e+b.e, true );
} // static bfloat_pr_product( BFloat const &, BFloat const & )
static inline BFloat bfloat_pr_double_product ( const BFloat& a, double const & b )
{
//assertBFloatValidity( a );
if( a.isZero() ) {
return 0.0;
}
if( b == 0.0 ) {
return 0.0;
}
register double mantisse = a.f*b;
int exponent = a.e;
DoubleNormalise(mantisse, exponent);
return BFloat( mantisse, exponent, false );
} // bfloat_pr_double_product ( BFloat const &, double const & )
static inline void bfloat_pr_product_accum ( BFloat& a, const BFloat& b )
{
if( a.isZero() ) {
// It's already 0.
return;
}
if( b.isZero() ) {
// Multiplying by 0..
a.clear();
return;
}
a.f *= b.f; a.e += b.e;
BFloatNormalise( a );
} // static bfloat_pr_product_accum( BFloat &, BFloat const & )
static inline void bfloat_pr_double_product_accum ( BFloat& a, double const & b )
{
if( a.isZero() ) {
// It's already 0.
return;
}
if( b == 0.0 ) {
// Multiplying by 0..
a.clear();
return;
}
register double mantisse = a.f*b;
DoubleNormalise( mantisse, a.e );
a.f = mantisse;
assertBFloatValidity( a );
return;
} // static bfloat_pr_double_product_accum( BFloat &, double const & )
// PTE 14/9/08
static inline BFloat bfloat_pr_power (const BFloat& a, const BFloat& b)
{
return BFloat( ::pow(a.f,b.f), (a.e*b.e), true );
}
static inline BFloat bfloat_pr_double_power ( const BFloat& a, double const & b )
{
// TODO: test, make sure it's safe. Probably it's not.
register double mantisse = ::pow(a.f, b);
int exponent = a.e;
DoubleNormalise(mantisse, exponent);
return BFloat(mantisse, exponent);
}
static inline BFloat bfloat_pr_quotient ( const BFloat& a, const BFloat& b )
{
if( b.isZero() ) {
return std::numeric_limits<double>::quiet_NaN();
} else if( b.isInfinity() ) {
return 0.0;
}
return BFloat( a.f/b.f, a.e-b.e, true );
} // bfloat_pr_product ( BFloat const &, BFloat const & )
static inline void bfloat_pr_quotient_accum ( BFloat& a, const BFloat& b )
{
if( b.isZero() ) {
a = std::numeric_limits<double>::quiet_NaN();
} else if( b.isInfinity() ) {
a = 0.0;
} else {
a.f /= b.f;
a.e -= b.e;
}
BFloatNormalise( a );
} // bfloat_pr_quotient_accum( BFloat &, BFloat const & )
static inline BFloat bfloat_pr_sum ( const BFloat& a, const BFloat& b )
{
if( b.isZero() ) {
assertBFloatValidity( a );
return a;
}
if( a.isZero() ) {
assertBFloatValidity( b );
return b;
}
if( b.isInfinity() || a.isInfinity() ) {
return std::numeric_limits<double>::infinity();
}
BFloat r;
if (a.e > b.e) {
if (a.e >= b.e + cBFloatConvTableSize) {
assertBFloatValidity( a );
return a;
} else {
r.f = a.f + b.f * BFloat::aConversionLookup[ a.e - b.e ];
r.e = a.e;
assertBFloatValidity( r );
// TODO: REMOVE?
//BFloatNormalise( r );
return r;
}
} else {
if (a.e <= b.e - cBFloatConvTableSize) {
assertBFloatValidity( b );
return b;
} else {
r.f = b.f + a.f * BFloat::aConversionLookup[ b.e - a.e ];
r.e = b.e;
// TODO: REMOVE?
//BFloatNormalise( r );
return r;
}
}
} // bfloat_pr_sum ( BFloat const &, BFloat const & )
static inline void bfloat_pr_sum_accum ( BFloat& a, const BFloat& b)
{
//assertBFloatValidity( a );
//assertBFloatValidity( b );
if( b.isZero() ) {
// Adding 0 does nothing.
return;
}
if( a.isInfinity() ) {
// Adding to infinity does nothing.
return;
}
if( a.isZero() ) {
// Adding to 0 is an assignment.
a.e = b.e;
a.f = b.f;
assertBFloatValidity( a );
return;
}
if( b.isInfinity() ) {
// Adding infinity makes a infinite.
a.e = b.e;
a.f = b.f;
return;
}
if (a.e >= b.e) {
if (a.e < b.e + cBFloatConvTableSize)
a.f += b.f * BFloat::aConversionLookup[ a.e - b.e ];
} else {
if (a.e > b.e - cBFloatConvTableSize) {
a.f = b.f + a.f * BFloat::aConversionLookup[ b.e - a.e ];
a.e = b.e;
} else {
// Assign b to a.
a.f = b.f;
a.e = b.e;
}
}
// TODO: REMOVE?
//BFloatNormalise( a );
} // bfloat_pr_sum_accum( BFloat &, BFloat const & )
static inline void bfloat_print ( const BFloat& x )
{
bfloat_print( std::cout, x );
}
static inline std::ostream& bfloat_print ( std::ostream& out, const BFloat& x )
{
static const double log10 = ::log(10.0);
static const double maxmantisse = 10.0 * (1.0 - 0.55 * exp(-cBFloatDigits * log10));
//out.setf(ios::fixed,ios::floatfield);
out.precision( cBFloatDigits );
if( x.isInfinity() ) {
out << 1.0 << "e+Inf";
} else if( x.isNaN() ) {
out << "NaN";
} else if( x.isZero() ) {
out << "0.0";
} else if( x.isOne() ) {
out << "1.0";
} else if( x.e == 0 ) {
out << x.f;
} else {
double iM = (::log(x.f) + logcBFloatRange*(double)x.e) / log10;
//long iExp = long(floor(iM));
// PTE 16/9/08
double iExp = floor(iM);
iM = exp((iM - iExp) * log10);
if (iM > maxmantisse) {
iExp += 1;
iM = 1.0;
}
out << iM << ( iExp<0 ? "e" : "e+" ) << iExp;
}
//out.setf(ios::fixed,ios::floatfield); // default
// TODO: MAGIC #
out.precision( 6 ); // default
return out;
} // bfloat_print ( ostream &, BFloat const & )
// forward decl.
static inline bool bfloat_equal ( const BFloat& a, const BFloat& b );
// PTE 14/9/08
static inline BFloat bfloat_pr_diff (const BFloat& a, const BFloat& b)
{
if( b.isZero() ) {
// subtracting zero does nothing.
assertBFloatValidity( a );
return a;
} else if( a.isZero() ) {
// a is 0. Negative!
#ifdef ALGEBRA_CHECK_NEGATIVE
std::cerr << "BFloat: Negative number" << std::endl;
std::cerr << "\tin bfloat_pr_diff( zero, ";
bfloat_print( std::cerr, b );
std::cerr << " )" << std::endl;
#endif // ALGEBRA_CHECK_NEGATIVE
return a; // 0
} // End if a is 0
if( ( a.e == b.e ) && ( a.f <= b.f ) ) {
#ifdef ALGEBRA_CHECK_NEGATIVE
if(
//( (a.e == b.e) && ( ( a.f - b.f ) <= -cBFloatEpsilon ) )
( (a.e == b.e) && ( ( a.f - b.f ) <= -(100.0f*cBFloatEpsilon) ) )
) {
std::cerr << "BFloat: Negative number" << std::endl;
std::cerr << "\tin bfloat_pr_diff( ";
bfloat_print( std::cerr, a );
std::cerr << ", ";
bfloat_print( std::cerr, b );
std::cerr << " )" << std::endl;
std::cerr << "\t\t(a.e - b.e) is zero" << std::endl;
std::cerr << "\t\t(a.f - b.f) is " << ( a.f - b.f ) << std::endl;
return 0.0;
} // End if it's negative ..
#endif // ALGEBRA_CHECK_NEGATIVE
return 0.0;
}
if( (a.e > b.e) || ( (a.e == b.e) && ( ( a.f - b.f ) > -cBFloatEpsilon ) ) ) {
// a positive result...
if( a.e >= b.e + cBFloatConvTableSize ) {
assertBFloatValidity( a );
return a;
} else {
return BFloat( a.f - b.f * BFloat::aConversionLookup[ a.e - b.e ], a.e, true );
}
} else {
// It could still be positive
if( a.e > ( b.e - cBFloatConvTableSize ) ) {
BFMantissa tmp_f = ( a.f * BFloat::aConversionLookup[ b.e - a.e ] ) - b.f;
if( tmp_f > 0 ) {
return BFloat( tmp_f, b.e, true );
} // else negative
}
if( bfloat_equal( a, b ) ) {
return 0.0;
}
// a negative result..
#ifdef ALGEBRA_CHECK_NEGATIVE
std::cerr << "BFloat: Negative number" << std::endl;
std::cerr << "\tin bfloat_pr_diff( ";
bfloat_print( std::cerr, a );
std::cerr << ", ";
bfloat_print( std::cerr, b );
std::cerr << " )" << std::endl;
std::cerr << "\t\t(a.e - b.e) is " << ( a.e - b.e ) << std::endl;
std::cerr << "\t\t(a.f - b.f) is " << ( a.f - b.f ) << std::endl;
#endif // ALGEBRA_CHECK_NEGATIVE
return 0.0;
//if (a.e <= b.e - cBFloatConvTableSize)
// return b;
//else
// return BFloat( b.f - a.f * BFloat::aConversionLookup[ b.e - a.e ], b.e );
} // End if positive .. else negative ..
} // bfloat_pr_diff ( const BFloat &, const BFloat & )
// PTE 14/9/08
static inline void bfloat_pr_diff_accum ( BFloat& a, const BFloat& b)
{
assertBFloatValidity( a );
assertBFloatValidity( b );
if( b.isZero() ) {
// subtracting zero does nothing.
return;
}
if( a.isZero() ) {
// a is 0. Negative!
#ifdef ALGEBRA_CHECK_NEGATIVE
std::cerr << "BFloat: Negative number" << std::endl;
std::cerr << "\tin bfloat_pr_diff_accum( zero, ";
bfloat_print( std::cerr, b );
std::cerr << " )" << std::endl;
#endif // ALGEBRA_CHECK_NEGATIVE
return;
} // End if a is 0
if( ( a.e == b.e ) && ( a.f <= b.f ) ) {
#ifdef ALGEBRA_CHECK_NEGATIVE
if(
( ( a.f - b.f ) <= -cBFloatEpsilon )
) {
std::cerr << "BFloat: Negative number" << std::endl;
std::cerr << "\tin bfloat_pr_diff_accum( ";
bfloat_print( std::cerr, a );
std::cerr << " [ a.f = " << a.f << ", a.e = " << a.e << " ]";
std::cerr << ", ";
bfloat_print( std::cerr, b );
std::cerr << " [ b.f = " << b.f << ", b.e = " << b.e << " ]";
std::cerr << " )" << std::endl;
a.f = 0.0;
a.e = 0;
return;
} // End if it's negative.
#endif // ALGEBRA_CHECK_NEGATIVE
a.f = 0.0;
a.e = 0;
return;
}
if( (a.e > b.e) || ( (a.e == b.e) && ( ( a.f - b.f ) > -cBFloatEpsilon ) ) ) {
// a positive result...
if( a.e < b.e + cBFloatConvTableSize ) {
a.f -= b.f * BFloat::aConversionLookup[ a.e - b.e ];
BFloatNormalise( a );
}
} else {
// It could still be positive
if( a.e > b.e - cBFloatConvTableSize ) {
if( a.f * BFloat::aConversionLookup[ b.e - a.e ] > b.f ) {
a.f = a.f * BFloat::aConversionLookup[ b.e - a.e ] - b.f;
a.e = b.e;
BFloatNormalise( a );
return;
} // else negative..
}
// a negative result..
#ifdef ALGEBRA_CHECK_NEGATIVE
std::cerr << "BFloat: Negative number" << std::endl;
std::cerr << "\tin bfloat_pr_diff_accum( ";
bfloat_print( std::cerr, a );
std::cerr << " [ a.f = " << a.f << ", a.e = " << a.e << " ]";
std::cerr << ", ";
bfloat_print( std::cerr, b );
std::cerr << " [ b.f = " << b.f << ", b.e = " << b.e << " ]";
std::cerr << " )" << std::endl;
#endif // ALGEBRA_CHECK_NEGATIVE
a.f = 0.0;
a.e = 0;
} // End if positive .. else negative ..
} // bfloat_pr_diff_accum ( BFloat &, const BFloat & )
static inline bool bfloat_less ( const BFloat& a, const BFloat& b)
{
// ASSUMPTION: a and b are non-negative.
assertBFloatValidity( a );
assertBFloatValidity( b );
if( a.isZero() ) {
return ( !b.isZero() );
}
if( b.isZero() ) {
return false;
}
if (a.e > b.e) {
if (a.e >= b.e + cBFloatConvTableSize)
return false;
else
return a.f < b.f * BFloat::aConversionLookup[ a.e - b.e ];
}
if (a.e <= b.e - cBFloatConvTableSize)
return true;
else
return a.f * BFloat::aConversionLookup[ b.e - a.e ] < b.f;
} // static bfloat_less ( BFloat const &, BFloat const & )
static inline bool bfloat_equal ( const BFloat& a, const BFloat& b )
{
assertBFloatValidity( a );
assertBFloatValidity( b );
if( a.isZero() ) {
return ( b.isZero() );
}
if( b.isZero() ) {
return false;
}
if (a.e > b.e) {
if (a.e >= b.e + cBFloatConvTableSize)
return false;
else
return a.f == b.f * BFloat::aConversionLookup[ a.e - b.e ];
}
if (a.e <= b.e - cBFloatConvTableSize)
return false;
else
return a.f * BFloat::aConversionLookup[ b.e - a.e ] == b.f;
} // static bfloat_equal( BFloat const &, BFloat const & )
static inline bool bfloat_lessequal ( const BFloat& a, const BFloat& b )
{
assertBFloatValidity( a );
assertBFloatValidity( b );
if( a.isZero() ) {
return true;
}
if( b.isZero() ) {
return false;
}
if (a.e > b.e) {
if (a.e >= b.e + cBFloatConvTableSize)
return false;
else
return a.f <= b.f * BFloat::aConversionLookup[ a.e - b.e ];
}
if (a.e <= b.e - cBFloatConvTableSize)
return true;
else
return a.f * BFloat::aConversionLookup[ b.e - a.e ] <= b.f;
} // static bfloat_lessequal( BFloat const &, BFloat const & )
//
// Wrapper to allow BFloats to be used by Algebra template
//
struct BFloatMethods
{
typedef BFloat Value;
static inline double to_double (BFloat const & iX) { return bfloat2double(iX); }
static inline BFloat from_double (double iP) { return double2bfloat(iP); }
static inline BFloat pmul ( BFloat const & iX, BFloat const & iY) { return bfloat_pr_product(iX,iY); }
static inline BFloat pmuldouble ( BFloat const & iX, double iY) { return bfloat_pr_double_product(iX,iY); }
// PTE 14/9/08
static inline BFloat ppow ( BFloat const & iX, BFloat const & iY) { return bfloat_pr_power(iX,iY); }
// PTE 14/9/08
static inline BFloat ppowdouble ( BFloat const & iX, double iY) { return bfloat_pr_double_power(iX,iY); }
static inline BFloat pdiv ( BFloat const & iX, BFloat const & iY) { return bfloat_pr_quotient(iX,iY); }
static inline BFloat psum ( BFloat const & iX, BFloat const & iY) { return bfloat_pr_sum(iX,iY); }
static inline BFloat pdiff ( BFloat const & iX, BFloat const & iY) { return bfloat_pr_diff(iX,iY); }
static inline BFloat doubleexp ( double iX ) { return bfloat_doubleexp(iX); }
static inline double doublelog ( BFloat const & iX ) { return bfloat_doublelog(iX); }
static inline void pmulacc ( BFloat& iX, BFloat const & iY ) { bfloat_pr_product_accum(iX,iY); }
static inline void pmulaccdouble ( BFloat& iX, double iY ) { bfloat_pr_double_product_accum(iX,iY); }
static inline void pdivacc ( BFloat& iX, BFloat const & iY ) { bfloat_pr_quotient_accum(iX,iY); }
static inline void psumacc ( BFloat& iX, BFloat const & iY ) { bfloat_pr_sum_accum(iX,iY); }
static inline void pdiffacc ( BFloat& iX, BFloat const & iY ) { bfloat_pr_diff_accum(iX,iY); }
static inline bool less ( BFloat const & iX, BFloat const & iY ) { return bfloat_less(iX,iY); }
static inline bool equal ( BFloat const & iX, BFloat const & iY ) { return bfloat_equal(iX,iY); }
static inline bool lessequal ( BFloat const & iX, BFloat const & iY ) { return bfloat_lessequal(iX,iY); }
static inline std::ostream& print ( std::ostream& iOut, BFloat const & iX ) { return bfloat_print( iOut, iX ); }
};
//
// Wrapper to use Algebra with a double or a float.
//
// PTE 15/9/08
template <typename RealspaceType>
struct RealspaceMethods
{
typedef RealspaceType Value;
static inline double to_double ( Value const & iX ) { return (double)iX.x; }
static inline Value from_double ( double const & iP ) { return Value(iP); }
static inline Value pmul ( Value const & iX, Value const & iY ) { return iX.x*iY.x; }
static inline Value pmuldouble ( Value const & iX, double const & iY ) { return iX.x*iY; }
// PTE 14/9/08
static inline Value ppow ( Value const & iX, Value const & iY ) { return ::pow(iX.x,iY.x); }
// PTE 14/9/08
static inline Value ppowdouble ( Value const & iX, double const & iY ) { return ::pow(iX.x,iY); }
static inline Value pdiv ( Value const & iX, Value const & iY ) { return iX.x/iY.x; }
static inline Value psum ( Value const & iX, Value const & iY ) { return iX.x+iY.x; }
static inline Value pdiff ( Value const & iX, Value const & iY ) { return iX.x-iY.x; }
static inline Value doubleexp ( double const & iX ) { return Value(exp(iX)); }
static inline double doublelog ( Value const & iX ) { return (double)log(iX.x); }
static inline void pmulacc ( Value& iX, Value const & iY ) { iX.x*=iY.x; }
static inline void pmulaccdouble ( Value& iX, double const & iY ) { iX.x*=static_cast<double>(Value(iY)); }
static inline void pdivacc ( Value& iX, Value const & iY ) { iX.x /= iY.x; }
static inline void psumacc ( Value& iX, Value const & iY ) { iX.x += iY.x; }
static inline void pdiffacc ( Value& iX, Value const & iY ) { iX.x -= iY.x; }
static inline bool less ( Value const & iX, Value const & iY ) { return ( !equal( iX, iY ) && (iX.x<iY.x) ); } //{ return iX.x<iY.x; }
// TODO: Implement a numeric_limits<DoubleRealspace> that inherits from numeric_limits<double>, and likewise for FloatRealspace, etc.
//static inline bool equal ( Value const & iX, Value const & iY ) { return ( ( iX.x == iY.x ) || ( ( iX.x > iY.x ) ? ( ( iX.x-iY.x ) <= std::numeric_limits<RealspaceType>::epsilon() ) : ( ( iY.x-iX.x ) <= std::numeric_limits<RealspaceType>::epsilon() ) ) ); } //{ return iX.x==iY.x; }
//static inline bool equal ( Value const & iX, Value const & iY ) { return ( ( iX.x == iY.x ) || ( ( iX.x > iY.x ) ? ( ( iX.x-iY.x ) <= std::numeric_limits<float>::epsilon() ) : ( ( iY.x-iX.x ) <= std::numeric_limits<float>::epsilon() ) ) ); } //{ return iX.x==iY.x; }