Skip to content

Commit 5ee4e07

Browse files
authored
Develop (#3)
* add unit tests * fix negative numbers
1 parent 69b0ac6 commit 5ee4e07

10 files changed

Lines changed: 497 additions & 72 deletions

File tree

README.md

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,43 @@ a floating point number. As it is only 2 bytes where float and double have typic
2525
4 and 8, gains can be made at the price of range and precision.
2626

2727

28-
## specs
28+
## Specifications
2929

30-
To elaborate table
3130

32-
| attribute | value |
33-
|:---------:|:----------:|
34-
| Size | 2 bytes |
35-
| mantissa | |
36-
| exponent | |
37-
| minimum | |
38-
| maximum | |
31+
| attribute | value | notes |
32+
|:----------|:-------------|:-------------|
33+
| Size | 2 bytes | |
34+
| sign | 1 bit | |
35+
| mantissa | 11 bit | ~ 3 digits |
36+
| exponent | 4 bit | |
37+
| minimum | 1.0009765625 | 1 + 2^−10 |
38+
| maximum | 65504 | |
39+
| | | |
3940

4041

4142
## Interface
4243

43-
See float16.h for a list of functions implemented.
44+
to elaborate
45+
46+
#### Constructors
47+
48+
- **float16(void)** defaults to zero.
49+
- **float16(double f)** constructor.
50+
- **float16(const float16 &f)** copy constructor.
51+
52+
53+
#### Conversion
54+
55+
- **double toDouble(void)** convert to double (or float)
56+
- **size_t printTo(Print& p) const** Printable interface.
57+
- **void setDecimals(uint8_t d)** idem, used for printTo.
58+
- **uint8_t getDecimals()** idem.
59+
60+
Note the setDecimals takes one byte per object which is not efficient for arrays.
61+
See array example for efficient storage using set/getBinary() functions.
62+
63+
64+
#### Compare
4465

4566
to elaborate
4667

@@ -56,16 +77,47 @@ The strategy is to get these working first and optionally optimize them later.
5677

5778
## TODO (future)
5879

59-
not in priority order.
80+
to get focus on getting things done...
81+
82+
83+
#### 0.1.4
84+
85+
the following should work:
86+
87+
- update documentation
88+
- positive numbers
89+
- negative numbers
90+
- infinity
91+
- rounding to zero (e.g. 1e-30)
92+
- array of numbers.
93+
- unit tests of the above..
94+
95+
96+
#### 0.1.5
97+
98+
- update documentation
99+
- comparison operators
100+
- unit tests of the above..
101+
102+
103+
#### 0.1.6
60104

61105
- update documentation
62106
- get basic math working (+-*/)
63-
- divide by zero errors.
64-
- f16tof32() + f32tof16()
65-
- rewrite toDouble with bit magic
66107
- isNan()
67108
- isINF()
68109
- abs()
69110
- sgn()
111+
- unit tests of the above..
112+
113+
114+
#### later
115+
116+
- update documentation
117+
- get basic math II working += -= *= /=
118+
- divide by zero errors.
119+
- f16tof32() + f32tof16()
120+
- rewrite toDouble with bit magic
121+
- ...
70122

71123

examples/float16_test0/float16_test0.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@
3131

3232
#include "float16.h"
3333

34-
uint32_t start;
35-
uint32_t stop;
3634

3735
void setup()
3836
{
37+
while(!Serial);
3938
Serial.begin(115200);
40-
Serial.print("Start float16_test0, LIB_VERSION: ");
39+
Serial.println(__FILE__);
40+
Serial.print("FLOAT16_LIB_VERSION: ");
4141
Serial.println(FLOAT16_LIB_VERSION);
42+
Serial.println("\nStart ");
43+
4244
Serial.println();
4345

4446
test_constructors();

examples/float16_test1/float16_test1.ino

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
//
2-
// FILE: .ino
2+
// FILE: float16_test1.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.00
5-
// PURPOSE:
6-
// DATE:
7-
// URL:
8-
//
9-
// Released to the public domain
4+
// VERSION: 0.1.0
5+
// PURPOSE: test float16
6+
// DATE: 2015-03-11
7+
// URL: https://github.com/RobTillaart/float16
108
//
119

1210
#include "float16.h"
1311

1412
float16 X;
1513

16-
uint32_t start;
17-
uint32_t stop;
1814

1915
void setup()
2016
{
17+
while(!Serial);
2118
Serial.begin(115200);
22-
Serial.println("Start ");
19+
Serial.println(__FILE__);
20+
Serial.print("FLOAT16_LIB_VERSION: ");
21+
Serial.println(FLOAT16_LIB_VERSION);
22+
Serial.println("\nStart ");
2323

2424
float f;
2525

@@ -29,11 +29,12 @@ void setup()
2929
Serial.print(n);
3030
Serial.print('\t');
3131
Serial.println(f, 6);
32-
3332
}
34-
Serial.println("done");
33+
34+
Serial.println("\ndone");
3535
}
3636

37+
3738
void loop()
3839
{
3940
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// FILE: float16_test_array.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: test float16
6+
// DATE: 2015-03-11
7+
// URL: https://github.com/RobTillaart/float16
8+
//
9+
10+
// show different storage needs
11+
12+
#include "float16.h"
13+
14+
float16 temperature[10]; // pointers + object are stored.
15+
uint16_t temp2[10]; // only raw data is stored.
16+
17+
18+
void setup()
19+
{
20+
while (!Serial);
21+
Serial.begin(115200);
22+
Serial.println(__FILE__);
23+
Serial.print("FLOAT16_LIB_VERSION: ");
24+
Serial.println(FLOAT16_LIB_VERSION);
25+
Serial.println("\nStart ");
26+
27+
// simulate temperature with random numbers
28+
for (uint32_t n = 0; n < 10; n++)
29+
{
30+
temperature[n] = (random(1000) - 300) * 0.01;
31+
temp2[n] = temperature[n].getBinary();
32+
}
33+
Serial.println();
34+
35+
36+
float average = 0;
37+
for (uint32_t n = 0; n < 10; n++)
38+
{
39+
Serial.print(n);
40+
Serial.print("\t");
41+
Serial.print(temperature[n].toDouble());
42+
Serial.println();
43+
average += temperature[n].toDouble();
44+
}
45+
Serial.print("\t");
46+
Serial.println(average * 0.01);
47+
Serial.println();
48+
49+
50+
average = 0;
51+
for (uint32_t n = 0; n < 10; n++)
52+
{
53+
float16 f16;
54+
f16.setBinary(temp2[n]);
55+
Serial.print(n);
56+
Serial.print("\t");
57+
Serial.print(f16.toDouble());
58+
Serial.println();
59+
average += f16.toDouble();
60+
}
61+
Serial.print("\t");
62+
Serial.println(average * 0.01);
63+
Serial.println();
64+
65+
66+
Serial.print("SIZE: ");
67+
Serial.println(sizeof(temperature));
68+
Serial.print("SIZE: ");
69+
Serial.println(sizeof(temp2));
70+
71+
Serial.println("\ndone");
72+
}
73+
74+
75+
void loop()
76+
{
77+
}
78+
79+
80+
// -- END OF FILE --
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// FILE: float16_test_negative.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: test float16
6+
// DATE: 2021-11-26
7+
// URL: https://github.com/RobTillaart/float16
8+
//
9+
10+
#include "float16.h"
11+
12+
13+
void setup()
14+
{
15+
while(!Serial);
16+
Serial.begin(115200);
17+
Serial.println(__FILE__);
18+
Serial.print("FLOAT16_LIB_VERSION: ");
19+
Serial.println(FLOAT16_LIB_VERSION);
20+
Serial.println("\nStart ");
21+
22+
23+
for( int i = -10; i < 2; i++)
24+
{
25+
float16 f16(i);
26+
Serial.print(i);
27+
Serial.print("\t");
28+
Serial.println(f16.toDouble(), 4);
29+
}
30+
31+
Serial.println("\ndone");
32+
}
33+
34+
35+
void loop()
36+
{
37+
}
38+
39+
40+
// -- END OF FILE --
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// FILE: float16_test_performance.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: test float16
6+
// DATE: 2021-11-26
7+
// URL: https://github.com/RobTillaart/float16
8+
//
9+
10+
// TO ELABORATE
11+
12+
#include "float16.h"
13+
14+
uint32_t start, stop;
15+
volatile float f;
16+
17+
18+
void setup()
19+
{
20+
while (!Serial);
21+
Serial.begin(115200);
22+
Serial.println(__FILE__);
23+
Serial.print("FLOAT16_LIB_VERSION: ");
24+
Serial.println(FLOAT16_LIB_VERSION);
25+
Serial.println("\nStart ");
26+
27+
f = random(1000000) * 0.001;
28+
29+
start = micros();
30+
float16 f16(f);
31+
stop = micros();
32+
Serial.print("Constructor: \t");
33+
Serial.println(stop - start);
34+
delay(10);
35+
36+
start = micros();
37+
f = f16.toDouble();
38+
stop = micros();
39+
Serial.print("toDouble(): \t");
40+
Serial.println(stop - start);
41+
delay(10);
42+
43+
Serial.println("\ndone");
44+
}
45+
46+
47+
void loop()
48+
{
49+
}
50+
51+
52+
// -- END OF FILE --

0 commit comments

Comments
 (0)