From 552f73d3a5258e76b6995110d2a998fa0a81fcc6 Mon Sep 17 00:00:00 2001 From: Alex Lin Date: Wed, 16 Mar 2016 16:54:25 -0500 Subject: [PATCH] Math with temperature values is not as expected. For addition and subtraction, we created a new routine that only scales second units to the first. We apply it to all conversions when adding and subtracting, but it only changes the temperature results, because that is the only units that has a bias factor. The comparision conversion routines were not changed. refs #202 --- .../trick_swig/swig_convert_units.cpp | 44 +++++++++++++++++++ trick_source/trick_swig/swig_convert_units.hh | 2 + trick_source/trick_swig/swig_double.cpp | 16 +++---- trick_source/trick_swig/swig_int.cpp | 16 +++---- 4 files changed, 62 insertions(+), 16 deletions(-) diff --git a/trick_source/trick_swig/swig_convert_units.cpp b/trick_source/trick_swig/swig_convert_units.cpp index 096c22fc..f8637603 100644 --- a/trick_source/trick_swig/swig_convert_units.cpp +++ b/trick_source/trick_swig/swig_convert_units.cpp @@ -34,3 +34,47 @@ int convert_united_value( std::string & to_units , std::string & from_units , do return 0 ; } +int scale_united_value( std::string & to_units , std::string & from_units , long long * val ) { + + if ( from_units.compare("--") ) { + try { + Units_t * from = new_units(from_units.c_str()) ; + Units_t * to = new_units(to_units.c_str()) ; + UnitsConvFn_t conv_fn; + if (conv_fn_u(from, to, &conv_fn) != CONV_OK) { + throw Unit::CONVERSION_ERROR(); + } + *val = (long long)(conv_fn.C[1] * (*val)) ; + free(from) ; + free(to) ; + } + catch (Unit::CONVERSION_ERROR & ce_err ) { + PyErr_SetString(PyExc_AttributeError,"Units conversion Error"); + return -1 ; + } + } + return 0 ; +} + +int scale_united_value( std::string & to_units , std::string & from_units , double * val ) { + + if ( from_units.compare("--") ) { + try { + Units_t * from = new_units(from_units.c_str()) ; + Units_t * to = new_units(to_units.c_str()) ; + UnitsConvFn_t conv_fn; + if (conv_fn_u(from, to, &conv_fn) != CONV_OK) { + throw Unit::CONVERSION_ERROR(); + } + *val = conv_fn.C[1] * (*val) ; + free(from) ; + free(to) ; + } + catch (Unit::CONVERSION_ERROR & ce_err ) { + PyErr_SetString(PyExc_AttributeError,"Units conversion Error"); + return -1 ; + } + } + return 0 ; +} + diff --git a/trick_source/trick_swig/swig_convert_units.hh b/trick_source/trick_swig/swig_convert_units.hh index ab3cdc86..2ba10947 100644 --- a/trick_source/trick_swig/swig_convert_units.hh +++ b/trick_source/trick_swig/swig_convert_units.hh @@ -6,4 +6,6 @@ int convert_united_value( std::string & to_units , std::string & from_units , long long * val ) ; int convert_united_value( std::string & to_units , std::string & from_units , double * val ) ; +int scale_united_value( std::string & to_units , std::string & from_units , long long * val ) ; +int scale_united_value( std::string & to_units , std::string & from_units , double * val ) ; #endif diff --git a/trick_source/trick_swig/swig_double.cpp b/trick_source/trick_swig/swig_double.cpp index 5b034370..f831b084 100644 --- a/trick_source/trick_swig/swig_double.cpp +++ b/trick_source/trick_swig/swig_double.cpp @@ -46,7 +46,7 @@ PyObject * swig_double::__add__( PyObject * obj1 ) { swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ; new_value = (double)temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_value ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_value ) ; if ( conv_ret == 0 ) { result->value = value + new_value ; } else { @@ -55,7 +55,7 @@ PyObject * swig_double::__add__( PyObject * obj1 ) { } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_double *"), 0 ))) { swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ; new_value = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_value ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_value ) ; if ( conv_ret == 0 ) { result->value = value + new_value ; } else { @@ -86,7 +86,7 @@ PyObject * swig_double::__sub__( PyObject * obj1 ) { swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ; new_value = (double)temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_value ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_value ) ; if ( conv_ret == 0 ) { result->value = value - new_value ; } else { @@ -95,7 +95,7 @@ PyObject * swig_double::__sub__( PyObject * obj1 ) { } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_double *"), 0 ))) { swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ; new_value = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_value ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_value ) ; if ( conv_ret == 0 ) { result->value = value - new_value ; } else { @@ -450,7 +450,7 @@ PyObject * swig_double::__iadd__( PyObject * obj1 ) { } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_int *"), 0 ))) { swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ; new_val = (double)temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { value += new_val ; } else { @@ -459,7 +459,7 @@ PyObject * swig_double::__iadd__( PyObject * obj1 ) { } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_double *"), 0 ))) { swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { value += new_val ; } else { @@ -485,7 +485,7 @@ PyObject * swig_double::__isub__( PyObject * obj1 ) { } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_int *"), 0 ))) { swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { value -= new_val ; } else { @@ -494,7 +494,7 @@ PyObject * swig_double::__isub__( PyObject * obj1 ) { } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_double *"), 0 ))) { swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { value -= new_val ; } else { diff --git a/trick_source/trick_swig/swig_int.cpp b/trick_source/trick_swig/swig_int.cpp index 09e7d8ed..251f1f26 100644 --- a/trick_source/trick_swig/swig_int.cpp +++ b/trick_source/trick_swig/swig_int.cpp @@ -43,7 +43,7 @@ PyObject * swig_int::__add__( PyObject * obj1 ) { long long new_val ; swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { result->value = value + new_val ; } else { @@ -53,7 +53,7 @@ PyObject * swig_int::__add__( PyObject * obj1 ) { double new_val ; swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { result->value = (long long)(round(value + new_val)) ; } else { @@ -83,7 +83,7 @@ PyObject * swig_int::__sub__( PyObject * obj1 ) { long long new_val ; swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { result->value = value - new_val ; } else { @@ -93,7 +93,7 @@ PyObject * swig_int::__sub__( PyObject * obj1 ) { double new_val ; swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { result->value = (long long)(round(value - new_val)) ; } else { @@ -626,7 +626,7 @@ PyObject * swig_int::__iadd__( PyObject * obj1 ) { long long new_val ; swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { value += new_val ; } else { @@ -636,7 +636,7 @@ PyObject * swig_int::__iadd__( PyObject * obj1 ) { double new_val ; swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { value = (long long)round(value + new_val) ; } else { @@ -663,7 +663,7 @@ PyObject * swig_int::__isub__( PyObject * obj1 ) { long long new_val ; swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { value -= new_val ; } else { @@ -673,7 +673,7 @@ PyObject * swig_int::__isub__( PyObject * obj1 ) { double new_val ; swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ; new_val = temp_m->value ; - conv_ret = convert_united_value( units , temp_m->units , &new_val ) ; + conv_ret = scale_united_value( units , temp_m->units , &new_val ) ; if ( conv_ret == 0 ) { value = (long long)round(value - new_val) ; } else {