Ignore:
Timestamp:
06/30/20 00:34:59 (4 years ago)
Author:
Maciej Komosinski
Message:

Added functions to properly round floating point values to specified precision

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/sstring.cpp

    r955 r970  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2323#include "extvalue.h"
    2424#include <assert.h>
    25 #ifdef USE_PRINTFLOAT_DRAGON4
    26 #include <PrintFloat/PrintFloat.h>
    27 #endif
     25#include <common/nonstd_math.h>
    2826
    2927#ifdef MULTITHREADED
     
    367365SString SString::valueOf(double d)
    368366{
    369 #ifdef USE_PRINTFLOAT_DRAGON4
    370367        SString tmp;
    371368        char* here = tmp.directWrite(30);
    372         tmp.endWrite(PrintFloat64(here, 30, d,
    373                 ((d < -1e17) || (d > 1e17) || ((d < 1e-4) && (d > -1e-4) && (d != 0.0)))
    374                 ? PrintFloatFormat_Scientific : PrintFloatFormat_Positional,
    375                 -1));//http://www.ryanjuckett.com/programming/printing-floating-point-numbers/
    376 #else
    377         SString tmp = SString::sprintf("%.17g", d); //https://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value
    378 #endif
     369        tmp.endWrite(doubleToString(d, -1, here, 30));
    379370        if ((!strchr(tmp.c_str(), '.')) && (!strchr(tmp.c_str(), 'e'))) tmp += ".0";
    380371        return tmp;
Note: See TracChangeset for help on using the changeset viewer.