Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
FloatValue.cpp@11:e1bee9a77652, 2014-11-15 (annotated)
- Committer:
- Cumulocity
- Date:
- Sat Nov 15 11:21:01 2014 +0100
- Revision:
- 11:e1bee9a77652
- Parent:
- 0:099f76422485
Updated from revision ce16ad78546a
Who changed what in which revision?
User | Revision | Line number | New 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 | 11:e1bee9a77652 | 33 | |
Cumulocity | 11:e1bee9a77652 | 34 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 35 | FloatValue::FloatValue(double number, uint8_t digits, bool zflag) |
Cumulocity | 0:099f76422485 | 36 | { |
Cumulocity | 11:e1bee9a77652 | 37 | _negative = (number < 0.0); |
Cumulocity | 11:e1bee9a77652 | 38 | _zflag = zflag; |
Cumulocity | 11:e1bee9a77652 | 39 | if (_negative) |
Cumulocity | 11:e1bee9a77652 | 40 | _number = -number; |
Cumulocity | 11:e1bee9a77652 | 41 | else |
Cumulocity | 11:e1bee9a77652 | 42 | _number = number; |
Cumulocity | 11:e1bee9a77652 | 43 | if (digits == SMARTREST_FLOATVALUE_DETECTPREC) |
Cumulocity | 11:e1bee9a77652 | 44 | _digits = detectPrecision(_number); |
Cumulocity | 11:e1bee9a77652 | 45 | else |
Cumulocity | 11:e1bee9a77652 | 46 | _digits = digits; |
Cumulocity | 0:099f76422485 | 47 | } |
Cumulocity | 11:e1bee9a77652 | 48 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 49 | uint8_t FloatValue::valueType() const |
Cumulocity | 0:099f76422485 | 50 | { |
Cumulocity | 11:e1bee9a77652 | 51 | return VALUE_FLOAT; |
Cumulocity | 0:099f76422485 | 52 | } |
Cumulocity | 11:e1bee9a77652 | 53 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 54 | long FloatValue::integerValue() const |
Cumulocity | 0:099f76422485 | 55 | { |
Cumulocity | 11:e1bee9a77652 | 56 | return 0L; |
Cumulocity | 0:099f76422485 | 57 | } |
Cumulocity | 11:e1bee9a77652 | 58 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 59 | double FloatValue::floatValue() const |
Cumulocity | 0:099f76422485 | 60 | { |
Cumulocity | 11:e1bee9a77652 | 61 | return (_negative) ? -_number : _number; |
Cumulocity | 0:099f76422485 | 62 | } |
Cumulocity | 11:e1bee9a77652 | 63 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 64 | const char * FloatValue::characterValue() const |
Cumulocity | 0:099f76422485 | 65 | { |
Cumulocity | 11:e1bee9a77652 | 66 | return 0; |
Cumulocity | 0:099f76422485 | 67 | } |
Cumulocity | 11:e1bee9a77652 | 68 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 69 | size_t FloatValue::write(AbstractDataSink& sink) const |
Cumulocity | 0:099f76422485 | 70 | { |
Cumulocity | 11:e1bee9a77652 | 71 | size_t n = 0; |
Cumulocity | 11:e1bee9a77652 | 72 | double number, rounding; |
Cumulocity | 11:e1bee9a77652 | 73 | uint8_t i; |
Cumulocity | 11:e1bee9a77652 | 74 | unsigned long int_part; |
Cumulocity | 11:e1bee9a77652 | 75 | double remainder; |
Cumulocity | 11:e1bee9a77652 | 76 | |
Cumulocity | 11:e1bee9a77652 | 77 | if (isnan(_number)) |
Cumulocity | 11:e1bee9a77652 | 78 | return sink.write("nan"); |
Cumulocity | 0:099f76422485 | 79 | |
Cumulocity | 11:e1bee9a77652 | 80 | if (_negative) |
Cumulocity | 11:e1bee9a77652 | 81 | n += sink.write('-'); |
Cumulocity | 0:099f76422485 | 82 | |
Cumulocity | 11:e1bee9a77652 | 83 | if (isinf(_number)) |
Cumulocity | 11:e1bee9a77652 | 84 | { |
Cumulocity | 11:e1bee9a77652 | 85 | n += sink.write("inf"); |
Cumulocity | 11:e1bee9a77652 | 86 | return n; |
Cumulocity | 11:e1bee9a77652 | 87 | } |
Cumulocity | 0:099f76422485 | 88 | |
Cumulocity | 11:e1bee9a77652 | 89 | number = _number; |
Cumulocity | 11:e1bee9a77652 | 90 | rounding = 0.5; |
Cumulocity | 11:e1bee9a77652 | 91 | for (i = 0; i < _digits; ++i) |
Cumulocity | 11:e1bee9a77652 | 92 | rounding /= 10.0; |
Cumulocity | 11:e1bee9a77652 | 93 | number += rounding; |
Cumulocity | 0:099f76422485 | 94 | |
Cumulocity | 11:e1bee9a77652 | 95 | int_part = (unsigned long)number; |
Cumulocity | 11:e1bee9a77652 | 96 | remainder = number - (double)int_part; |
Cumulocity | 0:099f76422485 | 97 | |
Cumulocity | 11:e1bee9a77652 | 98 | if ((_zflag) || (int_part)) |
Cumulocity | 11:e1bee9a77652 | 99 | n += sink.write(int_part); |
Cumulocity | 0:099f76422485 | 100 | |
Cumulocity | 11:e1bee9a77652 | 101 | if (_digits == 0) |
Cumulocity | 11:e1bee9a77652 | 102 | return n; |
Cumulocity | 11:e1bee9a77652 | 103 | n += sink.write("."); |
Cumulocity | 0:099f76422485 | 104 | |
Cumulocity | 11:e1bee9a77652 | 105 | uint8_t digits = _digits; |
Cumulocity | 11:e1bee9a77652 | 106 | while (digits-- > 0) |
Cumulocity | 11:e1bee9a77652 | 107 | { |
Cumulocity | 11:e1bee9a77652 | 108 | remainder *= 10.0; |
Cumulocity | 11:e1bee9a77652 | 109 | unsigned long l = (unsigned long)remainder; |
Cumulocity | 11:e1bee9a77652 | 110 | n += sink.write(l); |
Cumulocity | 11:e1bee9a77652 | 111 | remainder -= l; |
Cumulocity | 11:e1bee9a77652 | 112 | } |
Cumulocity | 0:099f76422485 | 113 | |
Cumulocity | 11:e1bee9a77652 | 114 | return n; |
Cumulocity | 0:099f76422485 | 115 | } |
Cumulocity | 11:e1bee9a77652 | 116 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 117 | size_t FloatValue::length() const |
Cumulocity | 0:099f76422485 | 118 | { |
Cumulocity | 11:e1bee9a77652 | 119 | size_t l = 0; |
Cumulocity | 11:e1bee9a77652 | 120 | double number, rounding; |
Cumulocity | 11:e1bee9a77652 | 121 | unsigned long n; |
Cumulocity | 11:e1bee9a77652 | 122 | uint8_t i; |
Cumulocity | 0:099f76422485 | 123 | |
Cumulocity | 11:e1bee9a77652 | 124 | if (isnan(_number)) |
Cumulocity | 11:e1bee9a77652 | 125 | return 3; |
Cumulocity | 0:099f76422485 | 126 | |
Cumulocity | 11:e1bee9a77652 | 127 | if (_negative) |
Cumulocity | 11:e1bee9a77652 | 128 | l++; |
Cumulocity | 0:099f76422485 | 129 | |
Cumulocity | 11:e1bee9a77652 | 130 | if (isinf(_number)) |
Cumulocity | 11:e1bee9a77652 | 131 | { |
Cumulocity | 11:e1bee9a77652 | 132 | l += 3; |
Cumulocity | 11:e1bee9a77652 | 133 | return l; |
Cumulocity | 11:e1bee9a77652 | 134 | } |
Cumulocity | 0:099f76422485 | 135 | |
Cumulocity | 11:e1bee9a77652 | 136 | number = _number; |
Cumulocity | 11:e1bee9a77652 | 137 | rounding = 0.5; |
Cumulocity | 11:e1bee9a77652 | 138 | for (i = 0; i < _digits; ++i) |
Cumulocity | 11:e1bee9a77652 | 139 | rounding /= 10.0; |
Cumulocity | 11:e1bee9a77652 | 140 | number += rounding; |
Cumulocity | 0:099f76422485 | 141 | |
Cumulocity | 11:e1bee9a77652 | 142 | n = (unsigned long)number; |
Cumulocity | 11:e1bee9a77652 | 143 | if ((_zflag) || (n)) |
Cumulocity | 11:e1bee9a77652 | 144 | { |
Cumulocity | 11:e1bee9a77652 | 145 | do |
Cumulocity | 11:e1bee9a77652 | 146 | { |
Cumulocity | 11:e1bee9a77652 | 147 | n /= 10; |
Cumulocity | 11:e1bee9a77652 | 148 | l++; |
Cumulocity | 11:e1bee9a77652 | 149 | } while(n); |
Cumulocity | 11:e1bee9a77652 | 150 | } |
Cumulocity | 0:099f76422485 | 151 | |
Cumulocity | 11:e1bee9a77652 | 152 | if (_digits > 0) |
Cumulocity | 11:e1bee9a77652 | 153 | l += 1 + _digits; |
Cumulocity | 0:099f76422485 | 154 | |
Cumulocity | 11:e1bee9a77652 | 155 | return l; |
Cumulocity | 0:099f76422485 | 156 | } |
Cumulocity | 11:e1bee9a77652 | 157 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 158 | Value* FloatValue::copy() const |
Cumulocity | 0:099f76422485 | 159 | { |
Cumulocity | 11:e1bee9a77652 | 160 | double number; |
Cumulocity | 0:099f76422485 | 161 | |
Cumulocity | 11:e1bee9a77652 | 162 | number = (_negative) ? -_number : _number; |
Cumulocity | 11:e1bee9a77652 | 163 | return new FloatValue(number, _digits, _zflag); |
Cumulocity | 0:099f76422485 | 164 | } |
Cumulocity | 11:e1bee9a77652 | 165 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 166 | uint8_t FloatValue::detectPrecision(double n) |
Cumulocity | 0:099f76422485 | 167 | { |
Cumulocity | 11:e1bee9a77652 | 168 | uint8_t prec = 0, count = 0; |
Cumulocity | 11:e1bee9a77652 | 169 | int8_t d; |
Cumulocity | 0:099f76422485 | 170 | |
Cumulocity | 11:e1bee9a77652 | 171 | if ((isinf(n)) || (isnan(n))) |
Cumulocity | 11:e1bee9a77652 | 172 | return 0; |
Cumulocity | 0:099f76422485 | 173 | |
Cumulocity | 11:e1bee9a77652 | 174 | n -= (long)n; |
Cumulocity | 0:099f76422485 | 175 | |
Cumulocity | 11:e1bee9a77652 | 176 | while ((prec < 6) && (count < 3)) |
Cumulocity | 11:e1bee9a77652 | 177 | { |
Cumulocity | 11:e1bee9a77652 | 178 | n *= 10; |
Cumulocity | 11:e1bee9a77652 | 179 | d = (uint8_t)n; |
Cumulocity | 0:099f76422485 | 180 | |
Cumulocity | 11:e1bee9a77652 | 181 | if (count == 0) |
Cumulocity | 11:e1bee9a77652 | 182 | { |
Cumulocity | 11:e1bee9a77652 | 183 | if ((d <= 0) || (d >= 9)) |
Cumulocity | 11:e1bee9a77652 | 184 | count++; |
Cumulocity | 11:e1bee9a77652 | 185 | } |
Cumulocity | 11:e1bee9a77652 | 186 | else |
Cumulocity | 11:e1bee9a77652 | 187 | { |
Cumulocity | 11:e1bee9a77652 | 188 | count++; |
Cumulocity | 11:e1bee9a77652 | 189 | } |
Cumulocity | 11:e1bee9a77652 | 190 | prec++; |
Cumulocity | 11:e1bee9a77652 | 191 | n -= d; |
Cumulocity | 11:e1bee9a77652 | 192 | } |
Cumulocity | 0:099f76422485 | 193 | |
Cumulocity | 11:e1bee9a77652 | 194 | return --prec; |
Cumulocity | 0:099f76422485 | 195 | } |
Cumulocity | 11:e1bee9a77652 | 196 | /*-------------------------------------------------------------------------*/ |