Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interval <-> char* conversion #190

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/arithmetic/ibex_Interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "ibex_Interval.h"
#include <float.h>
#include <cassert>
#include <sstream>
#include <iomanip>

#ifdef _IBEX_WITH_GAOL_
#include "ibex_Interval_gaol.cpp_"
Expand Down Expand Up @@ -289,5 +291,66 @@ int Interval::diff(const Interval& y, Interval& c1, Interval& c2) const {
return res;
}

Interval::Interval(const std::string& s) {
if(s.find(EMPTY_SET) != std::string::npos)
*this = EMPTY_SET;

else if(s.find(POS_REALS) != std::string::npos)
*this = POS_REALS;

else if(s.find(NEG_REALS) != std::string::npos)
*this = NEG_REALS;

else if(s.find(ALL_REALS) != std::string::npos)
*this = ALL_REALS;

else { // string of the form "[lb, ub]"
// Removing unwanted spaces:
std::string clean_s = s;
clean_s.erase(std::remove(clean_s.begin(), clean_s.end(), ' '), clean_s.end());

std::string delimiter = ",";
size_t pos_delimiter = clean_s.find(',');

assert(pos_delimiter != std::string::npos);

std::string lb = clean_s.substr(1, clean_s.find(delimiter) - delimiter.length());
std::string ub = clean_s.substr(lb.length() + 2, clean_s.length() - lb.length() - delimiter.length() - 2);

*this = Interval(atof(lb.c_str()), atof(ub.c_str()));
}
}

std::ostream& operator<<(std::ostream& os, const Interval& x) {
// A specific string has to be set for the following cases:
// (in order to not depend on the interval library)
if(x == Interval::EMPTY_SET)
os << "[ empty ]";

else if(x == Interval::POS_REALS)
os << "[ pos_reals ]";

else if(x == Interval::NEG_REALS)
os << "[ neg_reals ]";

else if(x == Interval::ALL_REALS)
os << "[ all_reals ]";

else
{
#ifdef _IBEX_WITH_FILIB_
filib::interval<FI_BASE,FI_ROUNDING,FI_MODE>::precision(os.precision());
#endif
os << "[" << (double)x.lb() << ", " << (double)x.ub() << "]";
}

return os;
}

Interval::operator std::string() const {
std::stringstream sstream;
sstream << *this;
return sstream.str();
}

} // end namespace
26 changes: 22 additions & 4 deletions src/arithmetic/ibex_Interval.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ class Interval {
/** \brief Create [a,a]. */
Interval(double a);

/**
* \brief Parses the std::string str, interpreting its content as
* an interval and instantiating the corresponding object.
*
* Note: unwanted spaces are removed before cast.
*
* \param str std::string, e.g.: "[-0.215,53.2]" or "[ empty ]"
*/
Interval(const std::string& str);

/** \brief True iff *this and x are exactly the same intervals. */
bool operator==(const Interval& x) const;

Expand Down Expand Up @@ -528,6 +538,10 @@ class Interval {
*/
operator const ExprConstant&() const;

/** \brief Cast the interval into a const std::string
*/
operator std::string() const;

//private:
#ifdef _IBEX_WITH_GAOL_
/* \brief Wrap the gaol-interval [x]. */
Expand Down Expand Up @@ -935,8 +949,6 @@ inline Interval& Interval::operator=(double x) {
return *this;
}



inline Interval& Interval::operator=(const Interval& x) {
itv = x.itv;
return *this;
Expand All @@ -953,7 +965,6 @@ inline Interval& Interval::inflate(double delta, double chi) {
return *this;
}


inline bool Interval::operator!=(const Interval& x) const {
return !(*this==x);
}
Expand Down Expand Up @@ -1191,7 +1202,6 @@ inline bool bwd_atan(const Interval& y, Interval& x) {
return !x.is_empty();
}


inline bool bwd_acosh(const Interval& y, Interval& x) {
if (y.is_empty() || y.ub()<0.0) {
x.set_empty(); return false;
Expand Down Expand Up @@ -1520,6 +1530,14 @@ inline bool bwd_imod(Interval& x, Interval& y, const double& p) {
return true;
}

inline std::string operator+(const std::string& s, const Interval& x) {
return s + (std::string)x;
}

inline std::string operator+(const Interval& x, const std::string& s) {
return (std::string)x + s;
}

} // end namespace ibex

#endif // _IBEX_INTERVAL_H_
8 changes: 0 additions & 8 deletions src/arithmetic/ibex_Interval_bias.cpp_
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,5 @@ const Interval Interval::PI(3.14159265358979323846, Succ(3.14159265358979323846)
const Interval Interval::TWO_PI = PI*2.0;
const Interval Interval::HALF_PI = PI/2.0;

std::ostream& operator<<(std::ostream& os, const Interval& x) {
if (x.is_empty())
return os << "[ empty ]";
else
return os << "[" << x.lb() << "," << x.ub() << "]";
//return os << x.itv;
}

} // end namespace

8 changes: 0 additions & 8 deletions src/arithmetic/ibex_Interval_direct.cpp_
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,5 @@ const Interval Interval::PI(3.14159265358979323846);
const Interval Interval::TWO_PI = PI*2;
const Interval Interval::HALF_PI = PI/2;

std::ostream& operator<<(std::ostream& os, const Interval& x) {
if (x.is_empty())
return os << "[ empty ]";
else
return os << "[" << x.lb() << "," << x.ub() << "]";
//return os << x.itv;
}

} // end namespace

10 changes: 0 additions & 10 deletions src/arithmetic/ibex_Interval_filib.cpp_
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,5 @@ const Interval Interval::HALF_PI =(filib::interval<FI_BASE,FI_ROUNDING,FI_MODE>:
filib::fp_traits<FI_BASE,FI_ROUNDING>::upward_divides(filib::constructFromBitSet<FI_BASE>( "0:10000000000:1001001000011111101101010100010001000010110100011001"),2.0)));
*/



std::ostream& operator<<(std::ostream& os, const Interval& x) {
if (x.is_empty())
return os << "[ empty ]";
else
filib::interval<FI_BASE,FI_ROUNDING,FI_MODE>::precision(os.precision());
return os << x.itv;
}

} // end namespace

4 changes: 0 additions & 4 deletions src/arithmetic/ibex_Interval_gaol.cpp_
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,4 @@ const Interval Interval::PI(pi_dn,pi_up);
const Interval Interval::TWO_PI(2.0*pi_dn,2.0*pi_up);
const Interval Interval::HALF_PI(half_pi_dn,half_pi_up);

std::ostream& operator<<(std::ostream& os, const Interval& x) {
return os << x.itv;
}

} // end namespace
5 changes: 2 additions & 3 deletions src/tools/ibex_String.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//============================================================================
// I B E X
// File : ibex_String.cpp
// Author : Gilles Chabert
// Author : Gilles Chabert, Simon Rohou
// Copyright : Ecole des Mines de Nantes (France)
// License : See the LICENSE file
// Created : Jul 18, 2012
// Last Update : Jul 18, 2012
// Last Update : April 18, 2016
//============================================================================

