Skip to content

Commit

Permalink
Use header-only fast_float library instead of std::from_chars due…
Browse files Browse the repository at this point in the history
… to incomplete standard library implementations
  • Loading branch information
lkeegan committed Feb 20, 2024
1 parent 9c09875 commit 5189b7b
Show file tree
Hide file tree
Showing 4 changed files with 3,759 additions and 25 deletions.
30 changes: 30 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,33 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-------------------------------------------------------------------------------

All files in symengine/utilities/fast_float were taken from the fastfloat library
and are licensed under the MIT License:

Copyright (c) 2021 The fast_float authors

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions symengine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ set(HEADERS
refine.h
simplify.h
utilities/stream_fmt.h
utilities/fast_float/fast_float.h
tuple.h
matrix_expressions.h
matrices/matrix_expr.h
Expand Down
33 changes: 8 additions & 25 deletions symengine/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,11 @@
#include <symengine/real_mpfr.h>
#include <symengine/ntheory_funcs.h>
#include <symengine/parser/tokenizer.h>
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
#include <charconv>
#endif
#include <symengine/utilities/fast_float/fast_float.h>

namespace SymEngine
{

static double string_to_double(const char *str, char **endptr)
{
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
// use std::from_chars: locale-independent, non-allocating, non-throwing
double result;
auto [ptr, ec] = std::from_chars(str, nullptr, result);
if (endptr != nullptr) {
*endptr = const_cast<char *>(ptr);
}
return result;
#else
// otherwise fall-back to using strtod
return std::strtod(str, endptr);
#endif
}

RCP<const Basic>
parse(const std::string &s, bool convert_xor,
const std::map<const std::string, const RCP<const Basic>> &constants)
Expand Down Expand Up @@ -329,8 +311,8 @@ RCP<const Basic> Parser::parse_numeric(const std::string &expr)
}
}
if (digits <= 15) {
char *endptr = 0;
double d = string_to_double(startptr, &endptr);
double d;
fast_float::from_chars(startptr, startptr + expr.size(), d);
return real_double(d);
} else {
// mpmath.libmp.libmpf.dps_to_prec
Expand All @@ -339,8 +321,8 @@ RCP<const Basic> Parser::parse_numeric(const std::string &expr)
return real_mpfr(mpfr_class(expr, prec));
}
#else
char *endptr = 0;
double d = string_to_double(startptr, &endptr);
double d;
fast_float::from_chars(startptr, startptr + expr.size(), d);
return real_double(d);
#endif
}
Expand All @@ -350,8 +332,9 @@ std::tuple<RCP<const Basic>, RCP<const Basic>>
Parser::parse_implicit_mul(const std::string &expr)
{
const char *startptr = expr.c_str();
char *endptr = 0;
string_to_double(startptr, &endptr);
double result;
const char *endptr
= fast_float::from_chars(startptr, startptr + expr.size(), result).ptr;

RCP<const Basic> num = one, sym;

Expand Down
Loading

0 comments on commit 5189b7b

Please sign in to comment.