Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
Cumulocity
Date:
Thu Jul 03 20:38:04 2014 +0200
Revision:
0:099f76422485
Child:
11:e1bee9a77652
Updated from revision 0413a0179eb6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * FloatValue.cpp
Cumulocity 0:099f76422485 3 *
Cumulocity 0:099f76422485 4 * Created on: Nov 1, 2013
Cumulocity 0:099f76422485 5 * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
Cumulocity 0:099f76422485 6 *
Cumulocity 0:099f76422485 7 * Copyright (c) 2013 Cumulocity GmbH
Cumulocity 0:099f76422485 8 *
Cumulocity 0:099f76422485 9 * Permission is hereby granted, free of charge, to any person obtaining
Cumulocity 0:099f76422485 10 * a copy of this software and associated documentation files (the
Cumulocity 0:099f76422485 11 * "Software"), to deal in the Software without restriction, including
Cumulocity 0:099f76422485 12 * without limitation the rights to use, copy, modify, merge, publish,
Cumulocity 0:099f76422485 13 * distribute, sublicense, and/or sell copies of the Software, and to
Cumulocity 0:099f76422485 14 * permit persons to whom the Software is furnished to do so, subject to
Cumulocity 0:099f76422485 15 * the following conditions:
Cumulocity 0:099f76422485 16 *
Cumulocity 0:099f76422485 17 * The above copyright notice and this permission notice shall be
Cumulocity 0:099f76422485 18 * included in all copies or substantial portions of the Software.
Cumulocity 0:099f76422485 19 *
Cumulocity 0:099f76422485 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Cumulocity 0:099f76422485 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Cumulocity 0:099f76422485 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Cumulocity 0:099f76422485 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Cumulocity 0:099f76422485 24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Cumulocity 0:099f76422485 25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Cumulocity 0:099f76422485 26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Cumulocity 0:099f76422485 27 */
Cumulocity 0:099f76422485 28
Cumulocity 0:099f76422485 29 #include "FloatValue.h"
Cumulocity 0:099f76422485 30 #include <math.h>
Cumulocity 0:099f76422485 31 #include <stdio.h>
Cumulocity 0:099f76422485 32
Cumulocity 0:099f76422485 33 FloatValue::FloatValue(double number, uint8_t digits, bool zflag)
Cumulocity 0:099f76422485 34 {
Cumulocity 0:099f76422485 35 _negative = (number < 0.0);
Cumulocity 0:099f76422485 36 _zflag = zflag;
Cumulocity 0:099f76422485 37 if (_negative)
Cumulocity 0:099f76422485 38 _number = -number;
Cumulocity 0:099f76422485 39 else
Cumulocity 0:099f76422485 40 _number = number;
Cumulocity 0:099f76422485 41 if (digits == SMARTREST_FLOATVALUE_DETECTPREC)
Cumulocity 0:099f76422485 42 _digits = detectPrecision(_number);
Cumulocity 0:099f76422485 43 else
Cumulocity 0:099f76422485 44 _digits = digits;
Cumulocity 0:099f76422485 45 }
Cumulocity 0:099f76422485 46
Cumulocity 0:099f76422485 47 uint8_t FloatValue::valueType() const
Cumulocity 0:099f76422485 48 {
Cumulocity 0:099f76422485 49 return VALUE_FLOAT;
Cumulocity 0:099f76422485 50 }
Cumulocity 0:099f76422485 51
Cumulocity 0:099f76422485 52 long FloatValue::integerValue() const
Cumulocity 0:099f76422485 53 {
Cumulocity 0:099f76422485 54 return 0L;
Cumulocity 0:099f76422485 55 }
Cumulocity 0:099f76422485 56
Cumulocity 0:099f76422485 57 double FloatValue::floatValue() const
Cumulocity 0:099f76422485 58 {
Cumulocity 0:099f76422485 59 return (_negative) ? -_number : _number;
Cumulocity 0:099f76422485 60 }
Cumulocity 0:099f76422485 61
Cumulocity 0:099f76422485 62 const char * FloatValue::characterValue() const
Cumulocity 0:099f76422485 63 {
Cumulocity 0:099f76422485 64 return 0;
Cumulocity 0:099f76422485 65 }
Cumulocity 0:099f76422485 66
Cumulocity 0:099f76422485 67 size_t FloatValue::write(AbstractDataSink& sink) const
Cumulocity 0:099f76422485 68 {
Cumulocity 0:099f76422485 69 size_t n = 0;
Cumulocity 0:099f76422485 70 double number, rounding;
Cumulocity 0:099f76422485 71 uint8_t i;
Cumulocity 0:099f76422485 72 unsigned long int_part;
Cumulocity 0:099f76422485 73 double remainder;
Cumulocity 0:099f76422485 74
Cumulocity 0:099f76422485 75 if (isnan(_number))
Cumulocity 0:099f76422485 76 return sink.write("nan");
Cumulocity 0:099f76422485 77
Cumulocity 0:099f76422485 78 if (_negative)
Cumulocity 0:099f76422485 79 n += sink.write('-');
Cumulocity 0:099f76422485 80
Cumulocity 0:099f76422485 81 if (isinf(_number)) {
Cumulocity 0:099f76422485 82 n += sink.write("inf");
Cumulocity 0:099f76422485 83 return n;
Cumulocity 0:099f76422485 84 }
Cumulocity 0:099f76422485 85
Cumulocity 0:099f76422485 86 number = _number;
Cumulocity 0:099f76422485 87 rounding = 0.5;
Cumulocity 0:099f76422485 88 for (i = 0; i < _digits; ++i)
Cumulocity 0:099f76422485 89 rounding /= 10.0;
Cumulocity 0:099f76422485 90 number += rounding;
Cumulocity 0:099f76422485 91
Cumulocity 0:099f76422485 92 int_part = (unsigned long)number;
Cumulocity 0:099f76422485 93 remainder = number - (double)int_part;
Cumulocity 0:099f76422485 94
Cumulocity 0:099f76422485 95 if ((_zflag) || (int_part))
Cumulocity 0:099f76422485 96 n += sink.write(int_part);
Cumulocity 0:099f76422485 97
Cumulocity 0:099f76422485 98 if (_digits == 0)
Cumulocity 0:099f76422485 99 return n;
Cumulocity 0:099f76422485 100 n += sink.write(".");
Cumulocity 0:099f76422485 101
Cumulocity 0:099f76422485 102 uint8_t digits = _digits;
Cumulocity 0:099f76422485 103 while (digits-- > 0) {
Cumulocity 0:099f76422485 104 remainder *= 10.0;
Cumulocity 0:099f76422485 105 unsigned long l = (unsigned long)remainder;
Cumulocity 0:099f76422485 106 n += sink.write(l);
Cumulocity 0:099f76422485 107 remainder -= l;
Cumulocity 0:099f76422485 108 }
Cumulocity 0:099f76422485 109
Cumulocity 0:099f76422485 110 return n;
Cumulocity 0:099f76422485 111 }
Cumulocity 0:099f76422485 112
Cumulocity 0:099f76422485 113 size_t FloatValue::length() const
Cumulocity 0:099f76422485 114 {
Cumulocity 0:099f76422485 115 size_t l = 0;
Cumulocity 0:099f76422485 116 double number, rounding;
Cumulocity 0:099f76422485 117 unsigned long n;
Cumulocity 0:099f76422485 118 uint8_t i;
Cumulocity 0:099f76422485 119
Cumulocity 0:099f76422485 120 if (isnan(_number))
Cumulocity 0:099f76422485 121 return 3;
Cumulocity 0:099f76422485 122
Cumulocity 0:099f76422485 123 if (_negative)
Cumulocity 0:099f76422485 124 l++;
Cumulocity 0:099f76422485 125
Cumulocity 0:099f76422485 126 if (isinf(_number)) {
Cumulocity 0:099f76422485 127 l += 3;
Cumulocity 0:099f76422485 128 return l;
Cumulocity 0:099f76422485 129 }
Cumulocity 0:099f76422485 130
Cumulocity 0:099f76422485 131 number = _number;
Cumulocity 0:099f76422485 132 rounding = 0.5;
Cumulocity 0:099f76422485 133 for (i = 0; i < _digits; ++i)
Cumulocity 0:099f76422485 134 rounding /= 10.0;
Cumulocity 0:099f76422485 135 number += rounding;
Cumulocity 0:099f76422485 136
Cumulocity 0:099f76422485 137 n = (unsigned long)number;
Cumulocity 0:099f76422485 138 if ((_zflag) || (n)) {
Cumulocity 0:099f76422485 139 do {
Cumulocity 0:099f76422485 140 n /= 10;
Cumulocity 0:099f76422485 141 l++;
Cumulocity 0:099f76422485 142 } while(n);
Cumulocity 0:099f76422485 143 }
Cumulocity 0:099f76422485 144
Cumulocity 0:099f76422485 145 if (_digits > 0)
Cumulocity 0:099f76422485 146 l += 1 + _digits;
Cumulocity 0:099f76422485 147
Cumulocity 0:099f76422485 148 return l;
Cumulocity 0:099f76422485 149 }
Cumulocity 0:099f76422485 150
Cumulocity 0:099f76422485 151 Value* FloatValue::copy() const
Cumulocity 0:099f76422485 152 {
Cumulocity 0:099f76422485 153 double number;
Cumulocity 0:099f76422485 154
Cumulocity 0:099f76422485 155 number = (_negative) ? -_number : _number;
Cumulocity 0:099f76422485 156 return new FloatValue(number, _digits, _zflag);
Cumulocity 0:099f76422485 157 }
Cumulocity 0:099f76422485 158
Cumulocity 0:099f76422485 159 uint8_t FloatValue::detectPrecision(double n)
Cumulocity 0:099f76422485 160 {
Cumulocity 0:099f76422485 161 uint8_t prec = 0, count = 0;
Cumulocity 0:099f76422485 162 int8_t d;
Cumulocity 0:099f76422485 163
Cumulocity 0:099f76422485 164 if ((isinf(n)) || (isnan(n)))
Cumulocity 0:099f76422485 165 return 0;
Cumulocity 0:099f76422485 166
Cumulocity 0:099f76422485 167 n -= (long)n;
Cumulocity 0:099f76422485 168
Cumulocity 0:099f76422485 169 while ((prec < 6) && (count < 3)) {
Cumulocity 0:099f76422485 170 n *= 10;
Cumulocity 0:099f76422485 171 d = (uint8_t)n;
Cumulocity 0:099f76422485 172
Cumulocity 0:099f76422485 173 if (count == 0) {
Cumulocity 0:099f76422485 174 if ((d <= 0) || (d >= 9))
Cumulocity 0:099f76422485 175 count++;
Cumulocity 0:099f76422485 176 } else {
Cumulocity 0:099f76422485 177 count++;
Cumulocity 0:099f76422485 178 }
Cumulocity 0:099f76422485 179 prec++;
Cumulocity 0:099f76422485 180 n -= d;
Cumulocity 0:099f76422485 181 }
Cumulocity 0:099f76422485 182
Cumulocity 0:099f76422485 183 return --prec;
Cumulocity 0:099f76422485 184 }