#include "ibex_String.h"
Expand Down Expand Up @@ -58,5 +58,4 @@ char* next_generated_func_name() {
return next_generated_name(BASE_FUNC_NAME,generated_func_count++);
}


} // end namespace ibex
5 changes: 3 additions & 2 deletions src/tools/ibex_String.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//============================================================================
// I B E X
// File : ibex_String.h
// Author : Gilles Chabert
// Author : Gilles Chabert, Simon Rohou
// Copyright : Ecole des Mines de Nantes (France)
// License : See the LICENSE file
// Created : Jul 18, 2012
// Last Update : Jul 18, 2012
// Last Update : April 18, 2016
//============================================================================

#ifndef __IBEX_STRING_H_
Expand All @@ -14,6 +14,7 @@
#include <cassert>
#include <string.h>
#include <stdio.h>
#include "ibex_Interval.h"

namespace ibex {

Expand Down
39 changes: 37 additions & 2 deletions tests/TestString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
* License : This program can be distributed under the terms of the GNU LGPL.
* See the file COPYING.LESSER.
*
* Author(s) : Gilles Chabert
* Author(s) : Gilles Chabert, Simon Rohou
* Created : Mar 2, 2012
* Updated : April 18, 2016
* ---------------------------------------------------------------------------- */

#include <iomanip>
#include "TestString.h"
#include "ibex_String.h"

using namespace std;

Expand All @@ -30,4 +31,38 @@ void TestString::test02() {
free(buf);
}

bool TestString::testConversion(const Interval& intv, int precision)
{
return testConversionString(intv) && testConversionOstream(intv, precision);
}

bool TestString::testConversionString(const Interval& intv)
{
Interval intv_parsed = Interval(" " + intv + " "); // adding unwanted spaces
return intv_parsed == intv ||
fabs(intv_parsed.lb() - intv.lb()) < 1.0e-1 && fabs(intv_parsed.ub() - intv.ub()) < 1.0e-1;
}

bool TestString::testConversionOstream(const Interval& intv, int precision)
{
stringstream sstream;
sstream << setprecision(precision) << " " << intv << " "; // adding unwanted spaces
Interval intv_parsed = Interval(sstream.str());
return intv_parsed == intv ||
fabs(intv_parsed.lb() - intv.lb()) < 1.0e-1 && fabs(intv_parsed.ub() - intv.ub()) < 1.0e-1;
}

void TestString::test03() {
CPPUNIT_ASSERT(testConversion(Interval(-6.3588151,2.864632), 4));
CPPUNIT_ASSERT(testConversion(Interval(0,27885.5523), 15));
CPPUNIT_ASSERT(testConversion(Interval(-99,-97), 7));
CPPUNIT_ASSERT(testConversion(Interval::EMPTY_SET, 4));
CPPUNIT_ASSERT(testConversion(Interval::ALL_REALS, 4));
CPPUNIT_ASSERT(testConversion(Interval::ZERO, 4));
CPPUNIT_ASSERT(testConversion(Interval::PI, 4));
CPPUNIT_ASSERT(testConversion(Interval::ONE, 3));
CPPUNIT_ASSERT(testConversion(Interval::POS_REALS, 3));
CPPUNIT_ASSERT(testConversion(Interval::NEG_REALS, 3));
}

} // end namespace
13 changes: 11 additions & 2 deletions tests/TestString.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* License : This program can be distributed under the terms of the GNU LGPL.
* See the file COPYING.LESSER.
*
* Author(s) : Gilles Chabert
* Author(s) : Gilles Chabert, Simon Rohou
* Created : Mar 2, 2012
* Updated : April 18, 2016
* ---------------------------------------------------------------------------- */

#ifndef __TEST_STRING_H__
Expand All @@ -25,13 +26,21 @@ class TestString : public CppUnit::TestFixture {

CPPUNIT_TEST_SUITE(TestString);


CPPUNIT_TEST(test01);
CPPUNIT_TEST(test02);
CPPUNIT_TEST(test03);

CPPUNIT_TEST_SUITE_END();

void test01();
void test02();
void test03();

protected:

bool testConversion(const Interval& intv, int precision);
bool testConversionString(const Interval& intv);
bool testConversionOstream(const Interval& intv, int precision);
};

CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
Expand Down