@@ -67,6 +67,13 @@ MathLib::bigint MathLib::toLongNumber(const std::string &str)
6767 return ret;
6868}
6969
70+ std::string MathLib::longToString (const bigint value)
71+ {
72+ std::ostringstream result;
73+ result << value;
74+ return result.str ();
75+ }
76+
7077double MathLib::toDoubleNumber (const std::string &str)
7178{
7279 if (isHex (str))
@@ -81,6 +88,18 @@ double MathLib::toDoubleNumber(const std::string &str)
8188 return ret;
8289}
8390
91+ std::string MathLib::doubleToString (const double value)
92+ {
93+ std::ostringstream result;
94+ result.precision (12 );
95+ result << value;
96+ if (result.str () == " -0" )
97+ return " 0.0" ;
98+ if (result.str ().find (" ." ) == std::string::npos)
99+ return result.str () + " .0" ;
100+ return result.str ();
101+ }
102+
84103bool MathLib::isFloat (const std::string &s)
85104{
86105 // every number that contains a . is a float
@@ -215,25 +234,25 @@ bool MathLib::isInt(const std::string & s)
215234std::string MathLib::add (const std::string & first, const std::string & second)
216235{
217236 if (MathLib::isInt (first) && MathLib::isInt (second)) {
218- return toString<bigint> (toLongNumber (first) + toLongNumber (second));
237+ return longToString (toLongNumber (first) + toLongNumber (second));
219238 }
220239
221240 double d1 = toDoubleNumber (first);
222241 double d2 = toDoubleNumber (second);
223242
224243 int count = 0 ;
225- while (d1 > 100000.0 * d2 && toString< double > (d1+d2)==first && ++count<5 )
244+ while (d1 > 100000.0 * d2 && doubleToString (d1+d2)==first && ++count<5 )
226245 d2 *= 10.0 ;
227- while (d2 > 100000.0 * d1 && toString< double > (d1+d2)==second && ++count<5 )
246+ while (d2 > 100000.0 * d1 && doubleToString (d1+d2)==second && ++count<5 )
228247 d1 *= 10.0 ;
229248
230- return toString< double > (d1 + d2);
249+ return doubleToString (d1 + d2);
231250}
232251
233252std::string MathLib::subtract (const std::string &first, const std::string &second)
234253{
235254 if (MathLib::isInt (first) && MathLib::isInt (second)) {
236- return toString<bigint> (toLongNumber (first) - toLongNumber (second));
255+ return longToString (toLongNumber (first) - toLongNumber (second));
237256 }
238257
239258 if (first == second)
@@ -243,12 +262,12 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon
243262 double d2 = toDoubleNumber (second);
244263
245264 int count = 0 ;
246- while (d1 > 100000.0 * d2 && toString< double > (d1-d2)==first && ++count<5 )
265+ while (d1 > 100000.0 * d2 && doubleToString (d1-d2)==first && ++count<5 )
247266 d2 *= 10.0 ;
248- while (d2 > 100000.0 * d1 && toString< double > (d1-d2)==second && ++count<5 )
267+ while (d2 > 100000.0 * d1 && doubleToString (d1-d2)==second && ++count<5 )
249268 d1 *= 10.0 ;
250269
251- return toString< double > (d1 - d2);
270+ return doubleToString (d1 - d2);
252271}
253272
254273std::string MathLib::divide (const std::string &first, const std::string &second)
@@ -257,17 +276,17 @@ std::string MathLib::divide(const std::string &first, const std::string &second)
257276 bigint b = toLongNumber (second);
258277 if (b == 0 )
259278 throw InternalError (0 , " Internal Error: Division by zero" );
260- return toString<bigint> (toLongNumber (first) / b);
279+ return longToString (toLongNumber (first) / b);
261280 }
262- return toString< double > (toDoubleNumber (first) / toDoubleNumber (second));
281+ return doubleToString (toDoubleNumber (first) / toDoubleNumber (second));
263282}
264283
265284std::string MathLib::multiply (const std::string &first, const std::string &second)
266285{
267286 if (MathLib::isInt (first) && MathLib::isInt (second)) {
268- return toString<bigint> (toLongNumber (first) * toLongNumber (second));
287+ return longToString (toLongNumber (first) * toLongNumber (second));
269288 }
270- return toString< double > (toDoubleNumber (first) * toDoubleNumber (second));
289+ return doubleToString (toDoubleNumber (first) * toDoubleNumber (second));
271290}
272291
273292std::string MathLib::mod (const std::string &first, const std::string &second)
@@ -276,9 +295,9 @@ std::string MathLib::mod(const std::string &first, const std::string &second)
276295 bigint b = toLongNumber (second);
277296 if (b == 0 )
278297 throw InternalError (0 , " Internal Error: Division by zero" );
279- return toString<MathLib::bigint> (toLongNumber (first) % b);
298+ return longToString (toLongNumber (first) % b);
280299 }
281- return toString< double > (fmod (toDoubleNumber (first),toDoubleNumber (second)));
300+ return doubleToString (fmod (toDoubleNumber (first),toDoubleNumber (second)));
282301}
283302
284303std::string MathLib::calculate (const std::string &first, const std::string &second, char action)
@@ -300,13 +319,13 @@ std::string MathLib::calculate(const std::string &first, const std::string &seco
300319 return MathLib::mod (first, second);
301320
302321 case ' &' :
303- return MathLib::toString (MathLib::toLongNumber (first) & MathLib::toLongNumber (second));
322+ return MathLib::longToString (MathLib::toLongNumber (first) & MathLib::toLongNumber (second));
304323
305324 case ' |' :
306- return MathLib::toString (MathLib::toLongNumber (first) | MathLib::toLongNumber (second));
325+ return MathLib::longToString (MathLib::toLongNumber (first) | MathLib::toLongNumber (second));
307326
308327 case ' ^' :
309- return MathLib::toString (MathLib::toLongNumber (first) ^ MathLib::toLongNumber (second));
328+ return MathLib::longToString (MathLib::toLongNumber (first) ^ MathLib::toLongNumber (second));
310329
311330 default :
312331 throw InternalError (0 , std::string (" Unexpected action '" ) + action + " ' in MathLib::calculate(). Please report this to Cppcheck developers." );
@@ -315,31 +334,31 @@ std::string MathLib::calculate(const std::string &first, const std::string &seco
315334
316335std::string MathLib::sin (const std::string &tok)
317336{
318- return toString< double > (std::sin (toDoubleNumber (tok)));
337+ return doubleToString (std::sin (toDoubleNumber (tok)));
319338}
320339
321340
322341std::string MathLib::cos (const std::string &tok)
323342{
324- return toString< double > (std::cos (toDoubleNumber (tok)));
343+ return doubleToString (std::cos (toDoubleNumber (tok)));
325344}
326345
327346std::string MathLib::tan (const std::string &tok)
328347{
329- return toString< double > (std::tan (toDoubleNumber (tok)));
348+ return doubleToString (std::tan (toDoubleNumber (tok)));
330349}
331350
332351
333352std::string MathLib::abs (const std::string &tok)
334353{
335- return toString< double > (std::abs (toDoubleNumber (tok)));
354+ return doubleToString (std::abs (toDoubleNumber (tok)));
336355}
337356
338357bool MathLib::isEqual (const std::string &first, const std::string &second)
339358{
340359 // this conversion is needed for formating
341360 // e.g. if first=0.1 and second=1.0E-1, the direct comparison of the strings whould fail
342- return toString< double > (toDoubleNumber (first)) == toString< double > (toDoubleNumber (second));
361+ return doubleToString (toDoubleNumber (first)) == doubleToString (toDoubleNumber (second));
343362}
344363
345364bool MathLib::isNotEqual (const std::string &first, const std::string &second)
0 commit comments