Skip to content

Commit 69b0ac6

Browse files
authored
Merge pull request #1 from RobTillaart/develop
0.1.4 new initial version
2 parents 03d02e8 + 24b704e commit 69b0ac6

14 files changed

Lines changed: 739 additions & 2 deletions

File tree

.arduino-ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
compile:
2+
# Choosing to run compilation tests on 2 different Arduino platforms
3+
platforms:
4+
- uno
5+
# - due
6+
# - zero
7+
# - leonardo
8+
- m4
9+
- esp32
10+
# - esp8266
11+
# - mega2560

.github/workflows/arduino-lint.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
name: Arduino-lint
3+
4+
on: [push, pull_request]
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
runTest:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb

.github/workflows/jsoncheck.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: json-syntax-check
15+
uses: limitusus/json-syntax-check@v1
16+
with:
17+
pattern: "\\.json$"
18+

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Rob Tillaart
3+
Copyright (c) 2015-2021 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,71 @@
1+
2+
3+
[![Arduino CI](https://github.com/RobTillaart/float16/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
4+
[![Arduino-lint](https://github.com/RobTillaart/float16/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/float16/actions/workflows/arduino-lint.yml)
5+
[![JSON check](https://github.com/RobTillaart/float16/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/float16/actions/workflows/jsoncheck.yml)
6+
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/float16/blob/master/LICENSE)
7+
[![GitHub release](https://img.shields.io/github/release/RobTillaart/float16.svg?maxAge=3600)](https://github.com/RobTillaart/Complex/releases)
8+
9+
110
# float16
2-
Arduino library to implement float16 data type
11+
12+
Arduino library to implement float16 data type.
13+
14+
15+
## Description
16+
17+
This **experimental** library defines the float16 (2 byte) data type, including conversion
18+
function to and from float32 type. It is definitely **work in progress**.
19+
20+
The library implements the **Printable** interface so one can directly print the
21+
float16 values in any stream e.g. Serial.
22+
23+
The primary usage of the float16 data type is to efficiently store and transport
24+
a floating point number. As it is only 2 bytes where float and double have typical
25+
4 and 8, gains can be made at the price of range and precision.
26+
27+
28+
## specs
29+
30+
To elaborate table
31+
32+
| attribute | value |
33+
|:---------:|:----------:|
34+
| Size | 2 bytes |
35+
| mantissa | |
36+
| exponent | |
37+
| minimum | |
38+
| maximum | |
39+
40+
41+
## Interface
42+
43+
See float16.h for a list of functions implemented.
44+
45+
to elaborate
46+
47+
48+
## Notes
49+
50+
51+
#### comparison functions
52+
53+
First version of inequality operations are implemented by converting data to double and compare those.
54+
The strategy is to get these working first and optionally optimize them later.
55+
56+
57+
## TODO (future)
58+
59+
not in priority order.
60+
61+
- update documentation
62+
- get basic math working (+-*/)
63+
- divide by zero errors.
64+
- f16tof32() + f32tof16()
65+
- rewrite toDouble with bit magic
66+
- isNan()
67+
- isINF()
68+
- abs()
69+
- sgn()
70+
71+
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//
2+
// FILE: float16_test0.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+
// Released to the public domain
10+
//
11+
12+
/*
13+
0 01111 0000000000 = 1
14+
0 01111 0000000001 = 1 + 2−10 = 1.0009765625 (next smallest float after 1)
15+
1 10000 0000000000 = −2
16+
17+
0 11110 1111111111 = 65504 (max half precision)
18+
19+
0 00001 0000000000 = 2−14 ≈ 6.10352 × 10−5 (minimum positive normal)
20+
0 00000 1111111111 = 2−14 - 2−24 ≈ 6.09756 × 10−5 (maximum subnormal)
21+
0 00000 0000000001 = 2−24 ≈ 5.96046 × 10−8 (minimum positive subnormal)
22+
23+
0 00000 0000000000 = 0
24+
1 00000 0000000000 = −0
25+
26+
0 11111 0000000000 = infinity
27+
1 11111 0000000000 = −infinity
28+
29+
0 01101 0101010101 = 0.333251953125 ≈ 1/3
30+
*/
31+
32+
#include "float16.h"
33+
34+
uint32_t start;
35+
uint32_t stop;
36+
37+
void setup()
38+
{
39+
Serial.begin(115200);
40+
Serial.print("Start float16_test0, LIB_VERSION: ");
41+
Serial.println(FLOAT16_LIB_VERSION);
42+
Serial.println();
43+
44+
test_constructors();
45+
test_numbers();
46+
test_binary();
47+
test_toDouble();
48+
49+
Serial.println("\ndone...");
50+
}
51+
52+
53+
void loop()
54+
{
55+
}
56+
57+
58+
void test_constructors()
59+
{
60+
Serial.println("\ntest_constructors:");
61+
float16 a;
62+
Serial.println(a.toDouble(), 9);
63+
Serial.println(a.getBinary(), HEX);
64+
65+
float16 b = 6;
66+
Serial.println(b.toDouble(), 9);
67+
Serial.println(b.getBinary(), HEX);
68+
69+
float16 e(EULER);
70+
Serial.println(e.toDouble(), 9);
71+
Serial.println(e.getBinary(), HEX);
72+
73+
float16 f(e);
74+
Serial.println(f.toDouble(), 9);
75+
Serial.println(f.getBinary(), HEX);
76+
77+
f = 1.61803398875; // golden ratio
78+
Serial.println(f.toDouble(), 9);
79+
Serial.println(f.getBinary(), HEX);
80+
81+
Serial.println();
82+
}
83+
84+
85+
void test_toDouble()
86+
{
87+
Serial.println("\ntest_toDouble:");
88+
float16 f(3.1415);
89+
Serial.println(f.toDouble(), 9); // 3.14
90+
f.setBinary(0x0000);
91+
Serial.println(f.toDouble(), 9); // 0
92+
f.setBinary(0xFC00);
93+
Serial.println(f.toDouble(), 9); //-inf
94+
f.setBinary(0x7C00);
95+
Serial.println(f.toDouble(), 9); // inf
96+
f.setBinary(0x3C00);
97+
Serial.println(f.toDouble(), 9); // 1
98+
f.setBinary(0x3C01);
99+
Serial.println(f.toDouble(), 9); // 1.0009765625
100+
f.setBinary(0x3555);
101+
Serial.println(f.toDouble(), 9); // 0.333333
102+
Serial.println();
103+
}
104+
105+
106+
void test_binary()
107+
{
108+
Serial.println("\ntest_binary:");
109+
float16 e(3.1415);
110+
float16 f(0);
111+
Serial.println(e.toDouble(), 9);
112+
f.setBinary(e.getBinary());
113+
Serial.println(f.toDouble(), 9);
114+
Serial.println();
115+
}
116+
117+
118+
void test_numbers()
119+
{
120+
Serial.println("\ntest_numbers:");
121+
Serial.println("** ZERO **");
122+
float16 a(0);
123+
Serial.println(a.toDouble(), 9);
124+
125+
Serial.println("** SUBNORMAL **");
126+
float16 b(1 / 1000.0);
127+
Serial.println(b.toDouble(), 9);
128+
129+
Serial.println("** NORMAL **");
130+
float16 c(1 / 2000.0);
131+
Serial.println(c.toDouble(), 9);
132+
133+
c = 1/3.0;
134+
Serial.println(c.toDouble(), 9);
135+
136+
float16 d(1);
137+
Serial.println(d.toDouble(), 9);
138+
139+
float16 e(PI);
140+
Serial.println(e.toDouble(), 9);
141+
142+
Serial.println("** OVERFLOW **");
143+
float16 f(1000000.0);
144+
Serial.println(f.toDouble(), 9);
145+
146+
Serial.println("** UNDERFLOW **");
147+
float16 g(1 / 1000000.0);
148+
Serial.println(g.toDouble(), 9);
149+
150+
Serial.println();
151+
}
152+
153+
154+
// -- END OF FILE --
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// FILE: .ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.00
5+
// PURPOSE:
6+
// DATE:
7+
// URL:
8+
//
9+
// Released to the public domain
10+
//
11+
12+
#include "float16.h"
13+
14+
float16 X;
15+
16+
uint32_t start;
17+
uint32_t stop;
18+
19+
void setup()
20+
{
21+
Serial.begin(115200);
22+
Serial.println("Start ");
23+
24+
float f;
25+
26+
for (uint16_t n = 0; n < 65535; n++)
27+
{
28+
f = X.f16tof32(n);
29+
Serial.print(n);
30+
Serial.print('\t');
31+
Serial.println(f, 6);
32+
33+
}
34+
Serial.println("done");
35+
}
36+
37+
void loop()
38+
{
39+
}
40+
41+
42+
// -- END OF FILE --
43+
44+

0 commit comments

Comments
 (0)