This function prints a floating-point value in a format similar to * printf("%.6g"). It can work with either single- or double-precision, * but for simplicity, it prints only 6 significant digits in either case. * Printing more than 6 digits accurately is hard (at least in the single- * precision case) and isn't attempted here. */
| 264 | * Printing more than 6 digits accurately is hard (at least in the single- |
| 265 | * precision case) and isn't attempted here. */ |
| 266 | void UnityPrintFloat(const UNITY_DOUBLE input_number) |
| 267 | { |
| 268 | UNITY_DOUBLE number = input_number; |
| 269 | |
| 270 | /* print minus sign (including for negative zero) */ |
| 271 | if (number < (double)0.0f || (number == (double)0.0f && (double)1.0f / number < (double)0.0f)) |
| 272 | { |
| 273 | UNITY_OUTPUT_CHAR('-'); |
| 274 | number = -number; |
| 275 | } |
| 276 | |
| 277 | /* handle zero, NaN, and +/- infinity */ |
| 278 | if (number == (double)0.0f) UnityPrint("0"); |
| 279 | else if (isnan(number)) UnityPrint("nan"); |
| 280 | else if (isinf(number)) UnityPrint("inf"); |
| 281 | else |
| 282 | { |
| 283 | int exponent = 0; |
| 284 | int decimals, digits; |
| 285 | UNITY_INT32 n; |
| 286 | char buf[16]; |
| 287 | |
| 288 | /* scale up or down by powers of 10 */ |
| 289 | while (number < (double)(100000.0f / 1e6f)) { number *= (double)1e6f; exponent -= 6; } |
| 290 | while (number < (double)100000.0f) { number *= (double)10.0f; exponent--; } |
| 291 | while (number > (double)(1000000.0f * 1e6f)) { number /= (double)1e6f; exponent += 6; } |
| 292 | while (number > (double)1000000.0f) { number /= (double)10.0f; exponent++; } |
| 293 | |
| 294 | /* round to nearest integer */ |
| 295 | n = ((UNITY_INT32)(number + number) + 1) / 2; |
| 296 | if (n > 999999) |
| 297 | { |
| 298 | n = 100000; |
| 299 | exponent++; |
| 300 | } |
| 301 | |
| 302 | /* determine where to place decimal point */ |
| 303 | decimals = (exponent <= 0 && exponent >= -9) ? -exponent : 5; |
| 304 | exponent += decimals; |
| 305 | |
| 306 | /* truncate trailing zeroes after decimal point */ |
| 307 | while (decimals > 0 && n % 10 == 0) |
| 308 | { |
| 309 | n /= 10; |
| 310 | decimals--; |
| 311 | } |
| 312 | |
| 313 | /* build up buffer in reverse order */ |
| 314 | digits = 0; |
| 315 | while (n != 0 || digits < decimals + 1) |
| 316 | { |
| 317 | buf[digits++] = (char)('0' + n % 10); |
| 318 | n /= 10; |
| 319 | } |
| 320 | while (digits > 0) |
| 321 | { |
| 322 | if(digits == decimals) UNITY_OUTPUT_CHAR('.'); |
| 323 | UNITY_OUTPUT_CHAR(buf[--digits]); |
searching dependent graphs…