Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Dóra Kocsis
dynare
Commits
48672ad1
Commit
48672ad1
authored
Jul 12, 2012
by
Sébastien Villemot
Browse files
Preprocessor: handle underflows and overflows as MATLAB and Octave do
i.e. don't fail when they happen and instead return 0 or Inf
parent
4d183aa9
Changes
5
Hide whitespace changes
Inline
Side-by-side
preprocessor/DataTree.cc
View file @
48672ad1
...
...
@@ -51,7 +51,7 @@ DataTree::~DataTree()
}
expr_t
DataTree
::
AddNonNegativeConstant
(
const
string
&
value
)
throw
(
NumericalConstants
::
InvalidFloatingPointNumberException
)
DataTree
::
AddNonNegativeConstant
(
const
string
&
value
)
{
int
id
=
num_constants
.
AddNonNegativeConstant
(
value
);
...
...
preprocessor/DataTree.hh
View file @
48672ad1
...
...
@@ -110,7 +110,7 @@ public:
};
//! Adds a non-negative numerical constant (possibly Inf or NaN)
expr_t
AddNonNegativeConstant
(
const
string
&
value
)
throw
(
NumericalConstants
::
InvalidFloatingPointNumberException
)
;
expr_t
AddNonNegativeConstant
(
const
string
&
value
);
//! Adds a variable
/*! The default implementation of the method refuses any lag != 0 */
virtual
VariableNode
*
AddVariable
(
int
symb_id
,
int
lag
=
0
);
...
...
preprocessor/NumericalConstants.cc
View file @
48672ad1
...
...
@@ -26,7 +26,7 @@
#include
"NumericalConstants.hh"
int
NumericalConstants
::
AddNonNegativeConstant
(
const
string
&
iConst
)
throw
(
InvalidFloatingPointNumberException
)
NumericalConstants
::
AddNonNegativeConstant
(
const
string
&
iConst
)
{
map
<
string
,
int
>::
const_iterator
iter
=
numConstantsIndex
.
find
(
iConst
);
...
...
@@ -37,12 +37,10 @@ NumericalConstants::AddNonNegativeConstant(const string &iConst) throw (InvalidF
mNumericalConstants
.
push_back
(
iConst
);
numConstantsIndex
[
iConst
]
=
id
;
errno
=
0
;
double
val
=
strtod
(
iConst
.
c_str
(),
NULL
);
// We check that the number is valid (e.g. not like "1e-10000")
if
(
errno
!=
0
)
throw
InvalidFloatingPointNumberException
(
iConst
);
/* Note that we allow underflows (will be converted to 0) and overflows (will
be converted to Inf), as MATLAB and Octave do. */
assert
(
val
>=
0
||
isnan
(
val
));
// Check we have a positive constant or a NaN
double_vals
.
push_back
(
val
);
...
...
preprocessor/NumericalConstants.hh
View file @
48672ad1
...
...
@@ -37,15 +37,8 @@ private:
//! Map matching constants to their id
map
<
string
,
int
>
numConstantsIndex
;
public:
class
InvalidFloatingPointNumberException
{
public:
const
string
fp
;
InvalidFloatingPointNumberException
(
const
string
&
fp_arg
)
:
fp
(
fp_arg
)
{}
};
//! Adds a non-negative constant (possibly Inf or NaN) and returns its ID
int
AddNonNegativeConstant
(
const
string
&
iConst
)
throw
(
InvalidFloatingPointNumberException
)
;
int
AddNonNegativeConstant
(
const
string
&
iConst
);
//! Get a constant in string form
string
get
(
int
ID
)
const
;
//! Get a constant in double form
...
...
preprocessor/ParsingDriver.cc
View file @
48672ad1
...
...
@@ -258,15 +258,7 @@ ParsingDriver::add_equation_tags(string *key, string *value)
expr_t
ParsingDriver
::
add_non_negative_constant
(
string
*
constant
)
{
expr_t
id
;
try
{
id
=
data_tree
->
AddNonNegativeConstant
(
*
constant
);
}
catch
(
NumericalConstants
::
InvalidFloatingPointNumberException
&
e
)
{
error
(
"Invalid floating point number: "
+
*
constant
);
}
expr_t
id
=
data_tree
->
AddNonNegativeConstant
(
*
constant
);
delete
constant
;
return
id
;
}
...
...
@@ -747,17 +739,11 @@ void
ParsingDriver
::
add_value
(
string
*
v
)
{
expr_t
id
;
try
{
if
(
v
->
at
(
0
)
==
'-'
)
id
=
data_tree
->
AddUMinus
(
data_tree
->
AddNonNegativeConstant
(
v
->
substr
(
1
,
string
::
npos
)));
else
id
=
data_tree
->
AddNonNegativeConstant
(
*
v
);
}
catch
(
NumericalConstants
::
InvalidFloatingPointNumberException
&
e
)
{
error
(
"Invalid floating point number: "
+
*
v
);
}
if
(
v
->
at
(
0
)
==
'-'
)
id
=
data_tree
->
AddUMinus
(
data_tree
->
AddNonNegativeConstant
(
v
->
substr
(
1
,
string
::
npos
)));
else
id
=
data_tree
->
AddNonNegativeConstant
(
*
v
);
delete
v
;
det_shocks_values
.
push_back
(
id
);
...
...
@@ -1004,17 +990,11 @@ void
ParsingDriver
::
add_to_row_const
(
string
*
v
)
{
expr_t
id
;
try
{
if
(
v
->
at
(
0
)
==
'-'
)
id
=
data_tree
->
AddUMinus
(
data_tree
->
AddNonNegativeConstant
(
v
->
substr
(
1
,
string
::
npos
)));
else
id
=
data_tree
->
AddNonNegativeConstant
(
*
v
);
}
catch
(
NumericalConstants
::
InvalidFloatingPointNumberException
&
e
)
{
error
(
"Invalid floating point number: "
+
*
v
);
}
if
(
v
->
at
(
0
)
==
'-'
)
id
=
data_tree
->
AddUMinus
(
data_tree
->
AddNonNegativeConstant
(
v
->
substr
(
1
,
string
::
npos
)));
else
id
=
data_tree
->
AddNonNegativeConstant
(
*
v
);
delete
v
;
sigmae_row
.
push_back
(
id
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment