GY-63_MS5611/libraries/IEEE754tools/examples/float2double/float2double.ino

112 lines
1.9 KiB
Arduino
Raw Normal View History

2020-11-27 05:16:22 -05:00
//
// FILE: float2double.ino
// AUTHOR: Rob Tillaart
// PURPOSE: experimental expands a float in a IEEE 754 double to be printed to PC.
//
2021-11-05 12:24:10 -04:00
// NOTE
2021-12-20 02:09:14 -05:00
// - WORKS ON AVR
2021-11-05 12:24:10 -04:00
// - FAILS ON ESP32
2020-11-27 05:16:22 -05:00
#include <IEEE754tools.h>
byte x[8];
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void setup()
{
Serial.begin(115200);
2023-11-06 09:19:19 -05:00
Serial.println();
Serial.println(__FILE__);
Serial.print("IEEE754_LIB_VERSION: ");
Serial.println(IEEE754_LIB_VERSION);
Serial.println();
2020-11-27 05:16:22 -05:00
test1();
test2();
test3();
test4();
Serial.println("done");
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void test1()
{
Serial.println();
2021-11-05 12:24:10 -04:00
Serial.println(__FUNCTION__);
2020-11-27 05:16:22 -05:00
for (float f = -50.0; f < 50.0; f += 10.0)
{
dumpFloat(f);
float2DoublePacked(f, x, LSBFIRST);
dumpByteArray(x);
float g = doublePacked2Float(x, LSBFIRST);
Serial.println(g, 10);
if (f != g) Serial.println("-- FAIL --");
Serial.println();
}
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void test2()
{
2021-11-05 12:24:10 -04:00
Serial.println();
Serial.println(__FUNCTION__);
2020-11-27 05:16:22 -05:00
Serial.println("\n0.15625");
dumpFloat(0.15625);
2023-11-06 09:19:19 -05:00
// sign = 0
// exponent = 7C
// mantissa = 0020 0000
2020-11-27 05:16:22 -05:00
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void test3()
{
2021-11-05 12:24:10 -04:00
Serial.println();
Serial.println(__FUNCTION__);
Serial.println("PI-check");
2020-11-27 05:16:22 -05:00
Serial.println(PI, 20);
float2DoublePacked(PI, x);
dumpByteArray(x);
float f = doublePacked2Float(x, LSBFIRST);
Serial.println(f, 20);
Serial.println();
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void test4()
{
2021-11-05 12:24:10 -04:00
Serial.println();
Serial.println(__FUNCTION__);
Serial.println("BIG-check");
2020-11-27 05:16:22 -05:00
Serial.println(1.23456789e38, 20);
dumpFloat(1.23456789e38);
float2DoublePacked(1.23456789e38, x);
dumpByteArray(x);
float f = doublePacked2Float(x, LSBFIRST);
2023-11-06 09:19:19 -05:00
Serial.println(f / 1e38, 20); // divide prevents overflow (ovf) in output
2020-11-27 05:16:22 -05:00
dumpFloat(f);
Serial.println();
}
void loop()
{
}
2021-12-20 02:09:14 -05:00
2020-11-27 05:16:22 -05:00
void dumpByteArray(byte *ar)
{
for (int i = 0; i < 8; i++)
{
if (ar[i] < 0x10) Serial.print('0');
Serial.print(ar[i], HEX);
2021-11-05 12:24:10 -04:00
Serial.print(' ');
2020-11-27 05:16:22 -05:00
}
Serial.println();
}
2021-11-05 12:24:10 -04:00
2023-11-06 09:19:19 -05:00
// -- END OF FILE --
2021-12-20 02:09:14 -05:00