- Timestamp:
- 07/08/20 01:53:39 (4 years ago)
- Location:
- cpp/common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/nonstd_math.cpp
r970 r979 4 4 5 5 #include "nonstd_math.h" 6 #ifdef USE_PRINTFLOAT_DRAGON47 6 #include <PrintFloat/PrintFloat.h> 8 #else9 7 #include <sstream> 10 # endif8 #include <algorithm> // std::min 11 9 12 10 RandomGenerator &rndGetInstance() … … 16 14 } 17 15 16 std::string doubleToString(double x, int precision) 17 { 18 std::stringstream ss; 19 ss << std::fixed; 20 ss.precision(precision); //set the number of places after decimal 21 ss << x; 22 return ss.str(); 23 } 24 18 25 int doubleToString(double x, int precision, char *buffer, int bufferlen) 19 26 { 27 // C++ in 2020 and the impossible challenge https://stackoverflow.com/questions/277772/avoid-trailing-zeroes-in-printf 28 if (precision < 0) 29 { 30 // The "g" format does not allow to use the number of decimal places after the decimal point. Dragon4 on the other hand fills in unnecessary trailinig zeros... so both are good only for "full precision". 20 31 #ifdef USE_PRINTFLOAT_DRAGON4 21 return PrintFloat64(buffer, bufferlen, x,22 ((x < -1e17) || (x > 1e17) || ((x < 1e-4) && (x > -1e-4) && (x != 0.0)))23 ? PrintFloatFormat_Scientific : PrintFloatFormat_Positional,24 precision); //http://www.ryanjuckett.com/programming/printing-floating-point-numbers/32 return PrintFloat64(buffer, bufferlen, x, 33 ((x < -1e17) || (x > 1e17) || ((x < 1e-4) && (x > -1e-4) && (x != 0.0))) 34 ? PrintFloatFormat_Scientific : PrintFloatFormat_Positional, 35 precision); //http://www.ryanjuckett.com/programming/printing-floating-point-numbers/ 25 36 #else 26 char format[10]; 27 sprintf(format, "%%.%dg", precision < 0 ? 17 : precision); //https://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value 28 return sprintf(buffer, format, x); 37 return sprintf(buffer, "%.17g", x); 29 38 #endif 39 } 40 else 41 { 42 std::string s = doubleToString(x, precision); 43 strncpy(buffer, s.c_str(), std::min(bufferlen, (int)s.length() + 1)); 44 buffer[bufferlen - 1] = 0; //ensure the string is truncated 45 return s.length(); 46 } 30 47 } 31 48 … … 33 50 double round(const double x, const int precision) 34 51 { 35 #ifdef USE_PRINTFLOAT_DRAGON4 36 char buffer[30]; 37 doubleToString(x, precision, buffer, 30); 38 double rounded = strtod(buffer, NULL); 39 #else 40 std::stringstream ss; 41 ss << std::fixed; 42 ss.precision(precision); // set the number of places after decimal 43 ss << x; 44 double rounded = stod(ss.str()); 45 #endif 52 double rounded = std::stod(doubleToString(x, precision)); 46 53 //printf("%d %20g \t %20g\n", precision, x, rounded); //for debugging 47 54 return rounded; -
cpp/common/nonstd_math.h
r970 r979 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 19Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 35 35 int doubleToString(double x, int precision, char *buffer, int bufferlen); 36 36 37 #include <string> 38 std::string doubleToString(double x, int precision); 37 39 double round(const double x, const int precision); 38 40
Note: See TracChangeset
for help on using the changeset viewer.