ソースを参照

Do rounding in integer (instead of FIXFLOAT)

Co-Authored-By: Bob-the-Kuhn <bob-the-kuhn@users.noreply.github.com>
Scott Lahteine 7年前
コミット
63e4afc910
3個のファイルの変更110行の追加113行の削除
  1. 1
    1
      Marlin/src/core/macros.h
  2. 107
    110
      Marlin/src/core/utility.cpp
  3. 2
    2
      Marlin/src/core/utility.h

+ 1
- 1
Marlin/src/core/macros.h ファイルの表示

@@ -223,7 +223,7 @@
223 223
 #define NEAR(x,y) NEAR_ZERO((x)-(y))
224 224
 
225 225
 #define RECIPROCAL(x) (NEAR_ZERO(x) ? 0.0 : 1.0 / (x))
226
-#define FIXFLOAT(f) (f + (f < 0.0 ? -0.00001 : 0.00001))
226
+#define FIXFLOAT(f) (f + (f < 0.0 ? -0.00005 : 0.00005))
227 227
 
228 228
 //
229 229
 // Maths macros that can be overridden by HAL

+ 107
- 110
Marlin/src/core/utility.cpp ファイルの表示

@@ -41,7 +41,7 @@ void safe_delay(millis_t ms) {
41 41
     uint8_t *ptr = (uint8_t *)data;
42 42
     while (cnt--) {
43 43
       *crc = (uint16_t)(*crc ^ (uint16_t)(((uint16_t)*ptr++) << 8));
44
-      for (uint8_t x = 0; x < 8; x++)
44
+      for (uint8_t i = 0; i < 8; i++)
45 45
         *crc = (uint16_t)((*crc & 0x8000) ? ((uint16_t)(*crc << 1) ^ 0x1021) : (*crc << 1));
46 46
     }
47 47
   }
