Bug 100 - DefaultValue::Bind() is not correct for floating points
DefaultValue::Bind() is not correct for floating points
Status: RESOLVED FIXED
Product: ns-3
Classification: Unclassified
Component: core
pre-release
All All
: P3 normal
Assigned To: ns-bugs
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2007-11-02 10:47 UTC by Tom Henderson
Modified: 2008-07-01 13:32 UTC (History)
0 users

See Also:


Attachments
proposed patch that does not use numeric_limits for bounds checking (4.04 KB, patch)
2007-11-08 00:57 UTC, Tom Henderson
Details | Diff
patch according to Mathieu's suggestion (1.97 KB, patch)
2007-11-09 01:36 UTC, Tom Henderson
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Henderson 2007-11-02 10:47:42 UTC
This code snippet does not work:

  NumericDefaultValue<double> y ("test-y", "help-y", 10.0);
  DefaultValue::Bind ("test-y", "0");  // fails
  DefaultValue::Bind ("test-y", "-1.0"); //fails
  DefaultValue::Bind ("test-y", "1.0"); // OK

This bind will fail unless the default value passed is greater than zero.

The problem is in NumericDefaultValue::DoParseValue because the value is compared against std::numeric_limits<double>::min() and max(), which are both positive numbers.

One change would be to separate integer from float default values.  However, I question whether the DoParseValue() code is even doing the right thing, because the act of passing the string value into a numeric value before the comparison is made is going to put this test m_value within range of the numeric limits.  So perhaps the right solution is to remove these checks for NumericDefaultValue?
Comment 1 Tom Henderson 2007-11-08 00:57:36 UTC
Created attachment 85 [details]
proposed patch that does not use numeric_limits for bounds checking
Comment 2 Mathieu Lacage 2007-11-08 02:36:51 UTC
I think that a more appropriate patch would be something along the lines of:

template <typename T> T real_min() {
if(numeric_limits<T>::is_integer) return numeric_limits<T>::min();
else return -numeric_limits<T>::max();
}

which I found with google.
Comment 3 Tom Henderson 2007-11-09 01:36:23 UTC
Created attachment 87 [details]
patch according to Mathieu's suggestion

thanks for the tip
Comment 4 Tom Henderson 2007-11-14 23:57:25 UTC
fixed with changeset 1822