Changeset 970 for cpp/common/nonstd_math.cpp
- Timestamp:
- 06/30/20 00:34:59 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/nonstd_math.cpp
r896 r970 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 5 5 #include "nonstd_math.h" 6 #ifdef USE_PRINTFLOAT_DRAGON4 7 #include <PrintFloat/PrintFloat.h> 8 #else 9 #include <sstream> 10 #endif 6 11 7 12 RandomGenerator &rndGetInstance() … … 11 16 } 12 17 18 int doubleToString(double x, int precision, char *buffer, int bufferlen) 19 { 20 #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/ 25 #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); 29 #endif 30 } 31 32 33 double round(const double x, const int precision) 34 { 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 46 //printf("%d %20g \t %20g\n", precision, x, rounded); //for debugging 47 return rounded; 48 } 13 49 14 50
Note: See TracChangeset
for help on using the changeset viewer.