-
Notifications
You must be signed in to change notification settings - Fork 5
/
printIntAsFloat.h
54 lines (47 loc) · 1.36 KB
/
printIntAsFloat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* MIT License
* Copyright (c) 2021 Brian T. Park
*/
/**
* @file printIntAsFloat.h
*
* Print integers as floating point numbers, without using floatin point
* operations which are expensive on 8-bit processors.
*
* * printUint16AsFloat3To()
* * divide an unsigned integer (uint16_t) by 1000 and print the result as a
* floating point number to 3 decimal places
* * printUint32AsFloat3To()
* * divide an unsigned integer (uint32_t) by 1000 and print the result as a
* floating point number to 3 decimal places
*/
#ifndef ACE_COMMON_PRINT_INT_AS_FLOAT_H
#define ACE_COMMON_PRINT_INT_AS_FLOAT_H
#include <stdint.h>
#include <Print.h>
#include "printPadTo.h"
namespace ace_common {
/**
* Print a uint16 (e.g. 12345U) as a float after dividing by 1000 (i.e.
* "12.345").
*/
inline void printUint16AsFloat3To(Print& printer, uint16_t value) {
uint16_t whole = value / 1000;
uint16_t frac = value % 1000;
printer.print(whole);
printer.print('.');
printPad3To(printer, frac, '0');
}
/**
* Print a uint32 (e.g. 123456UL) as a float after dividing by 1000 (i.e.
* "123.456").
*/
inline void printUint32AsFloat3To(Print& printer, uint32_t value) {
uint32_t whole = value / 1000;
uint16_t frac = value % 1000;
printer.print(whole);
printer.print('.');
printPad3To(printer, frac, '0');
}
}
#endif