Generic SmartRest library
Dependents: SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming
FloatValue.cpp
00001 /* 00002 * FloatValue.cpp 00003 * 00004 * Created on: Nov 1, 2013 00005 * * Authors: Vincent Wochnik <v.wochnik@gmail.com> 00006 * 00007 * Copyright (c) 2013 Cumulocity GmbH 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining 00010 * a copy of this software and associated documentation files (the 00011 * "Software"), to deal in the Software without restriction, including 00012 * without limitation the rights to use, copy, modify, merge, publish, 00013 * distribute, sublicense, and/or sell copies of the Software, and to 00014 * permit persons to whom the Software is furnished to do so, subject to 00015 * the following conditions: 00016 * 00017 * The above copyright notice and this permission notice shall be 00018 * included in all copies or substantial portions of the Software. 00019 * 00020 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00021 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00022 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00023 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00024 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00025 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00026 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00027 */ 00028 00029 #include "FloatValue.h" 00030 #include <math.h> 00031 00032 FloatValue::FloatValue(double number, uint8_t digits, bool zflag) 00033 { 00034 _negative = (number < 0.0); 00035 _zflag = zflag; 00036 if (_negative) 00037 _number = -number; 00038 else 00039 _number = number; 00040 _digits = digits; 00041 } 00042 00043 uint8_t FloatValue::valueType() const 00044 { 00045 return VALUE_FLOAT; 00046 } 00047 00048 long FloatValue::integerValue() const 00049 { 00050 return 0L; 00051 } 00052 00053 double FloatValue::floatValue() const 00054 { 00055 return (_negative) ? -_number : _number; 00056 } 00057 00058 const char * FloatValue::characterValue() const 00059 { 00060 return 0; 00061 } 00062 00063 size_t FloatValue::write(AbstractDataSink& sink) const 00064 { 00065 size_t n = 0; 00066 00067 if (isnan(_number)) return sink.write("nan"); 00068 if (isinf(_number)) return sink.write("inf"); 00069 if (_number > 4294967040.0) return sink.write("ovf"); 00070 00071 if (_negative) 00072 n += sink.write('-'); 00073 00074 double number = _number, rounding = 0.5; 00075 for (uint8_t i = 0; i < _digits; ++i) 00076 rounding /= 10.0; 00077 number += rounding; 00078 00079 unsigned long int_part = (unsigned long)number; 00080 double remainder = number - (double)int_part; 00081 00082 if ((_zflag) || (int_part)) 00083 n += sink.write(int_part); 00084 00085 if (_digits == 0) 00086 return n; 00087 n += sink.write("."); 00088 00089 uint8_t digits = _digits; 00090 while (digits-- > 0) { 00091 remainder *= 10.0; 00092 unsigned long l = (unsigned long)remainder; 00093 n += sink.write(l); 00094 remainder -= l; 00095 } 00096 00097 return n; 00098 } 00099 00100 size_t FloatValue::length() const 00101 { 00102 if ((isnan(_number)) || (isinf(_number)) || 00103 (_number > 4294967040.0)) 00104 return 3; 00105 00106 size_t l = 0; 00107 00108 if (_negative) 00109 l++; 00110 00111 double number = _number, rounding = 0.5; 00112 for (uint8_t i = 0; i < _digits; ++i) 00113 rounding /= 10.0; 00114 number += rounding; 00115 00116 unsigned long n = (unsigned long)number; 00117 if ((_zflag) || (n)) { 00118 do { 00119 n /= 10; 00120 l++; 00121 } while(n); 00122 } 00123 00124 if (_digits > 0) 00125 l += 1 + _digits; 00126 00127 return l; 00128 } 00129 00130 Value* FloatValue::copy() const 00131 { 00132 double number; 00133 00134 number = (_negative) ? -_number : _number; 00135 return new FloatValue(number, _digits, _zflag); 00136 } 00137
Generated on Wed Jul 13 2022 09:53:06 by 1.7.2