@@ -58,193 +58,190 @@ void safe_delay(millis_t ms) {
58 58
   #define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
59 59
 
60 60
   // Convert unsigned int to string 123 format
61
-  char* i8tostr3(const uint8_t xx) {
62
-    conv[4] = RJDIGIT(xx, 100);
63
-    conv[5] = RJDIGIT(xx, 10);
64
-    conv[6] = DIGIMOD(xx, 1);
61
+  char* i8tostr3(const uint8_t i) {
62
+    conv[4] = RJDIGIT(i, 100);
63
+    conv[5] = RJDIGIT(i, 10);
64
+    conv[6] = DIGIMOD(i, 1);
65 65
     return &conv[4];
66 66
   }
67 67
 
68 68
   // Convert signed int to rj string with 123 or -12 format
69
-  char* itostr3(const int x) {
70
-    int xx = x;
71
-    conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
72
-    conv[5] = RJDIGIT(xx, 10);
73
-    conv[6] = DIGIMOD(xx, 1);
69
+  char* itostr3(int i) {
70
+    conv[4] = MINUSOR(i, RJDIGIT(i, 100));
71
+    conv[5] = RJDIGIT(i, 10);
72
+    conv[6] = DIGIMOD(i, 1);
74 73
     return &conv[4];
75 74
   }
76 75
 
77 76
   // Convert unsigned int to lj string with 123 format
78
-  char* itostr3left(const int xx) {
77
+  char* itostr3left(const int i) {
79 78
     char *str = &conv[6];
80
-    *str = DIGIMOD(xx, 1);
81
-    if (xx >= 10) {
82
-      *(--str) = DIGIMOD(xx, 10);
83
-      if (xx >= 100)
84
-        *(--str) = DIGIMOD(xx, 100);
79
+    *str = DIGIMOD(i, 1);
80
+    if (i >= 10) {
81
+      *(--str) = DIGIMOD(i, 10);
82
+      if (i >= 100)
83
+        *(--str) = DIGIMOD(i, 100);
85 84
     }
86 85
     return str;
87 86
   }
88 87
 
89 88
   // Convert signed int to rj string with 1234, _123, -123, _-12, or __-1 format
90
-  char* itostr4sign(const int x) {
91
-    const bool neg = x < 0;
92
-    const int xx = neg ? -x : x;
93
-    if (x >= 1000) {
94
-      conv[3] = DIGIMOD(xx, 1000);
95
-      conv[4] = DIGIMOD(xx, 100);
96
-      conv[5] = DIGIMOD(xx, 10);
89
+  char* itostr4sign(const int i) {
90
+    const bool neg = i < 0;
91
+    const int ii = neg ? -i : i;
92
+    if (i >= 1000) {
93
+      conv[3] = DIGIMOD(ii, 1000);
94
+      conv[4] = DIGIMOD(ii, 100);
95
+      conv[5] = DIGIMOD(ii, 10);
96
+    }
97
+    else if (ii >= 100) {
98
+      conv[3] = neg ? '-' : ' ';
99
+      conv[4] = DIGIMOD(ii, 100);
100
+      conv[5] = DIGIMOD(ii, 10);
97 101
     }
98 102
     else {
99
-      if (xx >= 100) {
100
-        conv[3] = neg ? '-' : ' ';
101
-        conv[4] = DIGIMOD(xx, 100);
102
-        conv[5] = DIGIMOD(xx, 10);
103
+      conv[3] = ' ';
104
+      conv[4] = ' ';
105
+      if (ii >= 10) {
106
+        conv[4] = neg ? '-' : ' ';
107
+        conv[5] = DIGIMOD(ii, 10);
103 108
       }
104 109
       else {
105
-        conv[3] = ' ';
106
-        conv[4] = ' ';
107
-        if (xx >= 10) {
108
-          conv[4] = neg ? '-' : ' ';
109
-          conv[5] = DIGIMOD(xx, 10);
110
-        }
111
-        else {
112
-          conv[5] = neg ? '-' : ' ';
113
-        }
110
+        conv[5] = neg ? '-' : ' ';
114 111
       }
115 112
     }
116
-    conv[6] = DIGIMOD(xx, 1);
113
+    conv[6] = DIGIMOD(ii, 1);
117 114
     return &conv[3];
118 115
   }
119 116
 
120 117
   // Convert unsigned float to string with 1.23 format
121
-  char* ftostr12ns(const float &x) {
122
-    const long xx = ((x < 0 ? -x : x) + 0.001) * 100;
123
-    conv[3] = DIGIMOD(xx, 100);
118
+  char* ftostr12ns(const float &f) {
119
+    const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
120
+    conv[3] = DIGIMOD(i, 100);
124 121
     conv[4] = '.';
125
-    conv[5] = DIGIMOD(xx, 10);
126
-    conv[6] = DIGIMOD(xx, 1);
122
+    conv[5] = DIGIMOD(i, 10);
123
+    conv[6] = DIGIMOD(i, 1);
127 124
     return &conv[3];
128 125
   }
129 126
 
130 127
   // Convert signed float to fixed-length string with 023.45 / -23.45 format
131
-  char* ftostr32(const float &x) {
132
-    long xx = FIXFLOAT(x) * 100;
133
-    conv[1] = MINUSOR(xx, DIGIMOD(xx, 10000));
134
-    conv[2] = DIGIMOD(xx, 1000);
135
-    conv[3] = DIGIMOD(xx, 100);
128
+  char* ftostr32(const float &f) {
129
+    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
130
+    conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
131
+    conv[2] = DIGIMOD(i, 1000);
132
+    conv[3] = DIGIMOD(i, 100);
136 133
     conv[4] = '.';
137
-    conv[5] = DIGIMOD(xx, 10);
138
-    conv[6] = DIGIMOD(xx, 1);
134
+    conv[5] = DIGIMOD(i, 10);
135
+    conv[6] = DIGIMOD(i, 1);
139 136
     return &conv[1];
140 137
   }
141 138
 
142 139
   #if ENABLED(LCD_DECIMAL_SMALL_XY)
143 140
 
144 141
     // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
145
-    char* ftostr4sign(const float &fx) {
146
-      const int x = FIXFLOAT(fx) * 10;
147
-      if (!WITHIN(x, -99, 999)) return itostr4sign((int)fx);
148
-      const bool neg = x < 0;
149
-      const int xx = neg ? -x : x;
150
-      conv[3] = neg ? '-' : (xx >= 100 ? DIGIMOD(xx, 100) : ' ');
151
-      conv[4] = DIGIMOD(xx, 10);
142
+    char* ftostr4sign(const float &f) {
143
+      const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
144
+      if (!WITHIN(i, -99, 999)) return itostr4sign((int)f);
145
+      const bool neg = i < 0;
146
+      const int ii = neg ? -i : i;
147
+      conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
148
+      conv[4] = DIGIMOD(ii, 10);
152 149
       conv[5] = '.';
153
-      conv[6] = DIGIMOD(xx, 1);
150
+      conv[6] = DIGIMOD(ii, 1);
154 151
       return &conv[3];
155 152
     }
156 153
 
157 154
   #endif // LCD_DECIMAL_SMALL_XY
158 155
 
159 156
   // Convert float to fixed-length string with +123.4 / -123.4 format
160
-  char* ftostr41sign(const float &x) {
161
-    int xx = FIXFLOAT(x) * 10;
162
-    conv[1] = MINUSOR(xx, '+');
163
-    conv[2] = DIGIMOD(xx, 1000);
164
-    conv[3] = DIGIMOD(xx, 100);
165
-    conv[4] = DIGIMOD(xx, 10);
157
+  char* ftostr41sign(const float &f) {
158
+    int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
159
+    conv[1] = MINUSOR(i, '+');
160
+    conv[2] = DIGIMOD(i, 1000);
161
+    conv[3] = DIGIMOD(i, 100);
162
+    conv[4] = DIGIMOD(i, 10);
166 163
     conv[5] = '.';
167
-    conv[6] = DIGIMOD(xx, 1);
164
+    conv[6] = DIGIMOD(i, 1);
168 165
     return &conv[1];
169 166
   }
170 167
 
171 168
   // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
172
-  char* ftostr43sign(const float &x, char plus/*=' '*/) {
173
-    long xx = FIXFLOAT(x) * 1000;
174
-    conv[1] = xx ? MINUSOR(xx, plus) : ' ';
175
-    conv[2] = DIGIMOD(xx, 1000);
169
+  char* ftostr43sign(const float &f, char plus/*=' '*/) {
170
+    long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
171
+    conv[1] = i ? MINUSOR(i, plus) : ' ';
172
+    conv[2] = DIGIMOD(i, 1000);
176 173
     conv[3] = '.';
177
-    conv[4] = DIGIMOD(xx, 100);
178
-    conv[5] = DIGIMOD(xx, 10);
179
-    conv[6] = DIGIMOD(xx, 1);
174
+    conv[4] = DIGIMOD(i, 100);
175
+    conv[5] = DIGIMOD(i, 10);
176
+    conv[6] = DIGIMOD(i, 1);
180 177
     return &conv[1];
181 178
   }
182 179
 
183 180
   // Convert unsigned float to rj string with 12345 format
184
-  char* ftostr5rj(const float &x) {
185
-    const long xx = x < 0 ? -x : x;
186
-    conv[2] = RJDIGIT(xx, 10000);
187
-    conv[3] = RJDIGIT(xx, 1000);
188
-    conv[4] = RJDIGIT(xx, 100);
189
-    conv[5] = RJDIGIT(xx, 10);
190
-    conv[6] = DIGIMOD(xx, 1);
181
+  char* ftostr5rj(const float &f) {
182
+    const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
183
+    conv[2] = RJDIGIT(i, 10000);
184
+    conv[3] = RJDIGIT(i, 1000);
185
+    conv[4] = RJDIGIT(i, 100);
186
+    conv[5] = RJDIGIT(i, 10);
187
+    conv[6] = DIGIMOD(i, 1);
191 188
     return &conv[2];
192 189
   }
193 190
 
194 191
   // Convert signed float to string with +1234.5 format
195
-  char* ftostr51sign(const float &x) {
196
-    long xx = FIXFLOAT(x) * 10;
197
-    conv[0] = MINUSOR(xx, '+');
198
-    conv[1] = DIGIMOD(xx, 10000);
199
-    conv[2] = DIGIMOD(xx, 1000);
200
-    conv[3] = DIGIMOD(xx, 100);
201
-    conv[4] = DIGIMOD(xx, 10);
192
+  char* ftostr51sign(const float &f) {
193
+    long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
194
+    conv[0] = MINUSOR(i, '+');
195
+    conv[1] = DIGIMOD(i, 10000);
196
+    conv[2] = DIGIMOD(i, 1000);
197
+    conv[3] = DIGIMOD(i, 100);
198
+    conv[4] = DIGIMOD(i, 10);
202 199
     conv[5] = '.';
203
-    conv[6] = DIGIMOD(xx, 1);
200
+    conv[6] = DIGIMOD(i, 1);
204 201
     return conv;
205 202
   }
206 203
 
207 204
   // Convert signed float to string with +123.45 format
208
-  char* ftostr52sign(const float &x) {
209
-    long xx = FIXFLOAT(x) * 100;
210
-    conv[0] = MINUSOR(xx, '+');
211
-    conv[1] = DIGIMOD(xx, 10000);
212
-    conv[2] = DIGIMOD(xx, 1000);
213
-    conv[3] = DIGIMOD(xx, 100);
205
+  char* ftostr52sign(const float &f) {
206
+    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
207
+    conv[0] = MINUSOR(i, '+');
208
+    conv[1] = DIGIMOD(i, 10000);
209
+    conv[2] = DIGIMOD(i, 1000);
210
+    conv[3] = DIGIMOD(i, 100);
214 211
     conv[4] = '.';
215
-    conv[5] = DIGIMOD(xx, 10);
216
-    conv[6] = DIGIMOD(xx, 1);
212
+    conv[5] = DIGIMOD(i, 10);
213
+    conv[6] = DIGIMOD(i, 1);
217 214
     return conv;
218 215
   }
219 216
 
220 217
   // Convert unsigned float to string with 1234.56 format omitting trailing zeros
221
-  char* ftostr62rj(const float &x) {
222
-    const long xx = ((x < 0 ? -x : x) + 0.001) * 100;
223
-    conv[0] = RJDIGIT(xx, 100000);
224
-    conv[1] = RJDIGIT(xx, 10000);
225
-    conv[2] = RJDIGIT(xx, 1000);
226
-    conv[3] = DIGIMOD(xx, 100);
218
+  char* ftostr62rj(const float &f) {
219
+    const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
220
+    conv[0] = RJDIGIT(i, 100000);
221
+    conv[1] = RJDIGIT(i, 10000);
222
+    conv[2] = RJDIGIT(i, 1000);
223
+    conv[3] = DIGIMOD(i, 100);
227 224
     conv[4] = '.';
228
-    conv[5] = DIGIMOD(xx, 10);
229
-    conv[6] = DIGIMOD(xx, 1);
225
+    conv[5] = DIGIMOD(i, 10);
226
+    conv[6] = DIGIMOD(i, 1);
230 227
     return conv;
231 228
   }
232 229
 
233 230
   // Convert signed float to space-padded string with -_23.4_ format
234
-  char* ftostr52sp(const float &x) {
235
-    long xx = FIXFLOAT(x) * 100;
231
+  char* ftostr52sp(const float &f) {
232
+    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
236 233
     uint8_t dig;
237
-    conv[1] = MINUSOR(xx, RJDIGIT(xx, 10000));
238
-    conv[2] = RJDIGIT(xx, 1000);
239
-    conv[3] = DIGIMOD(xx, 100);
234
+    conv[1] = MINUSOR(i, RJDIGIT(i, 10000));
235
+    conv[2] = RJDIGIT(i, 1000);
236
+    conv[3] = DIGIMOD(i, 100);
240 237
 
241
-    if ((dig = xx % 10)) {          // second digit after decimal point?
238
+    if ((dig = i % 10)) {          // second digit after decimal point?
242 239
       conv[4] = '.';
243
-      conv[5] = DIGIMOD(xx, 10);
240
+      conv[5] = DIGIMOD(i, 10);
244 241
       conv[6] = DIGIT(dig);
245 242
     }
246 243
     else {
247
-      if ((dig = (xx / 10) % 10)) { // first digit after decimal point?
244
+      if ((dig = (i / 10) % 10)) { // first digit after decimal point?
248 245
         conv[4] = '.';
249 246
         conv[5] = DIGIT(dig);
250 247
       }

+ 2
- 2
Marlin/src/core/utility.h ファイルの表示

@@ -87,14 +87,14 @@ void safe_delay(millis_t ms);
87 87
   char* ftostr62rj(const float &x);
88 88
 
89 89
   // Convert float to rj string with 123 or -12 format
90
-  FORCE_INLINE char* ftostr3(const float &x) { return itostr3(int(FIXFLOAT(x))); }
90
+  FORCE_INLINE char* ftostr3(const float &x) { return itostr3(int(x + (x < 0 ? -0.5 : 0.5))); }
91 91
 
92 92
   #if ENABLED(LCD_DECIMAL_SMALL_XY)
93 93
     // Convert float to rj string with 1234, _123, 12.3, _1.2, -123, _-12, or -1.2 format
94 94
     char* ftostr4sign(const float &fx);
95 95
   #else
96 96
     // Convert float to rj string with 1234, _123, -123, __12, _-12, ___1, or __-1 format
97
-    FORCE_INLINE char* ftostr4sign(const float &x) { return itostr4sign(int(FIXFLOAT(x))); }
97
+    FORCE_INLINE char* ftostr4sign(const float &x) { return itostr4sign(int(x + (x < 0 ? -0.5 : 0.5))); }
98 98
   #endif
99 99
 
100 100
 #endif // ULTRA_LCD

読み込み中…
キャンセル
保存