File: doubleRep.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (71 lines) | stat: -rw-r--r-- 1,414 bytes parent folder | download | duplicates (10)
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
#ifdef DOUBLEREP
#include "doubleRep.h"
#include <cmath>



doubleRepMantisa::doubleRepMantisa(MDOUBLE mantissa, int expon){
	_mantissa=mantissa;
	_expon=expon;
	fixParams();
}


doubleRepMantisa::doubleRepMantisa(MDOUBLE a){
	int answerExp;
	MDOUBLE answerMantissa=frexp(a,&answerExp);
	_mantissa=answerMantissa;
	_expon=answerExp;
}

doubleRepMantisa::doubleRepMantisa(const doubleRepMantisa& other): _mantissa(other._mantissa), _expon(other._expon) {
}


//make sure 0.5<=mantissa<1, as a matter of convention
void doubleRepMantisa::fixParams(){
	while (_mantissa>=1){
		_expon++;
		_mantissa/=2.0;
	}
	while ((_mantissa<0.5) && (_mantissa>0)){
		_expon--;
		_mantissa*=2.0;
	}
	while (_mantissa<=-1){
		_expon++;
		_mantissa/=2.0;
	}
	while ((_mantissa>-0.5) && (_mantissa<0)){
		_expon--;
		_mantissa*=2.0;
	}
}

MDOUBLE convert(const doubleRepMantisa& a){
	MDOUBLE aFullRep= ldexp(a._mantissa,a._expon);
	return aFullRep;
}

//switches from base 2 to base e
const MDOUBLE doubleRepMantisa::d_log() const{
  static const MDOUBLE log2(log(2.0));
	return log(_mantissa)+log2*_expon;
}


ostream& operator<<(ostream &out, const doubleRepMantisa& a){
	a.output(out);
	//  a.output0x(out);
//	out<<a._mantissa<<string(" * 2^")<<a._expon;
//	out<<a._mantissa<<" * 2^"<<a._expon;
    return out;
}

istream& operator>>(istream &in, doubleRepMantisa& a) {
  MDOUBLE num;
  in >> num;
  a = num;
  return in;
}
#endif