Changeset 1253 for cpp


Ignore:
Timestamp:
06/22/23 03:27:28 (18 months ago)
Author:
Maciej Komosinski
Message:

Turn -0.0 to 0.0 when the allowed range starts at 0.0

Location:
cpp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/nonstd_math.h

    r1053 r1253  
    3838std::string doubleToString(double x, int precision);
    3939double round(const double x, const int precision);
     40static inline void clipNegativeZeroIfNeeded(double& value, const double range_low)
     41{
     42        if (value == 0.0 && range_low == 0.0) // if we have range_low==0.0 and we get value==-0.0 (which is ==0.0)
     43                value = 0.0; //turn -0.0 to 0.0 so that it does not look like exceeding the allowed range (even though -0.0==0.0). This code unnecessarily also "overwrites" value==0.0 with 0.0, but it is faster and simpler than additionally checking std::signbit(value) just to distinguish -0.0 from 0.0.
     44        //these conditions are not intended for range_low==-0.0, as we assume nobody would define allowed ranges using -0.0.
     45}
    4046
    4147
  • cpp/frams/param/param.cpp

    r1217 r1253  
    1111#include <frams/util/sstringutils.h>
    1212#include <common/virtfile/stringfile.h>
     13#include <common/nonstd_math.h>
    1314
    1415#ifdef _DEBUG
     
    358359{
    359360        SString ret = getName();
    360         if (ret.size()>0) //name might be unavailable, happened in GenMan sub-Params
     361        if (ret.size() > 0) //name might be unavailable, happened in GenMan sub-Params
    361362                ret += ".";
    362363        ret += id(prop);
     
    367368{
    368369        SString name_dot_prop = nameDotProperty(prop);
    369         if (getName()==NULL || getLongName()==NULL) //name/longname might be unavailable, happened in GenMan sub-Params
     370        if (getName() == NULL || getLongName() == NULL) //name/longname might be unavailable, happened in GenMan sub-Params
    370371                return name_dot_prop;
    371        
     372
    372373        if (strcmp(getName(), getLongName()) == 0)
    373374        {
     
    422423        // t+=SString(getName()); t+=':';
    423424        for (i = 0; p = id(i); i++)
    424                 if ((!((fl = flags(i)) & PARAM_DONTSAVE)) && type(i)[0]!='p')
     425                if ((!((fl = flags(i)) & PARAM_DONTSAVE)) && type(i)[0] != 'p')
    425426                {
    426427                        if (defdata && isequal(i, defdata))
     
    716717int ParamInterface::findGroupId(const char* name)
    717718{
    718 for(int i=0;i<getGroupCount();i++)
    719         if (!strcmp(grname(i),name))
    720                 return i;
    721 return -1;
     719        for (int i = 0; i < getGroupCount(); i++)
     720                if (!strcmp(grname(i), name))
     721                        return i;
     722        return -1;
    722723}
    723724
     
    11311132                        if (x < mn) { x = mn; result = PSET_HITMIN; }
    11321133                        else if (x > mx) { x = mx; result = PSET_HITMAX; }
     1134                        clipNegativeZeroIfNeeded(x, mn);  //by the official standard, 0.0 == -0.0 and so it is not true that -0.0 < 0.0, so -0.0 would not be clipped to 0.0, i.e., if we defined the allowed range to be, for example, [0.0, 1.0], then without this call, x==-0.0 would be accepted. Since we never allow x==-0.0 for mn==0 past this point, we don't need to check below if the value changed from -0.0 to 0.0 (which would require comparing old and new std::signbit(x) etc.), because -0.0 could have never been set earlier.
    11331135                }
    11341136
Note: See TracChangeset for help on using the changeset viewer.