-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update unit tests * add example code (tests) * some refactor
- Loading branch information
1 parent
5ee4e07
commit ea2e710
Showing
6 changed files
with
311 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// | ||
// FILE: float16_test_all.ino | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.0 | ||
// PURPOSE: test float16 | ||
// DATE: 2021-11-27 | ||
// URL: https://github.com/RobTillaart/float16 | ||
// | ||
|
||
// test all values except the NAN | ||
// test_1 takes ~ 2 minutes on UNO @ 115200baud | ||
|
||
// https://github.com/RobTillaart/float16/issues/2 | ||
|
||
|
||
#include "float16.h" | ||
|
||
float16 f16; | ||
|
||
uint32_t start, stop; | ||
uint32_t errors = 0; | ||
float prev; | ||
|
||
|
||
void setup() | ||
{ | ||
while (!Serial); | ||
Serial.begin(115200); | ||
Serial.println(__FILE__); | ||
Serial.print("FLOAT16_LIB_VERSION: "); | ||
Serial.println(FLOAT16_LIB_VERSION); | ||
Serial.println("\nStart "); | ||
|
||
f16.setDecimals(6); | ||
|
||
test_1(); | ||
test_2(); | ||
} | ||
|
||
|
||
void loop() | ||
{ | ||
} | ||
|
||
|
||
void test_2() | ||
{ | ||
start = millis(); | ||
for (uint32_t x = 0x0001; x < 0x7C01; x++) | ||
{ | ||
if (x % 16 == 0) Serial.println(); | ||
f16.setBinary(x); | ||
float16 f17 = f16.toDouble(); | ||
Serial.print(x, HEX); | ||
Serial.print("\t"); | ||
Serial.print(f17.toDouble() - f16.toDouble(), 5); | ||
Serial.println(); | ||
} | ||
stop = millis(); | ||
Serial.println(); | ||
Serial.print(" TIME: "); | ||
Serial.println(stop - start); | ||
} | ||
|
||
|
||
void test_1() | ||
{ | ||
// POSITIVE NUMBERS | ||
prev = 0; | ||
errors = 0; | ||
start = millis(); | ||
for (uint32_t x = 0x0000; x < 0x7C01; x++) | ||
{ | ||
if (x % 16 == 0) Serial.println(); | ||
f16.setBinary(x); | ||
Serial.print(x, HEX); | ||
Serial.print('\t'); | ||
float current = f16.toDouble(); | ||
Serial.print(current, 8); | ||
if (prev > current) | ||
{ | ||
Serial.print("\t\tERROR"); | ||
errors++; | ||
} | ||
prev = current; | ||
Serial.println(); | ||
} | ||
stop = millis(); | ||
Serial.println(); | ||
Serial.print(" TIME: "); | ||
Serial.println(stop - start); | ||
Serial.print("ERRORS: "); | ||
Serial.println(errors); | ||
Serial.println(); | ||
Serial.println(); | ||
|
||
|
||
// NEGATIVE NUMBERS | ||
prev = 0; | ||
errors = 0; | ||
start = millis(); | ||
for (uint32_t x = 0x8000; x < 0xFC01; x++) | ||
{ | ||
if (x % 16 == 0) Serial.println(); | ||
f16.setBinary(x); | ||
Serial.print(x, HEX); | ||
Serial.print('\t'); | ||
float current = f16.toDouble(); | ||
Serial.print(current, 8); | ||
if (prev < current) | ||
{ | ||
Serial.print("\t\tERROR"); | ||
errors++; | ||
} | ||
prev = current; | ||
Serial.println(); | ||
} | ||
stop = millis(); | ||
Serial.println(); | ||
Serial.print(" TIME: "); | ||
Serial.println(stop - start); | ||
Serial.print("ERRORS: "); | ||
Serial.println(errors); | ||
Serial.println(); | ||
Serial.println(); | ||
|
||
|
||
Serial.println("\ndone"); | ||
} | ||
|
||
|
||
|
||
// -- END OF FILE -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// test: UNO | ||
// IDE: 1.8.13 | ||
// | ||
|
||
FLOAT16_LIB_VERSION: 0.1.4 | ||
|
||
Constructor: 24 | ||
a = b: 4 | ||
toDouble(): 396 | ||
|
||
compare == : 4 | ||
compare != : 4 | ||
compare < : 784 | ||
compare <= : 784 | ||
compare >= : 784 | ||
compare > : 780 | ||
|
||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// FILE: float16_test_special.ino | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.0 | ||
// PURPOSE: test float16 | ||
// DATE: 2021-11-26 | ||
// URL: https://github.com/RobTillaart/float16 | ||
// | ||
|
||
// test special values ... | ||
// https://github.com/RobTillaart/float16/issues/2 | ||
|
||
|
||
#include "float16.h" | ||
|
||
uint16_t value[32] = | ||
{ | ||
0xFC00, 0xF400, 0xEC00, 0xE400, 0xDC00, 0xD400, 0xCC00, 0xC400, | ||
0xBC00, 0xB400, 0xAC00, 0xA400, 0x9C00, 0x9400, 0x8C00, 0x8400, | ||
0x0400, 0x0C00, 0x1400, 0x1C00, 0x2400, 0x2C00, 0x3400, 0x3C00, | ||
0x4400, 0x4C00, 0x5400, 0x5C00, 0x6400, 0x6C00, 0x7400, 0x7C00 | ||
}; | ||
|
||
float16 f16; | ||
|
||
|
||
void setup() | ||
{ | ||
while (!Serial); | ||
Serial.begin(115200); | ||
Serial.println(__FILE__); | ||
Serial.print("FLOAT16_LIB_VERSION: "); | ||
Serial.println(FLOAT16_LIB_VERSION); | ||
Serial.println("\nStart "); | ||
|
||
f16.setDecimals(6); | ||
|
||
for (int i = 0; i < 32; i++) | ||
{ | ||
f16.setBinary(value[i]); | ||
Serial.print(value[i], HEX); | ||
Serial.print("\t"); | ||
Serial.print(f16); | ||
Serial.print("\t"); | ||
Serial.print(f16.toDouble(), 6); | ||
Serial.print("\t"); | ||
Serial.println(); | ||
} | ||
|
||
Serial.println(); | ||
Serial.println(); | ||
|
||
for (uint32_t x = 1; x < 65535; x *= 4) | ||
{ | ||
f16 = x; | ||
Serial.print(f16.getBinary(), HEX); | ||
Serial.print("\t"); | ||
Serial.print(f16); | ||
Serial.print("\t"); | ||
Serial.print(f16.toDouble(), 6); | ||
Serial.print("\t"); | ||
Serial.println(); | ||
} | ||
|
||
Serial.println("\ndone"); | ||
} | ||
|
||
|
||
void loop() | ||
{ | ||
} | ||
|
||
|
||
// -- END OF FILE -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.