Pol  Revision:cb584c9
dbl.cpp
Go to the documentation of this file.
1 
7 #include <cmath>
8 #include <sstream>
9 #include <string>
10 
11 #include "../clib/stlutil.h"
12 #include "berror.h"
13 #include "bobject.h"
14 #include "impstr.h"
15 
16 namespace Pol
17 {
18 namespace Bscript
19 {
20 std::string Double::pack() const
21 {
22  OSTRINGSTREAM os;
23  os << "r" << dval_;
24  return OSTRINGSTREAM_STR( os );
25 }
26 
27 void Double::packonto( std::ostream& os ) const
28 {
29  os << "r" << dval_;
30 }
31 
32 BObjectImp* Double::unpack( const char* pstr )
33 {
34  double dv;
35  ISTRINGSTREAM is( pstr );
36  if ( is >> dv )
37  {
38  return new Double( dv );
39  }
40  else
41  {
42  return new BError( "Error extracting Real value" );
43  }
44 }
45 
46 BObjectImp* Double::unpack( std::istream& is )
47 {
48  double dv;
49  if ( is >> dv )
50  {
51  return new Double( dv );
52  }
53  else
54  {
55  return new BError( "Error extracting Real value" );
56  }
57 }
58 
59 size_t Double::sizeEstimate() const
60 {
61  return sizeof( Double );
62 }
63 
64 
65 bool Double::operator==( const BObjectImp& objimp ) const
66 {
67  if ( objimp.isa( OTDouble ) )
68  {
69  double diff = dval_ - ( (Double&)objimp ).dval_;
70  return fabs( diff ) < 0.00000001;
71  }
72  else if ( objimp.isa( OTLong ) )
73  {
74  double diff = dval_ - ( (BLong&)objimp ).value();
75  return fabs( diff ) < 0.00000001;
76  }
77  else if ( objimp.isa( OTBoolean ) )
78  {
79  return isTrue() == static_cast<const BBoolean&>( objimp ).isTrue();
80  }
81  else
82  {
83  return false;
84  }
85 }
86 
87 bool Double::operator<( const BObjectImp& objimp ) const
88 {
89  if ( objimp.isa( OTDouble ) )
90  {
91  return ( dval_ < ( (Double&)objimp ).dval_ );
92  }
93  else if ( objimp.isa( OTLong ) )
94  {
95  return ( dval_ < ( (BLong&)objimp ).value() );
96  }
97 
98  return base::operator<( objimp );
99 }
100 
101 std::string Double::getStringRep() const
102 {
103  OSTRINGSTREAM os;
104 
105  os << dval_;
106  return OSTRINGSTREAM_STR( os );
107 }
108 
110 {
111  return objimp.selfPlusObj( *this );
112 }
113 
114 BObjectImp* Double::selfPlusObj( const BLong& objimp ) const
115 {
116  return new Double( dval_ + objimp.value() );
117 }
118 BObjectImp* Double::selfPlusObj( const Double& objimp ) const
119 {
120  return new Double( dval_ + objimp.dval_ );
121 }
122 BObjectImp* Double::selfPlusObj( const String& objimp ) const
123 {
124  return new String( getStringRep() + objimp.data() );
125 }
127 {
128  objimp.selfPlusObj( *this, obj );
129 }
130 void Double::selfPlusObj( BLong& objimp, BObject& obj )
131 {
132  obj.setimp( selfPlusObj( objimp ) );
133 }
134 void Double::selfPlusObj( Double& objimp, BObject& /*obj*/ )
135 {
136  dval_ += objimp.dval_;
137 }
138 void Double::selfPlusObj( String& objimp, BObject& obj )
139 {
140  obj.setimp( selfPlusObj( objimp ) );
141 }
142 
144 {
145  return objimp.selfMinusObj( *this );
146 }
147 BObjectImp* Double::selfMinusObj( const BLong& objimp ) const
148 {
149  return new Double( dval_ - objimp.value() );
150 }
151 BObjectImp* Double::selfMinusObj( const Double& objimp ) const
152 {
153  return new Double( dval_ - objimp.dval_ );
154 }
155 BObjectImp* Double::selfMinusObj( const String& objimp ) const
156 {
157  String s( getStringRep() );
158  return s.selfMinusObj( objimp );
159 }
161 {
162  objimp.selfMinusObj( *this, obj );
163 }
164 void Double::selfMinusObj( BLong& objimp, BObject& /*obj*/ )
165 {
166  dval_ -= objimp.value();
167 }
168 void Double::selfMinusObj( Double& objimp, BObject& /*obj*/ )
169 {
170  dval_ -= objimp.value();
171 }
172 void Double::selfMinusObj( String& objimp, BObject& obj )
173 {
174  obj.setimp( selfMinusObj( objimp ) );
175 }
176 
178 {
179  return objimp.selfTimesObj( *this );
180 }
181 BObjectImp* Double::selfTimesObj( const BLong& objimp ) const
182 {
183  return new Double( dval_ * objimp.value() );
184 }
185 BObjectImp* Double::selfTimesObj( const Double& objimp ) const
186 {
187  return new Double( dval_ * objimp.value() );
188 }
190 {
191  objimp.selfTimesObj( *this, obj );
192 }
193 void Double::selfTimesObj( BLong& objimp, BObject& /*obj*/ )
194 {
195  dval_ *= objimp.value();
196 }
197 void Double::selfTimesObj( Double& objimp, BObject& /*obj*/ )
198 {
199  dval_ *= objimp.value();
200 }
201 
202 
204 {
205  return objimp.selfDividedByObj( *this );
206 }
208 {
209  int divisor = objimp.value();
210  if ( !divisor )
211  return new BError( "Divide by Zero" );
212  else
213  return new Double( dval_ / divisor );
214 }
216 {
217  double divisor = objimp.value();
218  if ( divisor == 0.0 )
219  return new BError( "Divide by Zero" );
220  else
221  return new Double( dval_ / divisor );
222 }
224 {
225  objimp.selfDividedByObj( *this, obj );
226 }
228 {
229  if ( !objimp.value() )
230  obj.setimp( new BError( "Divide by Zero" ) );
231  else
232  dval_ /= objimp.value();
233 }
235 {
236  if ( !objimp.value() )
237  obj.setimp( new BError( "Divide by Zero" ) );
238  else
239  dval_ /= objimp.value();
240 }
241 }
242 }
virtual BObjectImp * selfDividedByObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: dbl.cpp:203
virtual BObjectImp * selfMinusObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: dbl.cpp:143
int value() const
Definition: bobject.h:592
virtual BObjectImp * selfTimesObj(const BObjectImp &objimp) const
Definition: object.cpp:463
bool isa(BObjectType type) const
Definition: bobject.h:353
#define OSTRINGSTREAM_STR(x)
Definition: stlutil.h:76
virtual size_t sizeEstimate() const POL_OVERRIDE
Definition: dbl.cpp:59
virtual BObjectImp * selfMinusObj(const BObjectImp &objimp) const POL_OVERRIDE
Definition: str.cpp:290
void setimp(BObjectImp *imp)
Definition: bobject.h:446
virtual std::string pack() const POL_OVERRIDE
Definition: dbl.cpp:20
virtual BObjectImp * selfDividedByObj(const BObjectImp &objimp) const
Definition: object.cpp:512
virtual bool operator<(const BObjectImp &objimp) const
Definition: object.cpp:283
virtual bool operator==(const BObjectImp &objimp) const POL_OVERRIDE
Definition: dbl.cpp:65
virtual BObjectImp * selfMinusObj(const BLong &objimp) const POL_OVERRIDE
Definition: dbl.cpp:147
virtual bool operator<(const BObjectImp &objimp) const POL_OVERRIDE
Definition: dbl.cpp:87
#define OSTRINGSTREAM
Definition: stlutil.h:75
Double(double dval=0.0)
Definition: bobject.h:697
virtual bool isTrue() const POL_OVERRIDE
Definition: bobject.h:718
virtual BObjectImp * selfTimesObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: dbl.cpp:177
virtual BObjectImp * selfPlusObj(const BLong &objimp) const POL_OVERRIDE
Definition: dbl.cpp:114
#define ISTRINGSTREAM
Definition: stlutil.h:73
virtual BObjectImp * selfTimesObj(const BLong &objimp) const POL_OVERRIDE
Definition: dbl.cpp:181
virtual BObjectImp * selfMinusObj(const BObjectImp &objimp) const
Definition: object.cpp:414
virtual BObjectImp * selfPlusObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: dbl.cpp:109
double value() const
Definition: bobject.h:713
virtual BObjectImp * selfDividedByObj(const BLong &objimp) const POL_OVERRIDE
Definition: dbl.cpp:207
virtual void packonto(std::ostream &os) const POL_OVERRIDE
Definition: dbl.cpp:27
static BObjectImp * unpack(const char *pstr)
Definition: dbl.cpp:32
const char * data() const
Definition: impstr.h:66
virtual std::string getStringRep() const POL_OVERRIDE
Definition: dbl.cpp:101
Definition: berror.cpp:12
virtual BObjectImp * selfPlusObj(const BObjectImp &objimp) const
Definition: object.cpp:365