Browse Source

Fixed problem with casting of negative values to fload and long double issue #145

master
Artyom Beilis 8 years ago
parent
commit
2c02b8e260
2 changed files with 10 additions and 2 deletions
  1. +2
    -2
      cppcms/json.h
  2. +8
    -0
      tests/json_test.cpp

+ 2
- 2
cppcms/json.h View File

@@ -706,7 +706,7 @@ namespace json {
static float get(value const &v)
{
double r=v.number();
if( r < std::numeric_limits<float>::min()
if( r < (-std::numeric_limits<float>::max()) // actually should be C++11 lowest, but it should be under IEEE float lowest()=-max()
|| std::numeric_limits<float>::max() < r )
{
throw bad_value_cast();
@@ -727,7 +727,7 @@ namespace json {
}
static void set(value &v,long double const &in)
{
if( in < std::numeric_limits<double>::min()
if( in < -std::numeric_limits<double>::max() // should actually be std::numeric_limits<float>::lowest() but it is ==-max()
|| std::numeric_limits<double>::max() < in )
{
throw bad_value_cast();


+ 8
- 0
tests/json_test.cpp View File

@@ -227,6 +227,14 @@ int main()
TEST(v.type() == cppcms::json::is_object);
TEST(!v.load(part,part+4,true));

{
json::value v;
v["x"]=-10.0;
TEST(v.get<float>("x")==-10.0f);
TEST(v.get<double>("x")==-10.0);
TEST(v.get<long double>("x")==-10.0);
}

}
catch(std::exception const &e)
{


Loading…
Cancel
Save